From fd486bee24bd6f5fd065ed150cebf72ef14afc0a Mon Sep 17 00:00:00 2001
From: brinn <brinn>
Date: Tue, 15 Jul 2008 07:03:09 +0000
Subject: [PATCH] fix: logic on when to sleep

SVN: 7230
---
 .../cisd/common/process/CallableExecutor.java | 22 +++++++++++--------
 .../common/process/CallableExecutorTest.java  |  8 ++++++-
 2 files changed, 20 insertions(+), 10 deletions(-)

diff --git a/common/source/java/ch/systemsx/cisd/common/process/CallableExecutor.java b/common/source/java/ch/systemsx/cisd/common/process/CallableExecutor.java
index 80692a633ff..65d6cdda658 100644
--- a/common/source/java/ch/systemsx/cisd/common/process/CallableExecutor.java
+++ b/common/source/java/ch/systemsx/cisd/common/process/CallableExecutor.java
@@ -29,26 +29,26 @@ import ch.systemsx.cisd.common.exceptions.StopException;
  */
 public final class CallableExecutor
 {
-    private final int maxRetryOnFailure;
+    private final int maxInvocationsOnFailure;
 
     private final long millisToSleepOnFailure;
 
     public CallableExecutor()
     {
-        this(Constants.MAXIMUM_RETRY_COUNT, Constants.MILLIS_TO_SLEEP_BEFORE_RETRYING);
+        this(Constants.MAXIMUM_INVOCATIONS_ON_FAILURE, Constants.MILLIS_TO_SLEEP_BEFORE_RETRYING);
     }
 
-    public CallableExecutor(final int maxRetryOnFailure, final long millisToSleepOnFailure)
+    public CallableExecutor(final int maxInvocationsOnFailure, final long millisToSleepOnFailure)
     {
         assert millisToSleepOnFailure > -1 : "Negative value";
-        assert maxRetryOnFailure > -1 : "Negative value";
-        this.maxRetryOnFailure = maxRetryOnFailure;
+        assert maxInvocationsOnFailure > -1 : "Negative value";
+        this.maxInvocationsOnFailure = maxInvocationsOnFailure;
         this.millisToSleepOnFailure = millisToSleepOnFailure;
     }
 
     /**
      * Executes given <var>callable</var> until it returns a non-<code>null</code> value (or
-     * until <code>maxRetryOnFailure</code> is reached).
+     * until <code>maxInvocationsOnFailure</code> is reached).
      */
     public final <T> T executeCallable(final Callable<T> callable) throws StopException
     {
@@ -60,11 +60,15 @@ public final class CallableExecutor
             {
                 StopException.check();
                 result = callable.call();
-                if (counter > 0 && millisToSleepOnFailure > 0)
+                if (result == null)
                 {
-                    Thread.sleep(millisToSleepOnFailure);
+                    ++counter;
+                    if (counter < maxInvocationsOnFailure && millisToSleepOnFailure > 0)
+                    {
+                        Thread.sleep(millisToSleepOnFailure);
+                    }
                 }
-            } while (counter++ < maxRetryOnFailure && result == null);
+            } while (result == null && counter < maxInvocationsOnFailure);
         } catch (final Exception ex)
         {
             throw CheckedExceptionTunnel.wrapIfNecessary(ex);
diff --git a/common/sourceTest/java/ch/systemsx/cisd/common/process/CallableExecutorTest.java b/common/sourceTest/java/ch/systemsx/cisd/common/process/CallableExecutorTest.java
index d6ba0ed43b6..6a97a96f438 100644
--- a/common/sourceTest/java/ch/systemsx/cisd/common/process/CallableExecutorTest.java
+++ b/common/sourceTest/java/ch/systemsx/cisd/common/process/CallableExecutorTest.java
@@ -95,7 +95,13 @@ public final class CallableExecutorTest
         context.checking(new Expectations()
             {
                 {
-                    exactly(maxRetryOnFailure + 1).of(callable).call();
+                    if (maxRetryOnFailure == 0)
+                    {
+                        one(callable).call();
+                    } else
+                    {
+                        exactly(maxRetryOnFailure).of(callable).call();
+                    }
                     will(returnValue(null));
                 }
             });
-- 
GitLab