BUG-5280: implement backend message handling
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / DataTreeCohortIntegrationTest.java
index 84521e5ce6008bb388a2b02da6ab7c2f034ada73..21c8497db6042584aa4f03f14f4526eaf1c4baa4 100644 (file)
@@ -20,13 +20,11 @@ import akka.actor.Address;
 import akka.actor.AddressFromURIString;
 import akka.cluster.Cluster;
 import akka.testkit.JavaTestKit;
-import akka.util.Timeout;
 import com.google.common.base.Throwables;
 import com.google.common.util.concurrent.CheckedFuture;
 import com.google.common.util.concurrent.Futures;
 import com.typesafe.config.ConfigFactory;
 import java.io.IOException;
-import java.util.concurrent.TimeUnit;
 import org.junit.AfterClass;
 import org.junit.BeforeClass;
 import org.junit.Ignore;
@@ -47,7 +45,6 @@ import org.opendaylight.yangtools.concepts.ObjectRegistration;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
-import scala.concurrent.duration.Duration;
 
 public class DataTreeCohortIntegrationTest {
 
@@ -59,8 +56,6 @@ public class DataTreeCohortIntegrationTest {
     private static final DOMDataTreeIdentifier TEST_ID =
             new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, TestModel.TEST_PATH);
 
-    private static final Timeout TIMEOUT = new Timeout(Duration.create(5, TimeUnit.SECONDS));
-
     private static ActorSystem system;
 
     private final DatastoreContext.Builder datastoreContextBuilder =
@@ -91,30 +86,34 @@ public class DataTreeCohortIntegrationTest {
         ArgumentCaptor<DOMDataTreeCandidate> candidateCapt = ArgumentCaptor.forClass(DOMDataTreeCandidate.class);
         new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
             {
-                final DistributedDataStore dataStore = setupDistributedDataStore("transactionIntegrationTest", "test-1");
-                final ObjectRegistration<DOMDataTreeCommitCohort> cohortReg = dataStore.registerCommitCohort(TEST_ID, cohort);
-                Thread.sleep(1000); // Registration is asynchronous
-                assertNotNull(cohortReg);
-                testWriteTransaction(dataStore, TestModel.TEST_PATH,
+                try (final DistributedDataStore dataStore = setupDistributedDataStore("transactionIntegrationTest",
+                        "test-1")) {
+                    final ObjectRegistration<DOMDataTreeCommitCohort> cohortReg =
+                            dataStore.registerCommitCohort(TEST_ID, cohort);
+                    Thread.sleep(1000); // Registration is asynchronous
+                    assertNotNull(cohortReg);
+                    testWriteTransaction(dataStore, TestModel.TEST_PATH,
                         ImmutableNodes.containerNode(TestModel.TEST_QNAME));
-                Mockito.verify(cohort).canCommit(any(Object.class), candidateCapt.capture(), any(SchemaContext.class));
-                DOMDataTreeCandidate candidate = candidateCapt.getValue();
-                assertNotNull(candidate);
-                assertEquals(TEST_ID, candidate.getRootPath());
-                testWriteTransaction(dataStore, TestModel.OUTER_LIST_PATH,
+                    Mockito.verify(cohort).canCommit(any(Object.class), candidateCapt.capture(),
+                            any(SchemaContext.class));
+                    DOMDataTreeCandidate candidate = candidateCapt.getValue();
+                    assertNotNull(candidate);
+                    assertEquals(TEST_ID, candidate.getRootPath());
+                    testWriteTransaction(dataStore, TestModel.OUTER_LIST_PATH,
                         ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build());
-                Mockito.verify(cohort, Mockito.times(2)).canCommit(any(Object.class), any(DOMDataTreeCandidate.class),
-                        any(SchemaContext.class));
-                cohortReg.close();
-                testWriteTransaction(dataStore, TestModel.TEST_PATH,
+                    Mockito.verify(cohort, Mockito.times(2)).canCommit(any(Object.class),
+                            any(DOMDataTreeCandidate.class), any(SchemaContext.class));
+                    cohortReg.close();
+                    testWriteTransaction(dataStore, TestModel.TEST_PATH,
                         ImmutableNodes.containerNode(TestModel.TEST_QNAME));
-                Mockito.verifyNoMoreInteractions(cohort);
-                cleanup(dataStore);
+                    Mockito.verifyNoMoreInteractions(cohort);
+                }
             }
         };
     }
 
     @Test
+    @SuppressWarnings("checkstyle:IllegalCatch")
     public void failCanCommitTest() throws Exception {
         final DOMDataTreeCommitCohort failedCohort = mock(DOMDataTreeCommitCohort.class);
 
@@ -123,32 +122,30 @@ public class DataTreeCohortIntegrationTest {
 
         new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
             {
-                final DistributedDataStore dataStore =
-                        setupDistributedDataStore("transactionIntegrationTest", "test-1");
-                dataStore.registerCommitCohort(TEST_ID, failedCohort);
-                Thread.sleep(1000); // Registration is asynchronous
-
-                DOMStoreWriteTransaction writeTx = dataStore.newWriteOnlyTransaction();
-                writeTx.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
-                DOMStoreThreePhaseCommitCohort dsCohort = writeTx.ready();
-                try {
-                    // FIXME: Weird thing is that invoking canCommit on front-end invokes also
-                    // preCommit on backend.
-                    dsCohort.canCommit().get();
-                    fail("Exception should be raised.");
-                } catch (Exception e) {
-                    assertSame(FAILED_CAN_COMMIT, Throwables.getRootCause(e));
+                try (final DistributedDataStore dataStore =
+                        setupDistributedDataStore("transactionIntegrationTest", "test-1")) {
+                    dataStore.registerCommitCohort(TEST_ID, failedCohort);
+                    Thread.sleep(1000); // Registration is asynchronous
+
+                    DOMStoreWriteTransaction writeTx = dataStore.newWriteOnlyTransaction();
+                    writeTx.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
+                    DOMStoreThreePhaseCommitCohort dsCohort = writeTx.ready();
+                    try {
+                        // FIXME: Weird thing is that invoking canCommit on front-end invokes also
+                        // preCommit on backend.
+                        dsCohort.canCommit().get();
+                        fail("Exception should be raised.");
+                    } catch (Exception e) {
+                        assertSame(FAILED_CAN_COMMIT, Throwables.getRootCause(e));
+                    }
                 }
-                cleanup(dataStore);
             }
         };
     }
 
     /**
-     *
      * FIXME: Weird thing is that invoking canCommit on front-end invokes also preCommit on backend
      * so we can not test abort after can commit.
-     *
      */
     @Test
     @Ignore
@@ -160,19 +157,19 @@ public class DataTreeCohortIntegrationTest {
         Mockito.doReturn(ThreePhaseCommitStep.NOOP_ABORT_FUTURE).when(stepToAbort).abort();
         new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
             {
-                final DistributedDataStore dataStore =
-                        setupDistributedDataStore("transactionIntegrationTest", "test-1");
-                dataStore.registerCommitCohort(TEST_ID, cohortToAbort);
-                Thread.sleep(1000); // Registration is asynchronous
-
-                DOMStoreWriteTransaction writeTx = dataStore.newWriteOnlyTransaction();
-                writeTx.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
-                DOMStoreThreePhaseCommitCohort dsCohort = writeTx.ready();
-
-                dsCohort.canCommit().get();
-                dsCohort.abort().get();
-                Mockito.verify(stepToAbort, Mockito.times(1)).abort();
-                cleanup(dataStore);
+                try (final DistributedDataStore dataStore =
+                        setupDistributedDataStore("transactionIntegrationTest", "test-1")) {
+                    dataStore.registerCommitCohort(TEST_ID, cohortToAbort);
+                    Thread.sleep(1000); // Registration is asynchronous
+
+                    DOMStoreWriteTransaction writeTx = dataStore.newWriteOnlyTransaction();
+                    writeTx.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
+                    DOMStoreThreePhaseCommitCohort dsCohort = writeTx.ready();
+
+                    dsCohort.canCommit().get();
+                    dsCohort.abort().get();
+                    Mockito.verify(stepToAbort, Mockito.times(1)).abort();
+                }
             }
         };
     }