BUG-4328: TransactionChainManager state 36/27336/1
authorJozef Gloncak <jgloncak@cisco.com>
Wed, 23 Sep 2015 06:20:51 +0000 (08:20 +0200)
committerJozef Gloncak <jgloncak@cisco.com>
Wed, 23 Sep 2015 06:20:51 +0000 (08:20 +0200)
 - txChainManager actually never goes back from SHUTTING_DOWN state
 - added more unit tests in order to exersize the tear down mechanism

Change-Id: I6ef95d6b8e7d8211c81ce7f6e286756a040c535e
Signed-off-by: Jozef Gloncak <jgloncak@cisco.com>
openflowplugin-impl/src/test/java/org/opendaylight/openflowplugin/impl/device/DeviceTransactionChainManagerProviderTest.java
openflowplugin-impl/src/test/java/org/opendaylight/openflowplugin/impl/device/TransactionChainManagerTest.java

index 04cc1f6dd6af7503f3ac18f3b83eda5a9042ec22..6b759be020636e3ceb5219048c05569471900879 100644 (file)
@@ -8,14 +8,25 @@
 
 package org.opendaylight.openflowplugin.impl.device;
 
-import junit.framework.Assert;
+import com.google.common.base.Function;
+import com.google.common.util.concurrent.CheckedFuture;
+import com.google.common.util.concurrent.Futures;
+import com.google.common.util.concurrent.SettableFuture;
+import javax.annotation.Nullable;
+import org.junit.Assert;
 import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
+import org.mockito.Matchers;
 import org.mockito.Mock;
 import org.mockito.Mockito;
 import org.mockito.runners.MockitoJUnitRunner;
+import org.opendaylight.controller.md.sal.binding.api.BindingTransactionChain;
 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
+import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
+import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
+import org.opendaylight.controller.md.sal.common.api.data.TransactionChainListener;
+import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
 import org.opendaylight.openflowplugin.api.openflow.device.DeviceManager;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
@@ -30,9 +41,14 @@ public class DeviceTransactionChainManagerProviderTest {
     ConnectionContext connectionContext;
     @Mock
     ConnectionContext concurrentConnectionContex;
-
+    @Mock
+    private BindingTransactionChain txChain;
     @Mock
     DeviceManager deviceManager;
+    @Mock
+    private WriteTransaction writeTx;
+    @Mock
+    private ReadyForNewTransactionChainHandler readyForNewTransactionChainHandler;
 
     private static final NodeId nodeId = new NodeId("OPF:TEST");
     private DeviceTransactionChainManagerProvider deviceTransactionChainManagerProvider;
@@ -42,6 +58,20 @@ public class DeviceTransactionChainManagerProviderTest {
         deviceTransactionChainManagerProvider = new DeviceTransactionChainManagerProvider(dataBroker);
         Mockito.when(connectionContext.getNodeId()).thenReturn(nodeId);
         Mockito.when(concurrentConnectionContex.getNodeId()).thenReturn(nodeId);
+
+        final ReadOnlyTransaction readOnlyTx = Mockito.mock(ReadOnlyTransaction.class);
+        //final CheckedFuture<Optional<Node>, ReadFailedException> noExistNodeFuture = Futures.immediateCheckedFuture(Optional.<Node>absent());
+//        Mockito.when(readOnlyTx.read(LogicalDatastoreType.OPERATIONAL, nodeKeyIdent)).thenReturn(noExistNodeFuture);
+        Mockito.when(dataBroker.newReadOnlyTransaction()).thenReturn(readOnlyTx);
+        Mockito.when(dataBroker.createTransactionChain(Matchers.any(TransactionChainListener.class)))
+                .thenReturn(txChain);
+
+//        nodeKeyIdent = DeviceStateUtil.createNodeInstanceIdentifier(nodeId);
+//        txChainManager = new TransactionChainManager(dataBroker, nodeKeyIdent, registration);
+        Mockito.when(txChain.newWriteOnlyTransaction()).thenReturn(writeTx);
+
+//        path = InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(nodeId));
+//        Mockito.when(writeTx.submit()).thenReturn(Futures.<Void, TransactionCommitFailedException>immediateCheckedFuture(null));
     }
 
     /**
@@ -53,7 +83,11 @@ public class DeviceTransactionChainManagerProviderTest {
     @Test
     public void testProvideTransactionChainManagerOrWaitForNotification1() throws Exception {
         DeviceTransactionChainManagerProvider.TransactionChainManagerRegistration transactionChainManagerRegistration = deviceTransactionChainManagerProvider.provideTransactionChainManager(connectionContext);
+        final TransactionChainManager txChainManager = transactionChainManagerRegistration.getTransactionChainManager();
+
         Assert.assertTrue(transactionChainManagerRegistration.ownedByInvokingConnectionContext());
+        Assert.assertNotNull(txChainManager);
+        Assert.assertEquals(TransactionChainManager.TransactionChainManagerStatus.WORKING, txChainManager.getTransactionChainManagerStatus());
     }
 
     /**
@@ -65,9 +99,69 @@ public class DeviceTransactionChainManagerProviderTest {
     @Test
     public void testProvideTransactionChainManagerOrWaitForNotification2() throws Exception {
         DeviceTransactionChainManagerProvider.TransactionChainManagerRegistration transactionChainManagerRegistration_1 = deviceTransactionChainManagerProvider.provideTransactionChainManager(connectionContext);
-        Assert.assertTrue(TransactionChainManager.TransactionChainManagerStatus.WORKING.equals(transactionChainManagerRegistration_1.getTransactionChainManager().getTransactionChainManagerStatus()));
+        Assert.assertEquals(TransactionChainManager.TransactionChainManagerStatus.WORKING, transactionChainManagerRegistration_1.getTransactionChainManager().getTransactionChainManagerStatus());
         DeviceTransactionChainManagerProvider.TransactionChainManagerRegistration transactionChainManagerRegistration_2 = deviceTransactionChainManagerProvider.provideTransactionChainManager(concurrentConnectionContex);
         Assert.assertFalse(transactionChainManagerRegistration_2.ownedByInvokingConnectionContext());
     }
 
+    /**
+     * This test verifies code path for registering new connection when {@link org.opendaylight.openflowplugin.impl.device.TransactionChainManager}
+     * is present in registry and in SHUTTING_DOWN state (finished).
+     *
+     * @throws Exception
+     */
+    @Test
+    public void testProvideTransactionChainManagerRecreate1() throws Exception {
+        DeviceTransactionChainManagerProvider.TransactionChainManagerRegistration txChainManagerRegistration_1 = deviceTransactionChainManagerProvider.provideTransactionChainManager(connectionContext);
+        final TransactionChainManager txChainManager = txChainManagerRegistration_1.getTransactionChainManager();
+        Assert.assertTrue(txChainManagerRegistration_1.ownedByInvokingConnectionContext());
+        Assert.assertNotNull(txChainManager);
+        Assert.assertEquals(TransactionChainManager.TransactionChainManagerStatus.WORKING,
+                txChainManagerRegistration_1.getTransactionChainManager().getTransactionChainManagerStatus());
+
+        CheckedFuture<Void, TransactionCommitFailedException> checkedSubmitCleanFuture = Futures.immediateCheckedFuture(null);
+        Mockito.when(writeTx.submit()).thenReturn(checkedSubmitCleanFuture);
+        txChainManager.close();
+        Assert.assertEquals(TransactionChainManager.TransactionChainManagerStatus.SHUTTING_DOWN,
+                txChainManagerRegistration_1.getTransactionChainManager().getTransactionChainManagerStatus());
+        txChainManager.attemptToRegisterHandler(readyForNewTransactionChainHandler);
+        Mockito.verify(readyForNewTransactionChainHandler).onReadyForNewTransactionChain();
+    }
+
+
+    /**
+     * This test verifies code path for registering new connection when {@link org.opendaylight.openflowplugin.impl.device.TransactionChainManager}
+     * is present in registry and in SHUTTING_DOWN state (unfinished).
+     *
+     * @throws Exception
+     */
+    @Test
+    public void testProvideTransactionChainManagerRecreate2() throws Exception {
+        DeviceTransactionChainManagerProvider.TransactionChainManagerRegistration txChainManagerRegistration_1 = deviceTransactionChainManagerProvider.provideTransactionChainManager(connectionContext);
+        final TransactionChainManager txChainManager = txChainManagerRegistration_1.getTransactionChainManager();
+        Assert.assertTrue(txChainManagerRegistration_1.ownedByInvokingConnectionContext());
+        Assert.assertNotNull(txChainManager);
+        Assert.assertEquals(TransactionChainManager.TransactionChainManagerStatus.WORKING,
+                txChainManagerRegistration_1.getTransactionChainManager().getTransactionChainManagerStatus());
+
+        SettableFuture<Void> submitCleanFuture = SettableFuture.create();
+        CheckedFuture<Void, TransactionCommitFailedException> checkedSubmitCleanFuture =
+                Futures.makeChecked(submitCleanFuture, new Function<Exception, TransactionCommitFailedException>() {
+                    @Nullable
+                    @Override
+                    public TransactionCommitFailedException apply(Exception input) {
+                        return new TransactionCommitFailedException("tx failed..", input);
+                    }
+                });
+        Mockito.when(writeTx.submit()).thenReturn(checkedSubmitCleanFuture);
+        txChainManager.close();
+        Assert.assertEquals(TransactionChainManager.TransactionChainManagerStatus.SHUTTING_DOWN,
+                txChainManagerRegistration_1.getTransactionChainManager().getTransactionChainManagerStatus());
+        txChainManager.attemptToRegisterHandler(readyForNewTransactionChainHandler);
+        Mockito.verify(readyForNewTransactionChainHandler, Mockito.never()).onReadyForNewTransactionChain();
+
+        submitCleanFuture.set(null);
+        Mockito.verify(readyForNewTransactionChainHandler).onReadyForNewTransactionChain();
+    }
+
 }
\ No newline at end of file
index 7478b6fae751b8ff175ab29d418e984f4c677a24..9026c60f810c9f34f9a7b9300eb457336e6d380b 100644 (file)
@@ -171,8 +171,6 @@ public class TransactionChainManagerTest {
 
         boolean attemptResult = txChainManager.attemptToRegisterHandler(readyForNewTransactionChainHandler);
         Assert.assertTrue(attemptResult);
-        //TODO: uncomment when txChainManager fixed (BUG-4328)
-        //Assert.assertEquals(TransactionChainManager.TransactionChainManagerStatus.WORKING, txChainManager.getTransactionChainManagerStatus());
 
         inOrder.verify(txChain).newWriteOnlyTransaction();
         inOrder.verify(writeTx).delete(LogicalDatastoreType.OPERATIONAL, path);