Bug 4105: Implement candidate registration close
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / entityownership / DistributedEntityOwnershipServiceTest.java
index ee5b1c5a2d3d1f5a896e05e56c20b18ad2a010ba..860d3dc80914edb0f5ab028d1adb227fbf9b61c8 100644 (file)
@@ -10,9 +10,11 @@ package org.opendaylight.controller.cluster.datastore.entityownership;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertSame;
+import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
 import static org.mockito.Mockito.mock;
 import akka.actor.ActorRef;
+import akka.actor.PoisonPill;
 import akka.actor.Props;
 import com.google.common.util.concurrent.Uninterruptibles;
 import java.util.Collections;
@@ -21,12 +23,14 @@ import java.util.Map;
 import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.atomic.AtomicReference;
+import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
 import org.opendaylight.controller.cluster.datastore.AbstractActorTest;
 import org.opendaylight.controller.cluster.datastore.DatastoreContext;
 import org.opendaylight.controller.cluster.datastore.DistributedDataStore;
 import org.opendaylight.controller.cluster.datastore.entityownership.messages.RegisterCandidateLocal;
+import org.opendaylight.controller.cluster.datastore.entityownership.messages.UnregisterCandidateLocal;
 import org.opendaylight.controller.cluster.datastore.identifiers.ShardIdentifier;
 import org.opendaylight.controller.cluster.datastore.utils.MockClusterWrapper;
 import org.opendaylight.controller.cluster.datastore.utils.MockConfiguration;
@@ -57,7 +61,7 @@ public class DistributedEntityOwnershipServiceTest extends AbstractActorTest {
     private DistributedDataStore dataStore;
 
     @Before
-    public void setUp() throws Exception {
+    public void setUp() {
         DatastoreContext datastoreContext = DatastoreContext.newBuilder().dataStoreType(dataStoreType).
                 shardInitializationTimeout(10, TimeUnit.SECONDS).build();
         dataStore = new DistributedDataStore(getSystem(), new MockClusterWrapper(),
@@ -66,6 +70,11 @@ public class DistributedEntityOwnershipServiceTest extends AbstractActorTest {
         dataStore.onGlobalContextUpdated(TestModel.createTestContext());
     }
 
+    @After
+    public void tearDown() {
+        dataStore.getActorContext().getShardManager().tell(PoisonPill.getInstance(), ActorRef.noSender());
+    }
+
     @Test
     public void testEntityOwnershipShardCreated() throws Exception {
         DistributedEntityOwnershipService service = new DistributedEntityOwnershipService(dataStore);
@@ -101,7 +110,7 @@ public class DistributedEntityOwnershipServiceTest extends AbstractActorTest {
         verifyEntityOwnershipCandidateRegistration(entity, reg);
         verifyRegisterCandidateLocal(shardPropsCreator, entity, candidate);
 
-        // Test same entity - should throw exception
+        // Register the same entity - should throw exception
 
         EntityOwnershipCandidate candidate2 = mock(EntityOwnershipCandidate.class);
         try {
@@ -113,7 +122,7 @@ public class DistributedEntityOwnershipServiceTest extends AbstractActorTest {
             assertEquals("getEntity", entity, e.getEntity());
         }
 
-        // Test different entity
+        // Register a different entity - should succeed
 
         Entity entity2 = new Entity(ENTITY_TYPE2, YangInstanceIdentifier.of(QNAME));
         shardPropsCreator.expectShardMessage(RegisterCandidateLocal.class);
@@ -126,6 +135,46 @@ public class DistributedEntityOwnershipServiceTest extends AbstractActorTest {
         service.close();
     }
 
+    @Test
+    public void testCloseCandidateRegistration() throws Exception {
+        final TestShardPropsCreator shardPropsCreator = new TestShardPropsCreator();
+        DistributedEntityOwnershipService service = new DistributedEntityOwnershipService(dataStore) {
+            @Override
+            protected EntityOwnershipShardPropsCreator newShardPropsCreator() {
+                return shardPropsCreator;
+            }
+        };
+
+        service.start();
+
+        shardPropsCreator.expectShardMessage(RegisterCandidateLocal.class);
+
+        Entity entity = new Entity(ENTITY_TYPE, YangInstanceIdentifier.of(QNAME));
+        EntityOwnershipCandidate candidate = mock(EntityOwnershipCandidate.class);
+
+        EntityOwnershipCandidateRegistration reg = service.registerCandidate(entity, candidate);
+
+        verifyEntityOwnershipCandidateRegistration(entity, reg);
+        verifyRegisterCandidateLocal(shardPropsCreator, entity, candidate);
+
+        shardPropsCreator.expectShardMessage(UnregisterCandidateLocal.class);
+
+        reg.close();
+
+        UnregisterCandidateLocal unregCandidate = shardPropsCreator.waitForShardMessage();
+        assertEquals("getEntity", entity, unregCandidate.getEntity());
+
+        // Re-register - should succeed.
+
+        shardPropsCreator.expectShardMessage(RegisterCandidateLocal.class);
+
+        service.registerCandidate(entity, candidate);
+
+        verifyRegisterCandidateLocal(shardPropsCreator, entity, candidate);
+
+        service.close();
+    }
+
     @Test
     public void testRegisterListener() {
     }
@@ -156,8 +205,8 @@ public class DistributedEntityOwnershipServiceTest extends AbstractActorTest {
 
         @SuppressWarnings("unchecked")
         <T> T waitForShardMessage() {
-            assertEquals("Message received", true, Uninterruptibles.awaitUninterruptibly(
-                    messageReceived.get(), 5, TimeUnit.SECONDS));
+            assertTrue("Message " + messageClass.get().getSimpleName() + " was not received",
+                    Uninterruptibles.awaitUninterruptibly(messageReceived.get(), 5, TimeUnit.SECONDS));
             assertEquals("Message type", messageClass.get(), receivedMessage.get().getClass());
             return (T) receivedMessage.get();
         }