Use OptionalLong in AbstractClientConnection 05/83005/5
authorRobert Varga <robert.varga@pantheon.tech>
Wed, 10 Jul 2019 14:02:30 +0000 (16:02 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Wed, 10 Jul 2019 15:37:15 +0000 (17:37 +0200)
This removes the need for boxing longs.

Change-Id: I73a6c0be7f9a662f9e1df884f46adbc0fc121fb1
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
opendaylight/md-sal/cds-access-client/src/main/java/org/opendaylight/controller/cluster/access/client/AbstractClientConnection.java
opendaylight/md-sal/cds-access-client/src/test/java/org/opendaylight/controller/cluster/access/client/AbstractClientConnectionTest.java
opendaylight/md-sal/cds-access-client/src/test/java/org/opendaylight/controller/cluster/access/client/ConnectedClientConnectionTest.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/ReconnectingClientConnectionTest.java

index 0d45dd5..9c290df 100644 (file)
@@ -17,6 +17,7 @@ import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
 import java.util.Collection;
 import java.util.List;
 import java.util.Optional;
+import java.util.OptionalLong;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.TimeoutException;
 import java.util.concurrent.locks.Lock;
@@ -334,7 +335,7 @@ public abstract class AbstractClientConnection<T extends BackendInfo> {
                 // Requests are always scheduled in sequence, hence checking for timeout is relatively straightforward.
                 // Note we use also inquire about the delay, so we can re-schedule if needed, hence the unusual
                 // tri-state return convention.
-                final Optional<Long> delay = lockedCheckTimeout(now);
+                final OptionalLong delay = lockedCheckTimeout(now);
                 if (delay == null) {
                     // We have timed out. There is no point in scheduling a timer
                     LOG.debug("{}: connection {} timed out", context.persistenceId(), this);
@@ -344,7 +345,7 @@ public abstract class AbstractClientConnection<T extends BackendInfo> {
 
                 if (delay.isPresent()) {
                     // If there is new delay, schedule a timer
-                    scheduleTimer(delay.get());
+                    scheduleTimer(delay.getAsLong());
                 } else {
                     LOG.debug("{}: not scheduling timeout on {}", context.persistenceId(), this);
                 }
@@ -366,7 +367,7 @@ public abstract class AbstractClientConnection<T extends BackendInfo> {
     }
 
     @VisibleForTesting
-    final Optional<Long> checkTimeout(final long now) {
+    final OptionalLong checkTimeout(final long now) {
         lock.lock();
         try {
             return lockedCheckTimeout(now);
@@ -388,10 +389,10 @@ public abstract class AbstractClientConnection<T extends BackendInfo> {
     @SuppressFBWarnings(value = "NP_OPTIONAL_RETURN_NULL",
             justification = "Returning null Optional is documented in the API contract.")
     @GuardedBy("lock")
-    private Optional<Long> lockedCheckTimeout(final long now) {
+    private OptionalLong lockedCheckTimeout(final long now) {
         if (queue.isEmpty()) {
             LOG.debug("{}: connection {} is empty", context.persistenceId(), this);
-            return Optional.empty();
+            return OptionalLong.empty();
         }
 
         final long backendSilentTicks = backendSilentTicks(now);
@@ -406,7 +407,7 @@ public abstract class AbstractClientConnection<T extends BackendInfo> {
             final long beenOpen = now - head.getEnqueuedTicks();
             final long requestTimeout = context.config().getRequestTimeout();
             if (beenOpen < requestTimeout) {
-                return Optional.of(requestTimeout - beenOpen);
+                return OptionalLong.of(requestTimeout - beenOpen);
             }
 
             tasksTimedOut++;
@@ -421,7 +422,7 @@ public abstract class AbstractClientConnection<T extends BackendInfo> {
             queue.tryTransmit(now);
         }
 
-        return Optional.empty();
+        return OptionalLong.empty();
     }
 
     private void timeoutEntry(final ConnectionEntry entry, final long beenOpen) {
index 59930ca..1d38b62 100644 (file)
@@ -8,7 +8,11 @@
 package org.opendaylight.controller.cluster.access.client;
 
 import static org.hamcrest.CoreMatchers.hasItems;
-import static org.mockito.Matchers.isA;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertSame;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.ArgumentMatchers.isA;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.timeout;
 import static org.mockito.Mockito.verify;
@@ -19,10 +23,9 @@ import akka.actor.ActorSystem;
 import akka.testkit.TestProbe;
 import akka.testkit.javadsl.TestKit;
 import com.google.common.collect.Iterables;
-import java.util.Optional;
+import java.util.OptionalLong;
 import java.util.function.Consumer;
 import org.junit.After;
-import org.junit.Assert;
 import org.junit.Before;
 import org.junit.Test;
 import org.mockito.MockitoAnnotations;
@@ -75,7 +78,7 @@ public abstract class AbstractClientConnectionTest<T extends AbstractClientConne
 
     @Test
     public void testLocalActor() {
-        Assert.assertEquals(contextProbe.ref(), connection.localActor());
+        assertEquals(contextProbe.ref(), connection.localActor());
     }
 
     @Test
@@ -97,7 +100,7 @@ public abstract class AbstractClientConnectionTest<T extends AbstractClientConne
         final Request<?, ?> request = createRequest(replyToProbe.ref());
         connection.sendRequest(request, callback);
         final RequestEnvelope requestEnvelope = backendProbe.expectMsgClass(RequestEnvelope.class);
-        Assert.assertEquals(request, requestEnvelope.getMessage());
+        assertEquals(request, requestEnvelope.getMessage());
         final LocalHistoryIdentifier historyId = new LocalHistoryIdentifier(CLIENT_ID, 0L);
         final RequestSuccess<?, ?> message = new TransactionAbortSuccess(new TransactionIdentifier(historyId, 0L), 0L);
         final ResponseEnvelope<?> envelope = new SuccessEnvelope(message, 0L, 0L, 0L);
@@ -108,13 +111,12 @@ public abstract class AbstractClientConnectionTest<T extends AbstractClientConne
     @Test
     public void testRun() {
         final ClientActorBehavior<U> behavior = mock(ClientActorBehavior.class);
-        Assert.assertSame(behavior, connection.runTimer(behavior));
+        assertSame(behavior, connection.runTimer(behavior));
     }
 
     @Test
     public void testCheckTimeoutEmptyQueue() {
-        final Optional<Long> timeout = connection.checkTimeout(context.ticker().read());
-        Assert.assertFalse(timeout.isPresent());
+        assertEquals(OptionalLong.empty(), connection.checkTimeout(context.ticker().read()));
     }
 
     @Test
@@ -122,8 +124,8 @@ public abstract class AbstractClientConnectionTest<T extends AbstractClientConne
         final Consumer<Response<?, ?>> callback = mock(Consumer.class);
         connection.sendRequest(createRequest(replyToProbe.ref()), callback);
         final long now = context.ticker().read();
-        final Optional<Long> timeout = connection.checkTimeout(now);
-        Assert.assertTrue(timeout.isPresent());
+        final OptionalLong timeout = connection.checkTimeout(now);
+        assertTrue(timeout.isPresent());
     }
 
     @Test
@@ -134,8 +136,8 @@ public abstract class AbstractClientConnectionTest<T extends AbstractClientConne
         connection.sendRequest(request1, callback);
         connection.sendRequest(request2, callback);
         final Iterable<ConnectionEntry> entries = connection.startReplay();
-        Assert.assertThat(entries, hasItems(entryWithRequest(request1), entryWithRequest(request2)));
-        Assert.assertEquals(2, Iterables.size(entries));
+        assertThat(entries, hasItems(entryWithRequest(request1), entryWithRequest(request2)));
+        assertEquals(2, Iterables.size(entries));
         Iterables.removeIf(entries, e -> true);
         final ReconnectForwarder forwarder = mock(ReconnectForwarder.class);
         connection.finishReplay(forwarder);
index 530ac5a..ab80513 100644 (file)
@@ -7,15 +7,14 @@
  */
 package org.opendaylight.controller.cluster.access.client;
 
+import static org.junit.Assert.assertNull;
 import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.ArgumentMatchers.same;
 import static org.mockito.Mockito.doReturn;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.verify;
 
-import java.util.Optional;
 import java.util.function.Consumer;
-import org.junit.Assert;
 import org.junit.Test;
 import org.opendaylight.controller.cluster.access.ABIVersion;
 import org.opendaylight.controller.cluster.access.commands.ModifyTransactionRequestBuilder;
@@ -38,8 +37,7 @@ public class ConnectedClientConnectionTest
         final Consumer<Response<?, ?>> callback = mock(Consumer.class);
         connection.sendRequest(createRequest(replyToProbe.ref()), callback);
         final long now = context.ticker().read() + ConnectedClientConnection.DEFAULT_BACKEND_ALIVE_TIMEOUT_NANOS;
-        final Optional<Long> timeout = connection.checkTimeout(now);
-        Assert.assertNull(timeout);
+        assertNull(connection.checkTimeout(now));
     }
 
     @Override
@@ -47,7 +45,7 @@ public class ConnectedClientConnectionTest
         final BackendInfo backend = new BackendInfo(backendProbe.ref(), "test", 0L, ABIVersion.BORON, 10);
         final ConnectingClientConnection<BackendInfo> connectingConn = new ConnectingClientConnection<>(context, 0L,
                 backend.getName());
-        return  new ConnectedClientConnection<>(connectingConn, backend);
+        return new ConnectedClientConnection<>(connectingConn, backend);
     }
 
     @Override
index a3c4b5c..d0ffa02 100644 (file)
@@ -24,7 +24,7 @@ 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.OptionalLong;
 import java.util.concurrent.ThreadLocalRandom;
 import java.util.concurrent.TimeUnit;
 import java.util.function.Consumer;
@@ -198,7 +198,7 @@ public class ConnectingClientConnectionTest {
     @Test
     public void testSendRequestNeedsBackend() {
         queue.sendRequest(mockRequest, mockCallback);
-        final Optional<Long> ret = queue.checkTimeout(ticker.read());
+        final OptionalLong ret = queue.checkTimeout(ticker.read());
         assertNotNull(ret);
         assertTrue(ret.isPresent());
     }
@@ -214,7 +214,7 @@ public class ConnectingClientConnectionTest {
         setupBackend();
 
         queue.sendRequest(mockRequest, mockCallback);
-        final Optional<Long> ret = queue.checkTimeout(ticker.read());
+        final OptionalLong ret = queue.checkTimeout(ticker.read());
         assertNotNull(ret);
         assertTrue(ret.isPresent());
         assertTransmit(mockRequest, 0);
@@ -222,7 +222,7 @@ public class ConnectingClientConnectionTest {
 
     @Test
     public void testRunTimeoutEmpty() {
-        Optional<Long> ret = queue.checkTimeout(ticker.read());
+        OptionalLong ret = queue.checkTimeout(ticker.read());
         assertNotNull(ret);
         assertFalse(ret.isPresent());
     }
@@ -230,7 +230,7 @@ public class ConnectingClientConnectionTest {
     @Test
     public void testRunTimeoutWithoutShift() {
         queue.sendRequest(mockRequest, mockCallback);
-        Optional<Long> ret = queue.checkTimeout(ticker.read());
+        OptionalLong ret = queue.checkTimeout(ticker.read());
         assertNotNull(ret);
         assertTrue(ret.isPresent());
     }
@@ -241,7 +241,7 @@ public class ConnectingClientConnectionTest {
 
         ticker.advance(AbstractClientConnection.DEFAULT_BACKEND_ALIVE_TIMEOUT_NANOS - 1);
 
-        Optional<Long> ret = queue.checkTimeout(ticker.read());
+        OptionalLong ret = queue.checkTimeout(ticker.read());
         assertNotNull(ret);
         assertTrue(ret.isPresent());
     }
@@ -254,7 +254,7 @@ public class ConnectingClientConnectionTest {
 
         ticker.advance(AbstractClientConnection.DEFAULT_BACKEND_ALIVE_TIMEOUT_NANOS);
 
-        Optional<Long> ret = queue.checkTimeout(ticker.read());
+        OptionalLong ret = queue.checkTimeout(ticker.read());
         assertNull(ret);
     }
 
@@ -266,8 +266,7 @@ public class ConnectingClientConnectionTest {
 
         ticker.advance(AbstractClientConnection.DEFAULT_BACKEND_ALIVE_TIMEOUT_NANOS + 1);
 
-        Optional<Long> ret = queue.checkTimeout(ticker.read());
-        assertNull(ret);
+        assertNull(queue.checkTimeout(ticker.read()));
     }
 
     @SuppressWarnings({ "rawtypes", "unchecked" })
@@ -297,9 +296,7 @@ public class ConnectingClientConnectionTest {
         ticker.advance(AbstractClientConnection.DEFAULT_NO_PROGRESS_TIMEOUT_NANOS);
 
         // No problem
-        Optional<Long> ret = queue.checkTimeout(ticker.read());
-        assertNotNull(ret);
-        assertFalse(ret.isPresent());
+        assertEquals(OptionalLong.empty(), queue.checkTimeout(ticker.read()));
     }
 
     @Test
@@ -307,9 +304,7 @@ public class ConnectingClientConnectionTest {
         ticker.advance(AbstractClientConnection.DEFAULT_NO_PROGRESS_TIMEOUT_NANOS + 1);
 
         // No problem
-        Optional<Long> ret = queue.checkTimeout(ticker.read());
-        assertNotNull(ret);
-        assertFalse(ret.isPresent());
+        assertEquals(OptionalLong.empty(), queue.checkTimeout(ticker.read()));
     }
 
     @Test
@@ -355,8 +350,7 @@ public class ConnectingClientConnectionTest {
 
         ticker.advance(AbstractClientConnection.DEFAULT_NO_PROGRESS_TIMEOUT_NANOS - 11);
 
-        Optional<Long> ret = queue.checkTimeout(ticker.read());
-        assertNull(ret);
+        assertNull(queue.checkTimeout(ticker.read()));
     }
 
     private void setupBackend() {
index 0b7d0c2..84cfea4 100644 (file)
@@ -7,14 +7,16 @@
  */
 package org.opendaylight.controller.cluster.access.client;
 
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertSame;
+import static org.junit.Assert.assertTrue;
 import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.Mockito.after;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.verify;
 
-import java.util.Optional;
+import java.util.OptionalLong;
 import java.util.function.Consumer;
-import org.junit.Assert;
 import org.junit.Test;
 import org.opendaylight.controller.cluster.access.ABIVersion;
 import org.opendaylight.controller.cluster.access.commands.TransactionAbortSuccess;
@@ -35,9 +37,9 @@ public class ReconnectingClientConnectionTest
         final Consumer<Response<?, ?>> callback = mock(Consumer.class);
         connection.sendRequest(createRequest(replyToProbe.ref()), callback);
         final long now = context.ticker().read() + ConnectedClientConnection.DEFAULT_BACKEND_ALIVE_TIMEOUT_NANOS;
-        final Optional<Long> timeout = connection.checkTimeout(now);
-        Assert.assertNotNull(timeout);
-        Assert.assertTrue(timeout.isPresent());
+        final OptionalLong timeout = connection.checkTimeout(now);
+        assertNotNull(timeout);
+        assertTrue(timeout.isPresent());
     }
 
     @Override
@@ -54,7 +56,7 @@ public class ReconnectingClientConnectionTest
     @Test
     public void testReconnectConnection() {
         final ClientActorBehavior<BackendInfo> behavior = mock(ClientActorBehavior.class);
-        Assert.assertSame(behavior, connection.lockedReconnect(behavior, mock(RequestException.class)));
+        assertSame(behavior, connection.lockedReconnect(behavior, mock(RequestException.class)));
     }
 
     @Override

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.