Use guava-testlib's FakeTicker 59/53159/3
authorRobert Varga <rovarga@cisco.com>
Fri, 10 Mar 2017 16:53:20 +0000 (17:53 +0100)
committerTom Pantelis <tpanteli@brocade.com>
Mon, 13 Mar 2017 02:39:11 +0000 (02:39 +0000)
Rather than rolling our own, use the Ticker class available
in guava-testlib.

Change-Id: If3d1abec6aad20b439e19fbd48d961b7582aa607
Signed-off-by: Robert Varga <rovarga@cisco.com>
opendaylight/md-sal/cds-access-client/pom.xml
opendaylight/md-sal/cds-access-client/src/test/java/org/opendaylight/controller/cluster/access/client/AbstractClientActorTest.java
opendaylight/md-sal/cds-access-client/src/test/java/org/opendaylight/controller/cluster/access/client/ConnectingClientConnectionTest.java
opendaylight/md-sal/cds-access-client/src/test/java/org/opendaylight/controller/cluster/access/client/ConnectionEntryTest.java
opendaylight/md-sal/sal-clustering-commons/pom.xml
opendaylight/md-sal/sal-clustering-commons/src/test/java/org/opendaylight/controller/cluster/common/actor/MessageTrackerTest.java
opendaylight/md-sal/sal-clustering-commons/src/test/java/org/opendaylight/controller/cluster/common/actor/TestTicker.java [deleted file]

index 10f8fe7..7ba9f54 100644 (file)
             <artifactId>akka-testkit_${scala.version}</artifactId>
             <scope>test</scope>
         </dependency>
+        <dependency>
+           <groupId>com.google.guava</groupId>
+           <artifactId>guava-testlib</artifactId>
+        </dependency>
         <dependency>
           <groupId>org.opendaylight.controller</groupId>
           <artifactId>sal-clustering-commons</artifactId>
index d9e95b6..889bd58 100644 (file)
@@ -10,6 +10,7 @@ package org.opendaylight.controller.cluster.access.client;
 import static org.mockito.Mockito.doReturn;
 
 import akka.actor.ActorRef;
+import com.google.common.testing.FakeTicker;
 import org.junit.Before;
 import org.mockito.Mock;
 import org.mockito.MockitoAnnotations;
@@ -17,7 +18,6 @@ import org.opendaylight.controller.cluster.access.concepts.ClientIdentifier;
 import org.opendaylight.controller.cluster.access.concepts.FrontendIdentifier;
 import org.opendaylight.controller.cluster.access.concepts.FrontendType;
 import org.opendaylight.controller.cluster.access.concepts.MemberName;
-import org.opendaylight.controller.cluster.common.actor.TestTicker;
 
 /**
  * Abstract base class for client actors and their components.
@@ -32,7 +32,7 @@ public abstract class AbstractClientActorTest {
     @Mock
     private ActorRef mockSelf;
 
-    protected final TestTicker ticker = new TestTicker();
+    protected final FakeTicker ticker = new FakeTicker();
 
     @Before
     public void setup() {
index 2d5b696..8274d24 100644 (file)
@@ -22,6 +22,7 @@ import static org.mockito.Mockito.verifyNoMoreInteractions;
 import akka.actor.ActorRef;
 import akka.actor.ActorSystem;
 import akka.testkit.TestProbe;
+import com.google.common.testing.FakeTicker;
 import java.util.Optional;
 import java.util.concurrent.ThreadLocalRandom;
 import java.util.concurrent.TimeUnit;
@@ -43,7 +44,6 @@ import org.opendaylight.controller.cluster.access.concepts.RequestEnvelope;
 import org.opendaylight.controller.cluster.access.concepts.RequestException;
 import org.opendaylight.controller.cluster.access.concepts.RequestFailure;
 import org.opendaylight.controller.cluster.access.concepts.Response;
-import org.opendaylight.controller.cluster.common.actor.TestTicker;
 import org.opendaylight.yangtools.concepts.WritableIdentifier;
 import scala.concurrent.duration.FiniteDuration;
 
@@ -109,7 +109,7 @@ public class ConnectingClientConnectionTest {
     @Mock
     private ClientActorContext mockContext;
 
-    private TestTicker ticker;
+    private FakeTicker ticker;
     private BackendInfo mockBackendInfo;
     private MockRequest mockRequest;
     private MockRequest mockRequest2;
@@ -138,8 +138,8 @@ public class ConnectingClientConnectionTest {
 
         doNothing().when(mockCallback).accept(any(MockFailure.class));
 
-        ticker = new TestTicker();
-        ticker.increment(ThreadLocalRandom.current().nextLong());
+        ticker = new FakeTicker();
+        ticker.advance(ThreadLocalRandom.current().nextLong());
         doReturn(ticker).when(mockContext).ticker();
 
         mockActor = TestProbe.apply(actorSystem);
@@ -232,7 +232,7 @@ public class ConnectingClientConnectionTest {
     public void testRunTimeoutWithTimeoutLess() throws NoProgressException {
         queue.sendRequest(mockRequest, mockCallback);
 
-        ticker.increment(AbstractClientConnection.REQUEST_TIMEOUT_NANOS - 1);
+        ticker.advance(AbstractClientConnection.REQUEST_TIMEOUT_NANOS - 1);
 
         Optional<FiniteDuration> ret = queue.checkTimeout(ticker.read());
         assertNotNull(ret);
@@ -245,7 +245,7 @@ public class ConnectingClientConnectionTest {
 
         queue.sendRequest(mockRequest, mockCallback);
 
-        ticker.increment(AbstractClientConnection.REQUEST_TIMEOUT_NANOS);
+        ticker.advance(AbstractClientConnection.REQUEST_TIMEOUT_NANOS);
 
         Optional<FiniteDuration> ret = queue.checkTimeout(ticker.read());
         assertNull(ret);
@@ -257,7 +257,7 @@ public class ConnectingClientConnectionTest {
 
         queue.sendRequest(mockRequest, mockCallback);
 
-        ticker.increment(AbstractClientConnection.REQUEST_TIMEOUT_NANOS + 1);
+        ticker.advance(AbstractClientConnection.REQUEST_TIMEOUT_NANOS + 1);
 
         Optional<FiniteDuration> ret = queue.checkTimeout(ticker.read());
         assertNull(ret);
@@ -267,7 +267,7 @@ public class ConnectingClientConnectionTest {
     public void testRunTimeoutWithoutProgressExact() throws NoProgressException {
         queue.sendRequest(mockRequest, mockCallback);
 
-        ticker.increment(AbstractClientConnection.NO_PROGRESS_TIMEOUT_NANOS);
+        ticker.advance(AbstractClientConnection.NO_PROGRESS_TIMEOUT_NANOS);
 
         // Kaboom
         queue.runTimer((ClientActorBehavior) mockBehavior);
@@ -278,7 +278,7 @@ public class ConnectingClientConnectionTest {
     public void testRunTimeoutWithoutProgressMore() throws NoProgressException {
         queue.sendRequest(mockRequest, mockCallback);
 
-        ticker.increment(AbstractClientConnection.NO_PROGRESS_TIMEOUT_NANOS + 1);
+        ticker.advance(AbstractClientConnection.NO_PROGRESS_TIMEOUT_NANOS + 1);
 
         // Kaboom
         queue.runTimer((ClientActorBehavior) mockBehavior);
@@ -287,7 +287,7 @@ public class ConnectingClientConnectionTest {
 
     @Test
     public void testRunTimeoutEmptyWithoutProgressExact() throws NoProgressException {
-        ticker.increment(AbstractClientConnection.NO_PROGRESS_TIMEOUT_NANOS);
+        ticker.advance(AbstractClientConnection.NO_PROGRESS_TIMEOUT_NANOS);
 
         // No problem
         Optional<FiniteDuration> ret = queue.checkTimeout(ticker.read());
@@ -297,7 +297,7 @@ public class ConnectingClientConnectionTest {
 
     @Test
     public void testRunTimeoutEmptyWithoutProgressMore() throws NoProgressException {
-        ticker.increment(AbstractClientConnection.NO_PROGRESS_TIMEOUT_NANOS + 1);
+        ticker.advance(AbstractClientConnection.NO_PROGRESS_TIMEOUT_NANOS + 1);
 
         // No problem
         Optional<FiniteDuration> ret = queue.checkTimeout(ticker.read());
@@ -342,11 +342,11 @@ public class ConnectingClientConnectionTest {
 
         queue.sendRequest(mockRequest, mockCallback);
 
-        ticker.increment(10);
+        ticker.advance(10);
         queue.sendRequest(mockRequest2, mockCallback);
         queue.receiveResponse(mockResponseEnvelope);
 
-        ticker.increment(AbstractClientConnection.NO_PROGRESS_TIMEOUT_NANOS - 11);
+        ticker.advance(AbstractClientConnection.NO_PROGRESS_TIMEOUT_NANOS - 11);
 
         Optional<FiniteDuration> ret = queue.checkTimeout(ticker.read());
         assertNull(ret);
index a772ddc..a76a369 100644 (file)
@@ -14,6 +14,7 @@ import static org.mockito.Mockito.verify;
 import akka.actor.ActorRef;
 import akka.actor.ActorSystem;
 import akka.testkit.TestProbe;
+import com.google.common.testing.FakeTicker;
 import java.util.concurrent.ThreadLocalRandom;
 import java.util.function.Consumer;
 import org.junit.After;
@@ -30,7 +31,6 @@ import org.opendaylight.controller.cluster.access.concepts.Request;
 import org.opendaylight.controller.cluster.access.concepts.RequestException;
 import org.opendaylight.controller.cluster.access.concepts.RequestFailure;
 import org.opendaylight.controller.cluster.access.concepts.Response;
-import org.opendaylight.controller.cluster.common.actor.TestTicker;
 import org.opendaylight.yangtools.concepts.WritableIdentifier;
 
 /**
@@ -92,7 +92,7 @@ public class ConnectionEntryTest {
     @Mock
     private ClientActorBehavior<?> mockBehavior;
 
-    private TestTicker ticker;
+    private FakeTicker ticker;
     private Request<WritableIdentifier, ?> mockRequest;
     private Response<WritableIdentifier, ?> mockResponse;
 
@@ -117,8 +117,8 @@ public class ConnectionEntryTest {
 
         doNothing().when(mockCallback).accept(any(MockFailure.class));
 
-        ticker = new TestTicker();
-        ticker.increment(ThreadLocalRandom.current().nextLong());
+        ticker = new FakeTicker();
+        ticker.advance(ThreadLocalRandom.current().nextLong());
 
         mockActor = TestProbe.apply(actorSystem);
         mockRequest = new MockRequest(mockIdentifier, mockReplyTo);
index 029641b..a7b6b5f 100644 (file)
       <groupId>com.google.protobuf</groupId>
       <artifactId>protobuf-java</artifactId>
     </dependency>
+    <dependency>
+      <groupId>com.google.guava</groupId>
+      <artifactId>guava-testlib</artifactId>
+    </dependency>
 
     <!-- Scala -->
     <dependency>
index 896fd3c..3bead64 100644 (file)
@@ -12,6 +12,7 @@ import static java.util.concurrent.TimeUnit.MILLISECONDS;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.fail;
 
+import com.google.common.testing.FakeTicker;
 import java.util.List;
 import org.junit.Assert;
 import org.junit.Before;
@@ -26,12 +27,12 @@ public class MessageTrackerTest {
 
     private static final Logger LOG = LoggerFactory.getLogger(MessageTrackerTest.class);
 
-    private TestTicker ticker;
+    private FakeTicker ticker;
     private MessageTracker messageTracker;
 
     @Before
     public void setup() {
-        ticker = new TestTicker();
+        ticker = new FakeTicker();
         messageTracker = new MessageTracker(Foo.class, 10, ticker);
     }
 
@@ -40,7 +41,7 @@ public class MessageTrackerTest {
         MessageTracker.Context context1 = messageTracker.received(new Foo());
         context1.close();
 
-        ticker.increment(MILLISECONDS.toNanos(20));
+        ticker.advance(20, MILLISECONDS);
         MessageTracker.Context context2 = messageTracker.received(new Foo());
         context2.close();
     }
@@ -52,7 +53,7 @@ public class MessageTrackerTest {
         MessageTracker.Context context1 = messageTracker.received(new Foo());
         context1.close();
 
-        ticker.increment(MILLISECONDS.toNanos(20));
+        ticker.advance(20, MILLISECONDS);
 
         MessageTracker.Context context2 = messageTracker.received(new Foo());
         Assert.assertEquals(true, context2.error().isPresent());
@@ -71,7 +72,7 @@ public class MessageTrackerTest {
         messageTracker.received(10L).close();
         MessageTracker.Context context = messageTracker.received(100);
 
-        ticker.increment(MILLISECONDS.toNanos(20));
+        ticker.advance(20, MILLISECONDS);
 
         context.close();
 
@@ -96,7 +97,6 @@ public class MessageTrackerTest {
         LOG.error("An error occurred : {}" , error);
     }
 
-
     @Test
     public void testMetExpectationOnTracking() {
         messageTracker.begin();
@@ -104,7 +104,7 @@ public class MessageTrackerTest {
         MessageTracker.Context context1 = messageTracker.received(new Foo());
         context1.close();
 
-        ticker.increment(MILLISECONDS.toNanos(1));
+        ticker.advance(1, MILLISECONDS);
 
         MessageTracker.Context context2 = messageTracker.received(new Foo());
         Assert.assertEquals(false, context2.error().isPresent());
@@ -135,7 +135,7 @@ public class MessageTrackerTest {
     public void testDelayInFirstExpectedMessageArrival() {
         messageTracker.begin();
 
-        ticker.increment(MILLISECONDS.toNanos(20));
+        ticker.advance(20, MILLISECONDS);
 
         MessageTracker.Context context = messageTracker.received(new Foo());
 
@@ -156,7 +156,7 @@ public class MessageTrackerTest {
     public void testCallingBeginDoesNotResetWatch() {
         messageTracker.begin();
 
-        ticker.increment(MILLISECONDS.toNanos(20));
+        ticker.advance(20, MILLISECONDS);
 
         messageTracker.begin();
 
diff --git a/opendaylight/md-sal/sal-clustering-commons/src/test/java/org/opendaylight/controller/cluster/common/actor/TestTicker.java b/opendaylight/md-sal/sal-clustering-commons/src/test/java/org/opendaylight/controller/cluster/common/actor/TestTicker.java
deleted file mode 100644 (file)
index 3387eca..0000000
+++ /dev/null
@@ -1,29 +0,0 @@
-/*
- * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
- *
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License v1.0 which accompanies this distribution,
- * and is available at http://www.eclipse.org/legal/epl-v10.html
- */
-package org.opendaylight.controller.cluster.common.actor;
-
-import com.google.common.base.Ticker;
-
-/**
- * Utility {@link Ticker} where passage of time is explicitly controlled via {@link #increment(long)}.
- *
- * @author Robert Varga
- */
-public final class TestTicker extends Ticker {
-    private long counter = 0;
-
-    @Override
-    public long read() {
-        return counter;
-    }
-
-    public long increment(final long ticks) {
-        counter += ticks;
-        return counter;
-    }
-}