From 867b7e0b56ee784f04c9a02298216a3c8f946005 Mon Sep 17 00:00:00 2001
From: Jean-Noël Rouvignac <jean-noel.rouvignac@forgerock.com>
Date: Wed, 02 Nov 2016 15:53:49 +0000
Subject: [PATCH] TestTimer.java: Added repeatUntilSuccess(CallableVoid callable)

---
 opendj-server-legacy/src/test/java/org/opends/server/replication/GenerationIdTest.java        |   22 +++----
 opendj-server-legacy/src/test/java/org/opends/server/replication/plugin/StateMachineTest.java |   12 +--
 opendj-server-legacy/src/test/java/org/opends/server/util/TestTimer.java                      |   64 +++++++++++++++++++-
 opendj-server-legacy/src/test/java/org/opends/server/backends/ChangelogBackendTestCase.java   |   12 +--
 opendj-server-legacy/src/test/java/org/opends/server/replication/UpdateOperationTest.java     |   12 +--
 opendj-server-legacy/src/test/java/org/opends/server/protocols/jmx/JmxTestCase.java           |    7 +-
 opendj-server-legacy/src/test/java/org/opends/server/replication/SchemaReplicationTest.java   |    9 +-
 opendj-server-legacy/src/test/java/org/opends/server/replication/plugin/HistoricalTest.java   |    7 +-
 opendj-server-legacy/src/test/java/org/opends/server/replication/ReplicationTestCase.java     |   18 ++---
 9 files changed, 101 insertions(+), 62 deletions(-)

diff --git a/opendj-server-legacy/src/test/java/org/opends/server/backends/ChangelogBackendTestCase.java b/opendj-server-legacy/src/test/java/org/opends/server/backends/ChangelogBackendTestCase.java
index 39b369e..a3e5414 100644
--- a/opendj-server-legacy/src/test/java/org/opends/server/backends/ChangelogBackendTestCase.java
+++ b/opendj-server-legacy/src/test/java/org/opends/server/backends/ChangelogBackendTestCase.java
@@ -29,7 +29,6 @@
 import static org.opends.server.util.CollectionUtils.*;
 import static org.opends.server.util.ServerConstants.*;
 import static org.opends.server.util.StaticUtils.*;
-import static org.opends.server.util.TestTimer.*;
 import static org.testng.Assert.*;
 
 import java.io.ByteArrayOutputStream;
@@ -103,6 +102,7 @@
 import org.opends.server.types.SearchResultEntry;
 import org.opends.server.util.LDIFWriter;
 import org.opends.server.util.TestTimer;
+import org.opends.server.util.TestTimer.CallableVoid;
 import org.opends.server.util.TimeThread;
 import org.opends.server.workflowelement.localbackend.LocalBackendModifyDNOperation;
 import org.testng.annotations.AfterClass;
@@ -482,10 +482,10 @@
       .maxSleep(3, SECONDS)
       .sleepTimes(100, MILLISECONDS)
       .toTimer();
-    timer.repeatUntilSuccess(new Callable<Void>()
+    timer.repeatUntilSuccess(new CallableVoid()
     {
       @Override
-      public Void call() throws Exception
+      public void call() throws Exception
       {
         final ReplicationDomainDB domainDB = replicationServer.getChangelogDB().getReplicationDomainDB();
         CursorOptions options = new CursorOptions(GREATER_THAN_OR_EQUAL_TO_KEY, ON_MATCHING_KEY);
@@ -494,7 +494,6 @@
         {
           assertTrue(cursor.next(), "Expected to find at least one change in replicaDB for " + replicaId);
           assertEquals(cursor.getRecord().getCSN(), csn);
-          return END_RUN;
         }
       }
     });
@@ -1241,13 +1240,12 @@
       .maxSleep(500, MILLISECONDS)
       .sleepTimes(50, MILLISECONDS)
       .toTimer();
-    timer.repeatUntilSuccess(new Callable<Void>()
+    timer.repeatUntilSuccess(new CallableVoid()
     {
       @Override
-      public Void call() throws Exception
+      public void call() throws Exception
       {
         assertEquals(operation.getResultCode(), expectedResult, operation.getErrorMessage().toString());
-        return END_RUN;
       }
     });
   }
diff --git a/opendj-server-legacy/src/test/java/org/opends/server/protocols/jmx/JmxTestCase.java b/opendj-server-legacy/src/test/java/org/opends/server/protocols/jmx/JmxTestCase.java
index 12fa6f6..0ec2bd6 100644
--- a/opendj-server-legacy/src/test/java/org/opends/server/protocols/jmx/JmxTestCase.java
+++ b/opendj-server-legacy/src/test/java/org/opends/server/protocols/jmx/JmxTestCase.java
@@ -24,7 +24,6 @@
 import static org.testng.Assert.*;
 
 import java.util.List;
-import java.util.concurrent.Callable;
 
 import org.assertj.core.api.Assertions;
 import org.forgerock.opendj.ldap.DN;
@@ -36,6 +35,7 @@
 import org.opends.server.types.Attributes;
 import org.opends.server.types.Modification;
 import org.opends.server.util.TestTimer;
+import org.opends.server.util.TestTimer.CallableVoid;
 import org.testng.annotations.BeforeClass;
 import org.testng.annotations.Test;
 
@@ -83,14 +83,13 @@
       .maxSleep(20, SECONDS)
       .sleepTimes(200, MILLISECONDS)
       .toTimer();
-    timer.repeatUntilSuccess(new Callable<Void>()
+    timer.repeatUntilSuccess(new CallableVoid()
     {
       @Override
-      public Void call() throws Exception
+      public void call() throws Exception
       {
         Assertions.assertThat(rmiConnector.jmxRmiConnectorNoClientCertificate).isNotNull();
         Assertions.assertThat(rmiConnector.jmxRmiConnectorNoClientCertificate.isActive()).isTrue();
-        return null;
       }
     });
     return jmxConnectionHandler;
diff --git a/opendj-server-legacy/src/test/java/org/opends/server/replication/GenerationIdTest.java b/opendj-server-legacy/src/test/java/org/opends/server/replication/GenerationIdTest.java
index 5b32d27..8c1ed23 100644
--- a/opendj-server-legacy/src/test/java/org/opends/server/replication/GenerationIdTest.java
+++ b/opendj-server-legacy/src/test/java/org/opends/server/replication/GenerationIdTest.java
@@ -27,7 +27,6 @@
 import java.util.SortedSet;
 import java.util.TreeSet;
 import java.util.UUID;
-import java.util.concurrent.Callable;
 
 import org.assertj.core.api.Assertions;
 import org.assertj.core.api.SoftAssertions;
@@ -61,6 +60,7 @@
 import org.opends.server.types.Entry;
 import org.opends.server.types.LDIFImportConfig;
 import org.opends.server.util.TestTimer;
+import org.opends.server.util.TestTimer.CallableVoid;
 import org.testng.annotations.BeforeClass;
 import org.testng.annotations.Test;
 
@@ -366,16 +366,15 @@
         .maxSleep(1, MINUTES)
         .sleepTimes(200, MILLISECONDS)
         .toTimer();
-      timer.repeatUntilSuccess(new Callable<Void>()
+      timer.repeatUntilSuccess(new CallableVoid()
       {
         @Override
-        public Void call() throws Exception
+        public void call() throws Exception
         {
           LDAPReplicationDomain doToco = LDAPReplicationDomain.retrievesReplicationDomain(baseDN);
           assertNotNull(doToco);
           assertTrue(doToco.isConnected(), "not connected");
           debugInfo("ReplicationDomain: Import/Export is running ? " + doToco.ieRunning());
-          return null;
         }
       });
     }
@@ -434,13 +433,12 @@
           .maxSleep(10, SECONDS)
           .sleepTimes(100, MILLISECONDS)
           .toTimer();
-        timer.repeatUntilSuccess(new Callable<Void>()
+        timer.repeatUntilSuccess(new CallableVoid()
         {
           @Override
-          public Void call() throws Exception
+          public void call() throws Exception
           {
             assertNull(LDAPReplicationDomain.retrievesReplicationDomain(baseDN));
-            return null;
           }
         });
       }
@@ -858,15 +856,14 @@
       .maxSleep(timeout, MILLISECONDS)
       .sleepTimes(50, MILLISECONDS)
       .toTimer();
-    timer.repeatUntilSuccess(new Callable<Void>()
+    timer.repeatUntilSuccess(new CallableVoid()
     {
       @Override
-      public Void call() throws Exception
+      public void call() throws Exception
       {
         LDAPReplicationDomain domain = MultimasterReplication.findDomain(baseDN, null);
         assertNotNull(domain);
         assertTrue(domain.isConnected(), "server should have been connected to replication domain" + baseDN);
-        return null;
       }
     });
   }
@@ -996,13 +993,12 @@
       .maxSleep(10, SECONDS)
       .sleepTimes(100, MILLISECONDS)
       .toTimer();
-    timer.repeatUntilSuccess(new Callable<Void>()
+    timer.repeatUntilSuccess(new CallableVoid()
     {
       @Override
-      public Void call() throws Exception
+      public void call() throws Exception
       {
         assertGenIdEquals(expectedGenId);
-        return null;
       }
     });
   }
diff --git a/opendj-server-legacy/src/test/java/org/opends/server/replication/ReplicationTestCase.java b/opendj-server-legacy/src/test/java/org/opends/server/replication/ReplicationTestCase.java
index 8472094..f8579e8 100644
--- a/opendj-server-legacy/src/test/java/org/opends/server/replication/ReplicationTestCase.java
+++ b/opendj-server-legacy/src/test/java/org/opends/server/replication/ReplicationTestCase.java
@@ -75,6 +75,7 @@
 import org.opends.server.types.Modification;
 import org.opends.server.types.SearchResultEntry;
 import org.opends.server.util.TestTimer;
+import org.opends.server.util.TestTimer.CallableVoid;
 import org.testng.annotations.AfterClass;
 import org.testng.annotations.BeforeClass;
 import org.testng.annotations.Test;
@@ -244,20 +245,19 @@
       .maxSleep(secTimeout, SECONDS)
       .sleepTimes(1, SECONDS)
       .toTimer();
-    timer.repeatUntilSuccess(new Callable<Void>()
+    timer.repeatUntilSuccess(new CallableVoid()
     {
       @Override
-      public Void call() throws Exception
+      public void call() throws Exception
       {
         if (rb.isConnected())
         {
           logger.trace("checkConnection: connection of broker " + rb.getServerId()
               + " to RS " + rb.getRsGroupId() + " obtained.");
-          return null;
+          return;
         }
 
         rb.start();
-        return null;
       }
     });
   }
@@ -517,10 +517,10 @@
       .maxSleep(timeoutInSecs, SECONDS)
       .sleepTimes(100, MILLISECONDS)
       .toTimer();
-    timer.repeatUntilSuccess(new Callable<Void>()
+    timer.repeatUntilSuccess(new CallableVoid()
     {
       @Override
-      public Void call() throws Exception
+      public void call() throws Exception
       {
         final Entry newEntry = DirectoryServer.getEntry(dn);
         assertNotNull(newEntry);
@@ -529,7 +529,6 @@
         Attribute attr = attrs.iterator().next();
         boolean foundAttributeValue = attr.contains(ByteString.valueOfUtf8(valueString));
         assertEquals(foundAttributeValue, expectedAttributeValueFound, foundMsg);
-        return null;
       }
     });
   }
@@ -544,13 +543,12 @@
       .maxSleep(timeoutInMillis, MILLISECONDS)
       .sleepTimes(200, MILLISECONDS)
       .toTimer();
-    timer.repeatUntilSuccess(new Callable<Void>()
+    timer.repeatUntilSuccess(new CallableVoid()
     {
       @Override
-      public Void call() throws Exception
+      public void call() throws Exception
       {
         assertEquals(DirectoryServer.entryExists(dn), exist, "Expected entry with dn \"" + dn + "\" would exist");
-        return null;
       }
     });
 
diff --git a/opendj-server-legacy/src/test/java/org/opends/server/replication/SchemaReplicationTest.java b/opendj-server-legacy/src/test/java/org/opends/server/replication/SchemaReplicationTest.java
index becfe3c..bf3d2ef 100644
--- a/opendj-server-legacy/src/test/java/org/opends/server/replication/SchemaReplicationTest.java
+++ b/opendj-server-legacy/src/test/java/org/opends/server/replication/SchemaReplicationTest.java
@@ -23,11 +23,11 @@
 import java.net.SocketTimeoutException;
 import java.util.ArrayList;
 import java.util.List;
-import java.util.concurrent.Callable;
 
 import org.assertj.core.api.Assertions;
 import org.forgerock.i18n.LocalizableMessage;
 import org.forgerock.i18n.slf4j.LocalizedLogger;
+import org.forgerock.opendj.ldap.DN;
 import org.forgerock.opendj.ldap.ResultCode;
 import org.opends.server.TestCaseUtils;
 import org.opends.server.api.SynchronizationProvider;
@@ -41,11 +41,11 @@
 import org.opends.server.replication.service.ReplicationBroker;
 import org.opends.server.types.Attribute;
 import org.opends.server.types.Attributes;
-import org.forgerock.opendj.ldap.DN;
 import org.opends.server.types.Modification;
 import org.opends.server.types.Operation;
 import org.opends.server.types.RawModification;
 import org.opends.server.util.TestTimer;
+import org.opends.server.util.TestTimer.CallableVoid;
 import org.testng.annotations.BeforeClass;
 import org.testng.annotations.Test;
 
@@ -229,15 +229,14 @@
         .maxSleep(5, SECONDS)
         .sleepTimes(100, MILLISECONDS)
         .toTimer();
-      timer.repeatUntilSuccess(new Callable<Void>()
+      timer.repeatUntilSuccess(new CallableVoid()
       {
         @Override
-        public Void call() throws Exception
+        public void call() throws Exception
         {
           String fileStr = readAsString(schemaFile);
           assertTrue(fileStr.contains(stateStr), "The Schema persistentState (CSN:" + stateStr
               + ") has not been saved to " + schemaFile + " : " + fileStr);
-          return null;
         }
       });
     } finally
diff --git a/opendj-server-legacy/src/test/java/org/opends/server/replication/UpdateOperationTest.java b/opendj-server-legacy/src/test/java/org/opends/server/replication/UpdateOperationTest.java
index 9bd57ea..043acf6 100644
--- a/opendj-server-legacy/src/test/java/org/opends/server/replication/UpdateOperationTest.java
+++ b/opendj-server-legacy/src/test/java/org/opends/server/replication/UpdateOperationTest.java
@@ -30,7 +30,6 @@
 import java.net.SocketTimeoutException;
 import java.util.ArrayList;
 import java.util.List;
-import java.util.concurrent.Callable;
 
 import org.assertj.core.api.Assertions;
 import org.forgerock.i18n.LocalizableMessage;
@@ -66,6 +65,7 @@
 import org.opends.server.types.Operation;
 import org.opends.server.types.OperationType;
 import org.opends.server.util.TestTimer;
+import org.opends.server.util.TestTimer.CallableVoid;
 import org.opends.server.util.TimeThread;
 import org.testng.annotations.BeforeClass;
 import org.testng.annotations.DataProvider;
@@ -1047,13 +1047,12 @@
       .maxSleep(200, SECONDS)
       .sleepTimes(100, MILLISECONDS)
       .toTimer();
-    timer.repeatUntilSuccess(new Callable<Void>()
+    timer.repeatUntilSuccess(new CallableVoid()
     {
       @Override
-      public Void call() throws Exception
+      public void call() throws Exception
       {
         assertNotEquals(getMonitorDelta() , 0);
-        return null;
       }
     });
   }
@@ -1326,13 +1325,12 @@
           .maxSleep(5, SECONDS)
           .sleepTimes(100, MILLISECONDS)
           .toTimer();
-        timer.repeatUntilSuccess(new Callable<Void>()
+        timer.repeatUntilSuccess(new CallableVoid()
         {
           @Override
-          public Void call() throws Exception
+          public void call() throws Exception
           {
             assertNotEquals(getMonitorAttrValue(baseDN, "replayed-updates"), initialCount);
-            return null;
           }
         });
       }
diff --git a/opendj-server-legacy/src/test/java/org/opends/server/replication/plugin/HistoricalTest.java b/opendj-server-legacy/src/test/java/org/opends/server/replication/plugin/HistoricalTest.java
index 28671bc..3559af8 100644
--- a/opendj-server-legacy/src/test/java/org/opends/server/replication/plugin/HistoricalTest.java
+++ b/opendj-server-legacy/src/test/java/org/opends/server/replication/plugin/HistoricalTest.java
@@ -28,7 +28,6 @@
 
 import java.util.List;
 import java.util.UUID;
-import java.util.concurrent.Callable;
 
 import org.assertj.core.api.Assertions;
 import org.forgerock.opendj.ldap.AttributeDescription;
@@ -52,6 +51,7 @@
 import org.opends.server.types.Modification;
 import org.opends.server.types.Operation;
 import org.opends.server.util.TestTimer;
+import org.opends.server.util.TestTimer.CallableVoid;
 import org.testng.annotations.BeforeClass;
 import org.testng.annotations.Test;
 
@@ -319,13 +319,12 @@
       .maxSleep(2, SECONDS)
       .sleepTimes(100, MILLISECONDS)
       .toTimer();
-    timer.repeatUntilSuccess(new Callable<Void>()
+    timer.repeatUntilSuccess(new CallableVoid()
     {
       @Override
-      public Void call() throws Exception
+      public void call() throws Exception
       {
         assertEquals(getEntryValue(entryDN, attrDesc), expectedValue);
-        return null;
       }
     });
   }
diff --git a/opendj-server-legacy/src/test/java/org/opends/server/replication/plugin/StateMachineTest.java b/opendj-server-legacy/src/test/java/org/opends/server/replication/plugin/StateMachineTest.java
index 76bcb13..c899e16 100644
--- a/opendj-server-legacy/src/test/java/org/opends/server/replication/plugin/StateMachineTest.java
+++ b/opendj-server-legacy/src/test/java/org/opends/server/replication/plugin/StateMachineTest.java
@@ -26,7 +26,6 @@
 import java.net.SocketTimeoutException;
 import java.util.SortedSet;
 import java.util.TreeSet;
-import java.util.concurrent.Callable;
 import java.util.concurrent.atomic.AtomicBoolean;
 
 import org.forgerock.i18n.LocalizableMessage;
@@ -56,6 +55,7 @@
 import org.opends.server.replication.service.ReplicationBroker;
 import org.opends.server.types.Entry;
 import org.opends.server.util.TestTimer;
+import org.opends.server.util.TestTimer.CallableVoid;
 import org.testng.annotations.AfterClass;
 import org.testng.annotations.BeforeClass;
 import org.testng.annotations.DataProvider;
@@ -143,13 +143,12 @@
       .maxSleep(30, SECONDS)
       .sleepTimes(100, MILLISECONDS)
       .toTimer();
-    timer.repeatUntilSuccess(new Callable<Void>()
+    timer.repeatUntilSuccess(new CallableVoid()
     {
       @Override
-      public Void call() throws Exception
+      public void call() throws Exception
       {
         assertTrue(isConnected(dsId), "checkConnection: DS " + dsId + " is not connected to the RS");
-        return null;
       }
     });
   }
@@ -1130,13 +1129,12 @@
       .maxSleep(30, SECONDS)
       .sleepTimes(500, MILLISECONDS)
       .toTimer();
-    timer.repeatUntilSuccess(new Callable<Void>()
+    timer.repeatUntilSuccess(new CallableVoid()
     {
       @Override
-      public Void call() throws Exception
+      public void call() throws Exception
       {
         assertEquals(domain.getStatus(), expectedStatus);
-        return null;
       }
     });
   }
diff --git a/opendj-server-legacy/src/test/java/org/opends/server/util/TestTimer.java b/opendj-server-legacy/src/test/java/org/opends/server/util/TestTimer.java
index 3698297..ceeaa39 100644
--- a/opendj-server-legacy/src/test/java/org/opends/server/util/TestTimer.java
+++ b/opendj-server-legacy/src/test/java/org/opends/server/util/TestTimer.java
@@ -11,7 +11,7 @@
  * Header, with the fields enclosed by brackets [] replaced by your own identifying
  * information: "Portions Copyright [year] [name of copyright owner]".
  *
- * Copyright 2015 ForgeRock AS.
+ * Copyright 2015-2016 ForgeRock AS.
  */
 package org.opends.server.util;
 
@@ -27,10 +27,25 @@
 public interface TestTimer
 {
   /**
-   * Constant that can be used at the end of {@code Callable<Void>.call()} to better explicit this
-   * is the end of the method.
+   * Equivalent to {@code Callable<Void>} or a {@code Runnable} that can throw exception.
+   * <p>
+   * <b>Note:</b> The name has been designed to stay close to {@code Callable<Void>} to allow to
+   * easily change test code back and forth between {@code java.util.Callable} and
+   * {@code CallableVoid} while writing the test code. It also avoids name collisions while writing
+   * the rest of the OpenDJ code base.
    */
-  Void END_RUN = null;
+  public static interface CallableVoid
+  {
+    /**
+     * Equivalent of {@link Runnable#run()} or {@link Callable#call()}.
+     *
+     * @throws Exception
+     *           if an error occurred
+     * @see {@link Runnable#run()}
+     * @see {@link Callable#call()}
+     */
+    void call() throws Exception;
+  }
 
   /**
    * Repeatedly call the supplied callable (respecting a sleep interval) until:
@@ -46,7 +61,7 @@
    * </ul>
    * <p>
    * Note: The test code in the callable can be written as any test code outside a callable. In
-   * particular, asserts can and should be used inside the {@link Callable#call()}.
+   * particular, asserts can and should be used inside the {@link Callable#call()} method.
    *
    * @param callable
    *          the callable to repeat until success
@@ -61,6 +76,31 @@
    */
   <R> R repeatUntilSuccess(Callable<R> callable) throws Exception, InterruptedException;
 
+  /**
+   * Repeatedly call the supplied callable (respecting a sleep interval) until:
+   * <ul>
+   * <li>it returns,</li>
+   * <li>it throws an exception other than {@link AssertionError},</li>
+   * <li>the current timer times out.</li>
+   * </ul>
+   * If the current timer times out, then it will:
+   * <ul>
+   * <li>either rethrow an {@link AssertionError} thrown by the callable,</li>
+   * <li>or return {@code null}.</li>
+   * </ul>
+   * <p>
+   * Note: The test code in the callable can be written as any test code outside a callable. In
+   * particular, asserts can and should be used inside the {@link TestTimer.Callable#call()} method.
+   *
+   * @param callable
+   *          the callable to repeat until success
+   * @throws Exception
+   *           The exception thrown by the provided callable
+   * @throws InterruptedException
+   *           If the thread is interrupted while sleeping
+   */
+  void repeatUntilSuccess(CallableVoid callable) throws Exception, InterruptedException;
+
   /** Builder for a {@link TestTimer}. */
   public static final class Builder
   {
@@ -173,6 +213,20 @@
     }
 
     @Override
+    public void repeatUntilSuccess(final CallableVoid callable) throws Exception, InterruptedException
+    {
+      repeatUntilSuccess(new Callable<Void>()
+      {
+        @Override
+        public Void call() throws Exception
+        {
+          callable.call();
+          return null;
+        }
+      });
+    }
+
+    @Override
     public String toString()
     {
       return totalNbSteps * sleepTime + " ms max sleep time"

--
Gitblit v1.10.0