Execute the ShardedDOMDataTreeTransaction.submit() async.
[mdsal.git] / dom / mdsal-dom-broker / src / test / java / org / opendaylight / mdsal / dom / broker / ShardedDOMDataTreeTest.java
index d0d5ffbcac4fd6cf94e3fefc6c114665a565e75a..6cc21b8d0575cc74c256393402e887a9bdbf7411 100644 (file)
@@ -1,3 +1,10 @@
+/*
+ * 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.mdsal.dom.broker;
 
 import static org.junit.Assert.assertEquals;
@@ -85,7 +92,7 @@ public class ShardedDOMDataTreeTest {
     public void setUp() throws Exception {
         MockitoAnnotations.initMocks(this);
 
-        rootShard = InMemoryDOMDataTreeShard.create(ROOT_ID, executor, 5000);
+        rootShard = InMemoryDOMDataTreeShard.create(ROOT_ID, executor, 1);
         rootShard.onGlobalContextUpdated(schemaContext);
 
         final ShardedDOMDataTree dataTree = new ShardedDOMDataTree();
@@ -98,8 +105,40 @@ public class ShardedDOMDataTreeTest {
 
     @Test(expected = IllegalArgumentException.class)
     public void testProducerPathContention() throws Exception {
-        final DOMDataTreeProducer p1 = dataTreeService.createProducer(Collections.singletonList(ROOT_ID));
-        final DOMDataTreeProducer p2 = dataTreeService.createProducer(Collections.singletonList(TEST_ID));
+        dataTreeService.createProducer(Collections.singletonList(ROOT_ID));
+        dataTreeService.createProducer(Collections.singletonList(TEST_ID));
+    }
+
+    @Test
+    public void testShardRegistrationClose() throws Exception {
+        rootShardReg.close();
+
+        final InMemoryDOMDataTreeShard newRootShard = InMemoryDOMDataTreeShard.create(ROOT_ID, executor, 1);
+        newRootShard.onGlobalContextUpdated(schemaContext);
+        final DOMDataTreeProducer shardRegProducer = dataTreeService.createProducer(Collections.singletonList(ROOT_ID));
+
+        final ListenerRegistration<InMemoryDOMDataTreeShard> newRootShardReg =
+                dataTreeService.registerDataTreeShard(ROOT_ID, rootShard, shardRegProducer);
+        shardRegProducer.close();
+
+        final InMemoryDOMDataTreeShard innerShard = InMemoryDOMDataTreeShard.create(INNER_CONTAINER_ID, executor, 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
@@ -134,11 +173,11 @@ public class ShardedDOMDataTreeTest {
         cursor.close();
         tx.submit().checkedGet();
 
-        verify(mockedDataTreeListener, timeout(1000).times(2)).onDataTreeChanged(captorForChanges.capture(), captorForSubtrees.capture());
+        verify(mockedDataTreeListener, timeout(1000).times(3)).onDataTreeChanged(captorForChanges.capture(), captorForSubtrees.capture());
         final List<Collection<DataTreeCandidate>> capturedValue = captorForChanges.getAllValues();
-        assertTrue(capturedValue.size() == 2);
+        assertTrue(capturedValue.size() == 3);
 
-        final ContainerNode capturedChange = (ContainerNode) capturedValue.get(0).iterator().next().getRootNode().getDataAfter().get();
+        final ContainerNode capturedChange = (ContainerNode) capturedValue.get(1).iterator().next().getRootNode().getDataAfter().get();
         final ContainerNode innerContainerVerify = (ContainerNode) crossShardContainer.getChild(TestModel.INNER_CONTAINER_PATH.getLastPathArgument()).get();
         assertEquals(innerContainerVerify, capturedChange);