Fix NPE when creating a producer after shard was unregistered
[mdsal.git] / dom / mdsal-dom-broker / src / test / java / org / opendaylight / mdsal / dom / broker / ShardedDOMDataTreeTest.java
index 1ffa904ef44eb78f61c58872912bfa68d7b7ef49..528234f56e3f715b311c7d7cd7ada943ee3dfc85 100644 (file)
@@ -24,6 +24,7 @@ import java.util.List;
 import java.util.Map;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
+import junit.framework.Assert;
 import org.junit.Before;
 import org.junit.Test;
 import org.mockito.ArgumentCaptor;
@@ -109,6 +110,38 @@ public class ShardedDOMDataTreeTest {
         dataTreeService.createProducer(Collections.singletonList(TEST_ID));
     }
 
+    @Test
+    public void testShardRegistrationClose() throws Exception {
+        rootShardReg.close();
+
+        final InMemoryDOMDataTreeShard newRootShard = InMemoryDOMDataTreeShard.create(ROOT_ID, executor, 1, 1);
+        newRootShard.onGlobalContextUpdated(schemaContext);
+        final DOMDataTreeProducer shardRegProducer = dataTreeService.createProducer(Collections.singletonList(ROOT_ID));
+
+        ListenerRegistration<InMemoryDOMDataTreeShard> newRootShardReg =
+                dataTreeService.registerDataTreeShard(ROOT_ID, rootShard, shardRegProducer);
+        shardRegProducer.close();
+
+        final InMemoryDOMDataTreeShard innerShard = InMemoryDOMDataTreeShard.create(INNER_CONTAINER_ID, executor, 1, 1);
+        innerShard.onGlobalContextUpdated(schemaContext);
+        final DOMDataTreeProducer shardRegProducer2 = dataTreeService.createProducer(Collections.singletonList(INNER_CONTAINER_ID));
+        ListenerRegistration<InMemoryDOMDataTreeShard> innerShardReg = dataTreeService.registerDataTreeShard(INNER_CONTAINER_ID, innerShard, shardRegProducer2);
+
+        innerShardReg.close();
+        // try to register the shard again
+        innerShardReg = dataTreeService.registerDataTreeShard(INNER_CONTAINER_ID, innerShard, shardRegProducer2);
+        final DOMDataTreeCursorAwareTransaction tx = shardRegProducer2.createTransaction(false);
+        final DOMDataTreeWriteCursor cursor = tx.createCursor(INNER_CONTAINER_ID);
+        assertNotNull(cursor);
+
+        cursor.close();
+        tx.cancel();
+        shardRegProducer2.close();
+
+        innerShardReg.close();
+        newRootShardReg.close();
+    }
+
     @Test
     public void testSingleShardWrite() throws Exception {
         final DOMDataTreeListener mockedDataTreeListener = Mockito.mock(DOMDataTreeListener.class);