Migrate to java.time.Duration
[controller.git] / opendaylight / md-sal / sal-akka-raft / src / test / java / org / opendaylight / controller / cluster / raft / RaftActorTest.java
index db6fe1a687c64865c6b31bbee1fa2979e81478a8..d0cedce83a1d0787317e03c50e62537be511a774 100644 (file)
@@ -14,10 +14,10 @@ import static org.junit.Assert.assertNotSame;
 import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertSame;
 import static org.junit.Assert.assertTrue;
-import static org.mockito.Matchers.any;
-import static org.mockito.Matchers.anyObject;
-import static org.mockito.Matchers.eq;
-import static org.mockito.Matchers.same;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyObject;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.ArgumentMatchers.same;
 import static org.mockito.Mockito.doReturn;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.never;
@@ -27,7 +27,6 @@ import static org.mockito.Mockito.verify;
 
 import akka.actor.ActorRef;
 import akka.actor.PoisonPill;
-import akka.actor.Props;
 import akka.actor.Status.Failure;
 import akka.actor.Terminated;
 import akka.dispatch.Dispatchers;
@@ -36,14 +35,15 @@ import akka.persistence.SaveSnapshotFailure;
 import akka.persistence.SaveSnapshotSuccess;
 import akka.persistence.SnapshotMetadata;
 import akka.persistence.SnapshotOffer;
-import akka.testkit.JavaTestKit;
+import akka.protobuf.ByteString;
 import akka.testkit.TestActorRef;
+import akka.testkit.javadsl.TestKit;
 import com.google.common.base.Optional;
 import com.google.common.collect.ImmutableMap;
 import com.google.common.util.concurrent.Uninterruptibles;
-import com.google.protobuf.ByteString;
 import java.io.ByteArrayOutputStream;
 import java.io.ObjectOutputStream;
+import java.time.Duration;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collections;
@@ -92,7 +92,6 @@ import org.opendaylight.controller.cluster.raft.utils.MessageCollectorActor;
 import org.opendaylight.yangtools.concepts.Identifier;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
-import scala.concurrent.duration.Duration;
 import scala.concurrent.duration.FiniteDuration;
 
 public class RaftActorTest extends AbstractActorTest {
@@ -107,7 +106,7 @@ public class RaftActorTest extends AbstractActorTest {
     }
 
     @After
-    public void tearDown() throws Exception {
+    public void tearDown() {
         factory.close();
         InMemoryJournal.clear();
         InMemorySnapshotStore.clear();
@@ -126,10 +125,10 @@ public class RaftActorTest extends AbstractActorTest {
 
 
     @Test
-    public void testRaftActorRecoveryWithPersistenceEnabled() throws Exception {
+    public void testRaftActorRecoveryWithPersistenceEnabled() {
         TEST_LOG.info("testRaftActorRecoveryWithPersistenceEnabled starting");
 
-        JavaTestKit kit = new JavaTestKit(getSystem());
+        TestKit kit = new TestKit(getSystem());
         String persistenceId = factory.generateActorId("follower-");
 
         DefaultConfigParamsImpl config = new DefaultConfigParamsImpl();
@@ -184,7 +183,7 @@ public class RaftActorTest extends AbstractActorTest {
 
         // kill the actor
         followerActor.tell(PoisonPill.getInstance(), null);
-        kit.expectMsgClass(JavaTestKit.duration("5 seconds"), Terminated.class);
+        kit.expectMsgClass(Duration.ofSeconds(5), Terminated.class);
 
         kit.unwatch(followerActor);
 
@@ -213,7 +212,7 @@ public class RaftActorTest extends AbstractActorTest {
     }
 
     @Test
-    public void testRaftActorRecoveryWithPersistenceDisabled() throws Exception {
+    public void testRaftActorRecoveryWithPersistenceDisabled() {
         String persistenceId = factory.generateActorId("follower-");
 
         DefaultConfigParamsImpl config = new DefaultConfigParamsImpl();
@@ -222,7 +221,7 @@ public class RaftActorTest extends AbstractActorTest {
 
         TestActorRef<MockRaftActor> ref = factory.createTestActor(MockRaftActor.props(persistenceId,
                 ImmutableMap.<String, String>builder().put("member1", "address").build(),
-                config, new NonPersistentDataProvider()), persistenceId);
+                config, createProvider()), persistenceId);
 
         MockRaftActor mockRaftActor = ref.underlyingActor();
 
@@ -234,8 +233,8 @@ public class RaftActorTest extends AbstractActorTest {
     }
 
     @Test
-    public void testUpdateElectionTermPersistedWithPersistenceDisabled() throws Exception {
-        final JavaTestKit kit = new JavaTestKit(getSystem());
+    public void testUpdateElectionTermPersistedWithPersistenceDisabled() {
+        final TestKit kit = new TestKit(getSystem());
         String persistenceId = factory.generateActorId("follower-");
         DefaultConfigParamsImpl config = new DefaultConfigParamsImpl();
         config.setHeartBeatInterval(new FiniteDuration(100, TimeUnit.MILLISECONDS));
@@ -245,7 +244,7 @@ public class RaftActorTest extends AbstractActorTest {
 
         TestActorRef<MockRaftActor> ref = factory.createTestActor(MockRaftActor.props(persistenceId,
                 ImmutableMap.<String, String>builder().put("member1", "address").build(),
-                config, new NonPersistentDataProvider())
+                config, createProvider())
                 .withDispatcher(Dispatchers.DefaultDispatcherId()), persistenceId);
 
         InMemoryJournal.waitForWriteMessagesComplete(persistenceId);
@@ -258,7 +257,7 @@ public class RaftActorTest extends AbstractActorTest {
         config.setHeartBeatInterval(new FiniteDuration(1, TimeUnit.DAYS));
         ref = factory.createTestActor(MockRaftActor.props(persistenceId,
                 ImmutableMap.<String, String>builder().put("member1", "address").build(), config,
-                new NonPersistentDataProvider()).withDispatcher(Dispatchers.DefaultDispatcherId()),
+                createProvider()).withDispatcher(Dispatchers.DefaultDispatcherId()),
                 factory.generateActorId("follower-"));
 
         MockRaftActor actor = ref.underlyingActor();
@@ -394,7 +393,7 @@ public class RaftActorTest extends AbstractActorTest {
     }
 
     @Test
-    public void testApplyState() throws Exception {
+    public void testApplyState() {
         String persistenceId = factory.generateActorId("leader-");
 
         DefaultConfigParamsImpl config = new DefaultConfigParamsImpl();
@@ -420,8 +419,7 @@ public class RaftActorTest extends AbstractActorTest {
 
     @Test
     public void testRaftRoleChangeNotifierWhenRaftActorHasNoPeers() throws Exception {
-        TestActorRef<MessageCollectorActor> notifierActor = factory.createTestActor(
-                Props.create(MessageCollectorActor.class));
+        ActorRef notifierActor = factory.createActor(MessageCollectorActor.props());
         MessageCollectorActor.waitUntilReady(notifierActor);
 
         DefaultConfigParamsImpl config = new DefaultConfigParamsImpl();
@@ -433,7 +431,7 @@ public class RaftActorTest extends AbstractActorTest {
 
         final TestActorRef<MockRaftActor> raftActorRef = factory.createTestActor(MockRaftActor.builder()
                 .id(persistenceId).config(config).roleChangeNotifier(notifierActor).dataPersistenceProvider(
-                        new NonPersistentDataProvider()).props().withDispatcher(Dispatchers.DefaultDispatcherId()),
+                    createProvider()).props().withDispatcher(Dispatchers.DefaultDispatcherId()),
                 persistenceId);
 
         List<RoleChanged> matches =  MessageCollectorActor.expectMatching(notifierActor, RoleChanged.class, 3);
@@ -463,14 +461,14 @@ public class RaftActorTest extends AbstractActorTest {
         assertEquals(raftRoleChanged.getMemberId(), leaderStateChange.getLeaderId());
         assertEquals(MockRaftActor.PAYLOAD_VERSION, leaderStateChange.getLeaderPayloadVersion());
 
-        notifierActor.underlyingActor().clear();
+        MessageCollectorActor.clearMessages(notifierActor);
 
         MockRaftActor raftActor = raftActorRef.underlyingActor();
         final String newLeaderId = "new-leader";
         final short newLeaderVersion = 6;
         Follower follower = new Follower(raftActor.getRaftActorContext()) {
             @Override
-            public RaftActorBehavior handleMessage(ActorRef sender, Object message) {
+            public RaftActorBehavior handleMessage(final ActorRef sender, final Object message) {
                 setLeaderId(newLeaderId);
                 setLeaderPayloadVersion(newLeaderVersion);
                 return this;
@@ -487,7 +485,7 @@ public class RaftActorTest extends AbstractActorTest {
         assertEquals(RaftState.Leader.name(), raftRoleChanged.getOldRole());
         assertEquals(RaftState.Follower.name(), raftRoleChanged.getNewRole());
 
-        notifierActor.underlyingActor().clear();
+        MessageCollectorActor.clearMessages(notifierActor);
 
         raftActor.handleCommand("any");
 
@@ -496,7 +494,7 @@ public class RaftActorTest extends AbstractActorTest {
         assertEquals(newLeaderId, leaderStateChange.getLeaderId());
         assertEquals(newLeaderVersion, leaderStateChange.getLeaderPayloadVersion());
 
-        notifierActor.underlyingActor().clear();
+        MessageCollectorActor.clearMessages(notifierActor);
 
         raftActor.handleCommand("any");
 
@@ -507,7 +505,7 @@ public class RaftActorTest extends AbstractActorTest {
 
     @Test
     public void testRaftRoleChangeNotifierWhenRaftActorHasPeers() throws Exception {
-        ActorRef notifierActor = factory.createActor(Props.create(MessageCollectorActor.class));
+        ActorRef notifierActor = factory.createActor(MessageCollectorActor.props());
         MessageCollectorActor.waitUntilReady(notifierActor);
 
         DefaultConfigParamsImpl config = new DefaultConfigParamsImpl();
@@ -552,8 +550,7 @@ public class RaftActorTest extends AbstractActorTest {
         final String persistenceId = factory.generateActorId("leader-");
         final String follower1Id = factory.generateActorId("follower-");
 
-        ActorRef followerActor1 =
-                factory.createActor(Props.create(MessageCollectorActor.class));
+        ActorRef followerActor1 = factory.createActor(MessageCollectorActor.props());
 
         DefaultConfigParamsImpl config = new DefaultConfigParamsImpl();
         config.setHeartBeatInterval(new FiniteDuration(1, TimeUnit.DAYS));
@@ -642,9 +639,7 @@ public class RaftActorTest extends AbstractActorTest {
         final String persistenceId = factory.generateActorId("follower-");
         final String leaderId = factory.generateActorId("leader-");
 
-
-        ActorRef leaderActor1 =
-                factory.createActor(Props.create(MessageCollectorActor.class));
+        ActorRef leaderActor1 = factory.createActor(MessageCollectorActor.props());
 
         DefaultConfigParamsImpl config = new DefaultConfigParamsImpl();
         config.setHeartBeatInterval(new FiniteDuration(1, TimeUnit.DAYS));
@@ -734,8 +729,8 @@ public class RaftActorTest extends AbstractActorTest {
         final String follower1Id = factory.generateActorId("follower-");
         final String follower2Id = factory.generateActorId("follower-");
 
-        final ActorRef followerActor1 = factory.createActor(Props.create(MessageCollectorActor.class), follower1Id);
-        final ActorRef followerActor2 = factory.createActor(Props.create(MessageCollectorActor.class), follower2Id);
+        final ActorRef followerActor1 = factory.createActor(MessageCollectorActor.props(), follower1Id);
+        final ActorRef followerActor2 = factory.createActor(MessageCollectorActor.props(), follower2Id);
 
         DefaultConfigParamsImpl config = new DefaultConfigParamsImpl();
         config.setHeartBeatInterval(new FiniteDuration(1, TimeUnit.DAYS));
@@ -820,7 +815,7 @@ public class RaftActorTest extends AbstractActorTest {
         config.setIsolatedLeaderCheckInterval(new FiniteDuration(1, TimeUnit.DAYS));
         config.setSnapshotBatchCount(5);
 
-        DataPersistenceProvider dataPersistenceProvider = new NonPersistentDataProvider();
+        DataPersistenceProvider dataPersistenceProvider = createProvider();
 
         Map<String, String> peerAddresses = ImmutableMap.<String, String>builder().put("member1", "address").build();
 
@@ -864,7 +859,7 @@ public class RaftActorTest extends AbstractActorTest {
         config.setIsolatedLeaderCheckInterval(new FiniteDuration(1, TimeUnit.DAYS));
         config.setSnapshotBatchCount(5);
 
-        DataPersistenceProvider dataPersistenceProvider = new NonPersistentDataProvider();
+        DataPersistenceProvider dataPersistenceProvider = createProvider();
 
         Map<String, String> peerAddresses = ImmutableMap.<String, String>builder().put("member1", "address").build();
 
@@ -897,6 +892,10 @@ public class RaftActorTest extends AbstractActorTest {
         assertEquals(3, leader.getReplicatedToAllIndex());
     }
 
+    private static DataPersistenceProvider createProvider() {
+        return new NonPersistentDataProvider(Runnable::run);
+    }
+
     @Test
     public void testSwitchBehavior() {
         String persistenceId = factory.generateActorId("leader-");
@@ -906,7 +905,7 @@ public class RaftActorTest extends AbstractActorTest {
         config.setIsolatedLeaderCheckInterval(new FiniteDuration(1, TimeUnit.DAYS));
         config.setSnapshotBatchCount(5);
 
-        DataPersistenceProvider dataPersistenceProvider = new NonPersistentDataProvider();
+        DataPersistenceProvider dataPersistenceProvider = createProvider();
 
         Map<String, String> peerAddresses = ImmutableMap.<String, String>builder().build();
 
@@ -938,7 +937,7 @@ public class RaftActorTest extends AbstractActorTest {
         assertEquals(RaftState.Leader, leaderActor.getCurrentBehavior().state());
     }
 
-    public static ByteString fromObject(Object snapshot) throws Exception {
+    public static ByteString fromObject(final Object snapshot) throws Exception {
         ByteArrayOutputStream bos = null;
         ObjectOutputStream os = null;
         try {
@@ -959,7 +958,7 @@ public class RaftActorTest extends AbstractActorTest {
     }
 
     @Test
-    public void testUpdateConfigParam() throws Exception {
+    public void testUpdateConfigParam() {
         DefaultConfigParamsImpl emptyConfig = new DefaultConfigParamsImpl();
         String persistenceId = factory.generateActorId("follower-");
         ImmutableMap<String, String> peerAddresses =
@@ -1007,10 +1006,10 @@ public class RaftActorTest extends AbstractActorTest {
     }
 
     @Test
-    public void testGetSnapshot() throws Exception {
+    public void testGetSnapshot() {
         TEST_LOG.info("testGetSnapshot starting");
 
-        final JavaTestKit kit = new JavaTestKit(getSystem());
+        final TestKit kit = new TestKit(getSystem());
 
         String persistenceId = factory.generateActorId("test-actor-");
         DefaultConfigParamsImpl config = new DefaultConfigParamsImpl();
@@ -1063,14 +1062,15 @@ public class RaftActorTest extends AbstractActorTest {
         // Test with timeout
 
         mockRaftActor.getSnapshotMessageSupport().setSnapshotReplyActorTimeout(
-                Duration.create(200, TimeUnit.MILLISECONDS));
+            FiniteDuration.create(200, TimeUnit.MILLISECONDS));
         reset(mockRaftActor.snapshotCohortDelegate);
 
         raftActorRef.tell(GetSnapshot.INSTANCE, kit.getRef());
         Failure failure = kit.expectMsgClass(akka.actor.Status.Failure.class);
         assertEquals("Failure cause type", TimeoutException.class, failure.cause().getClass());
 
-        mockRaftActor.getSnapshotMessageSupport().setSnapshotReplyActorTimeout(Duration.create(30, TimeUnit.SECONDS));
+        mockRaftActor.getSnapshotMessageSupport().setSnapshotReplyActorTimeout(
+            FiniteDuration.create(30, TimeUnit.SECONDS));
 
         // Test with persistence disabled.
 
@@ -1096,7 +1096,7 @@ public class RaftActorTest extends AbstractActorTest {
     }
 
     @Test
-    public void testRestoreFromSnapshot() throws Exception {
+    public void testRestoreFromSnapshot() {
         TEST_LOG.info("testRestoreFromSnapshot starting");
 
         String persistenceId = factory.generateActorId("test-actor-");
@@ -1209,7 +1209,7 @@ public class RaftActorTest extends AbstractActorTest {
     }
 
     @Test
-    public void testNonVotingOnRecovery() throws Exception {
+    public void testNonVotingOnRecovery() {
         TEST_LOG.info("testNonVotingOnRecovery starting");
 
         DefaultConfigParamsImpl config = new DefaultConfigParamsImpl();
@@ -1235,11 +1235,10 @@ public class RaftActorTest extends AbstractActorTest {
     }
 
     @Test
-    public void testLeaderTransitioning() throws Exception {
+    public void testLeaderTransitioning() {
         TEST_LOG.info("testLeaderTransitioning starting");
 
-        TestActorRef<MessageCollectorActor> notifierActor = factory.createTestActor(
-                Props.create(MessageCollectorActor.class));
+        ActorRef notifierActor = factory.createActor(MessageCollectorActor.props());
 
         DefaultConfigParamsImpl config = new DefaultConfigParamsImpl();
         config.setCustomRaftPolicyImplementationClass(DisableElectionsRaftPolicy.class.getName());
@@ -1276,7 +1275,7 @@ public class RaftActorTest extends AbstractActorTest {
         final String leaderId = factory.generateActorId("leader-");
         final String followerId = factory.generateActorId("follower-");
 
-        final ActorRef followerActor = factory.createActor(Props.create(MessageCollectorActor.class));
+        final ActorRef followerActor = factory.createActor(MessageCollectorActor.props());
 
         DefaultConfigParamsImpl config = new DefaultConfigParamsImpl();
         config.setHeartBeatInterval(new FiniteDuration(1, TimeUnit.DAYS));
@@ -1321,7 +1320,7 @@ public class RaftActorTest extends AbstractActorTest {
         final String leaderId = factory.generateActorId("leader-");
         final String followerId = factory.generateActorId("follower-");
 
-        final ActorRef followerActor = factory.createActor(Props.create(MessageCollectorActor.class));
+        final ActorRef followerActor = factory.createActor(MessageCollectorActor.props());
 
         DefaultConfigParamsImpl config = new DefaultConfigParamsImpl();
         config.setHeartBeatInterval(new FiniteDuration(1, TimeUnit.DAYS));