X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-distributed-datastore%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fcluster%2Fdatastore%2FTransactionChainProxyTest.java;h=37cdc4a019d8444ff415fddf21b73e287512056c;hp=acba775445879d5a1305969cc099fc6fd35b2cb8;hb=abaef4a5ae37f27542155457fe7306a4662b1eeb;hpb=00cc355c0c58e999ffebd531bca3a507e150e441 diff --git a/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/TransactionChainProxyTest.java b/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/TransactionChainProxyTest.java index acba775445..37cdc4a019 100644 --- a/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/TransactionChainProxyTest.java +++ b/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/TransactionChainProxyTest.java @@ -1,117 +1,117 @@ /* + * Copyright (c) 2014, 2015 Cisco Systems, Inc. and others. All rights reserved. * - * Copyright (c) 2014 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 - * + * 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.controller.cluster.datastore; -import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; -import static org.mockito.Matchers.anyObject; -import static org.mockito.Matchers.eq; -import static org.mockito.Matchers.isA; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.ArgumentMatchers.isA; import static org.mockito.Mockito.doReturn; -import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; import static org.mockito.Mockito.timeout; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; -import static org.opendaylight.controller.cluster.datastore.TransactionProxy.TransactionType.READ_WRITE; -import static org.opendaylight.controller.cluster.datastore.TransactionProxy.TransactionType.WRITE_ONLY; +import static org.opendaylight.controller.cluster.datastore.TransactionType.READ_WRITE; +import static org.opendaylight.controller.cluster.datastore.TransactionType.WRITE_ONLY; + import akka.actor.ActorRef; +import akka.util.Timeout; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicReference; +import java.util.function.Function; import org.junit.Assert; import org.junit.Test; +import org.opendaylight.controller.cluster.access.concepts.LocalHistoryIdentifier; import org.opendaylight.controller.cluster.datastore.messages.BatchedModifications; -import org.opendaylight.controller.cluster.datastore.messages.BatchedModificationsReply; -import org.opendaylight.controller.cluster.datastore.messages.ReadyTransaction; import org.opendaylight.controller.cluster.datastore.modification.WriteModification; import org.opendaylight.controller.cluster.datastore.shardstrategy.DefaultShardStrategy; -import org.opendaylight.controller.cluster.datastore.utils.ActorContext; import org.opendaylight.controller.md.cluster.datastore.model.TestModel; -import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadTransaction; -import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadWriteTransaction; -import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransaction; -import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction; +import org.opendaylight.mdsal.dom.spi.store.DOMStoreReadTransaction; +import org.opendaylight.mdsal.dom.spi.store.DOMStoreReadWriteTransaction; +import org.opendaylight.mdsal.dom.spi.store.DOMStoreTransaction; +import org.opendaylight.mdsal.dom.spi.store.DOMStoreWriteTransaction; import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode; import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes; import scala.concurrent.Promise; public class TransactionChainProxyTest extends AbstractTransactionProxyTest { + private LocalHistoryIdentifier historyId; + + @Override + public void setUp() { + super.setUp(); + historyId = MockIdentifiers.historyIdentifier(TransactionChainProxyTest.class, memberName); + } @SuppressWarnings("resource") @Test - public void testNewReadOnlyTransaction() throws Exception { + public void testNewReadOnlyTransaction() { - DOMStoreTransaction dst = new TransactionChainProxy(mockActorContext).newReadOnlyTransaction(); - Assert.assertTrue(dst instanceof DOMStoreReadTransaction); + DOMStoreTransaction dst = new TransactionChainProxy(mockComponentFactory, historyId).newReadOnlyTransaction(); + Assert.assertTrue(dst instanceof DOMStoreReadTransaction); } @SuppressWarnings("resource") @Test - public void testNewReadWriteTransaction() throws Exception { - DOMStoreTransaction dst = new TransactionChainProxy(mockActorContext).newReadWriteTransaction(); + public void testNewReadWriteTransaction() { + DOMStoreTransaction dst = new TransactionChainProxy(mockComponentFactory, historyId).newReadWriteTransaction(); Assert.assertTrue(dst instanceof DOMStoreReadWriteTransaction); } @SuppressWarnings("resource") @Test - public void testNewWriteOnlyTransaction() throws Exception { - DOMStoreTransaction dst = new TransactionChainProxy(mockActorContext).newWriteOnlyTransaction(); + public void testNewWriteOnlyTransaction() { + DOMStoreTransaction dst = new TransactionChainProxy(mockComponentFactory, historyId).newWriteOnlyTransaction(); Assert.assertTrue(dst instanceof DOMStoreWriteTransaction); } + @SuppressWarnings("unchecked") @Test - public void testClose() throws Exception { - new TransactionChainProxy(mockActorContext).close(); + public void testClose() { + new TransactionChainProxy(mockComponentFactory, historyId).close(); - verify(mockActorContext, times(1)).broadcast(anyObject()); + verify(mockActorContext, times(1)).broadcast(any(Function.class), any(Class.class)); } @Test - public void testTransactionChainsHaveUniqueId(){ - TransactionChainProxy one = new TransactionChainProxy(mock(ActorContext.class)); - TransactionChainProxy two = new TransactionChainProxy(mock(ActorContext.class)); - - Assert.assertNotEquals(one.getTransactionChainId(), two.getTransactionChainId()); - } + public void testRateLimitingUsedInReadWriteTxCreation() { + try (TransactionChainProxy txChainProxy = new TransactionChainProxy(mockComponentFactory, historyId)) { - @Test - public void testRateLimitingUsedInReadWriteTxCreation(){ - TransactionChainProxy txChainProxy = new TransactionChainProxy(mockActorContext); + txChainProxy.newReadWriteTransaction(); - txChainProxy.newReadWriteTransaction(); - - verify(mockActorContext, times(1)).acquireTxCreationPermit(); + verify(mockActorContext, times(1)).acquireTxCreationPermit(); + } } @Test - public void testRateLimitingUsedInWriteOnlyTxCreation(){ - TransactionChainProxy txChainProxy = new TransactionChainProxy(mockActorContext); + public void testRateLimitingUsedInWriteOnlyTxCreation() { + try (TransactionChainProxy txChainProxy = new TransactionChainProxy(mockComponentFactory, historyId)) { - txChainProxy.newWriteOnlyTransaction(); + txChainProxy.newWriteOnlyTransaction(); - verify(mockActorContext, times(1)).acquireTxCreationPermit(); + verify(mockActorContext, times(1)).acquireTxCreationPermit(); + } } - @Test - public void testRateLimitingNotUsedInReadOnlyTxCreation(){ - TransactionChainProxy txChainProxy = new TransactionChainProxy(mockActorContext); + public void testRateLimitingNotUsedInReadOnlyTxCreation() { + try (TransactionChainProxy txChainProxy = new TransactionChainProxy(mockComponentFactory, historyId)) { - txChainProxy.newReadOnlyTransaction(); + txChainProxy.newReadOnlyTransaction(); - verify(mockActorContext, times(0)).acquireTxCreationPermit(); + verify(mockActorContext, times(0)).acquireTxCreationPermit(); + } } /** @@ -119,41 +119,40 @@ public class TransactionChainProxyTest extends AbstractTransactionProxyTest { * initiated until the first one completes its read future. */ @Test + @SuppressWarnings("checkstyle:IllegalCatch") public void testChainedWriteOnlyTransactions() throws Exception { dataStoreContextBuilder.writeOnlyTransactionOptimizationsEnabled(true); - TransactionChainProxy txChainProxy = new TransactionChainProxy(mockActorContext); + try (TransactionChainProxy txChainProxy = new TransactionChainProxy(mockComponentFactory, historyId)) { - ActorRef txActorRef1 = setupActorContextWithoutInitialCreateTransaction(getSystem()); + ActorRef txActorRef1 = setupActorContextWithoutInitialCreateTransaction(getSystem()); - Promise batchedReplyPromise1 = akka.dispatch.Futures.promise(); - doReturn(batchedReplyPromise1.future()).when(mockActorContext).executeOperationAsync( - eq(actorSelection(txActorRef1)), isA(BatchedModifications.class)); + Promise batchedReplyPromise1 = akka.dispatch.Futures.promise(); + doReturn(batchedReplyPromise1.future()).when(mockActorContext).executeOperationAsync( + eq(actorSelection(txActorRef1)), isA(BatchedModifications.class), any(Timeout.class)); - DOMStoreWriteTransaction writeTx1 = txChainProxy.newWriteOnlyTransaction(); + DOMStoreWriteTransaction writeTx1 = txChainProxy.newWriteOnlyTransaction(); - NormalizedNode writeNode1 = ImmutableNodes.containerNode(TestModel.TEST_QNAME); - writeTx1.write(TestModel.TEST_PATH, writeNode1); + NormalizedNode writeNode1 = ImmutableNodes.containerNode(TestModel.TEST_QNAME); + writeTx1.write(TestModel.TEST_PATH, writeNode1); - writeTx1.ready(); + writeTx1.ready(); - verify(mockActorContext, times(1)).findPrimaryShardAsync(eq(DefaultShardStrategy.DEFAULT_SHARD)); + verify(mockActorContext, times(1)).findPrimaryShardAsync(eq(DefaultShardStrategy.DEFAULT_SHARD)); - verifyOneBatchedModification(txActorRef1, new WriteModification(TestModel.TEST_PATH, writeNode1), true); + verifyOneBatchedModification(txActorRef1, new WriteModification(TestModel.TEST_PATH, writeNode1), true); - ActorRef txActorRef2 = setupActorContextWithoutInitialCreateTransaction(getSystem()); + ActorRef txActorRef2 = setupActorContextWithoutInitialCreateTransaction(getSystem()); - expectBatchedModifications(txActorRef2, 1); + expectBatchedModifications(txActorRef2, 1); - final NormalizedNode writeNode2 = ImmutableNodes.containerNode(TestModel.OUTER_LIST_QNAME); + final NormalizedNode writeNode2 = ImmutableNodes.containerNode(TestModel.OUTER_LIST_QNAME); - final DOMStoreWriteTransaction writeTx2 = txChainProxy.newWriteOnlyTransaction(); + final DOMStoreWriteTransaction writeTx2 = txChainProxy.newWriteOnlyTransaction(); - final AtomicReference caughtEx = new AtomicReference<>(); - final CountDownLatch write2Complete = new CountDownLatch(1); - new Thread() { - @Override - public void run() { + final AtomicReference caughtEx = new AtomicReference<>(); + final CountDownLatch write2Complete = new CountDownLatch(1); + new Thread(() -> { try { writeTx2.write(TestModel.OUTER_LIST_PATH, writeNode2); } catch (Exception e) { @@ -161,25 +160,26 @@ public class TransactionChainProxyTest extends AbstractTransactionProxyTest { } finally { write2Complete.countDown(); } - } - }.start(); + }).start(); - assertEquals("Tx 2 write should've completed", true, write2Complete.await(5, TimeUnit.SECONDS)); + assertTrue("Tx 2 write should've completed", write2Complete.await(5, TimeUnit.SECONDS)); - if(caughtEx.get() != null) { - throw caughtEx.get(); - } + if (caughtEx.get() != null) { + throw caughtEx.get(); + } - try { - verify(mockActorContext, times(1)).findPrimaryShardAsync(eq(DefaultShardStrategy.DEFAULT_SHARD)); - } catch (AssertionError e) { - fail("Tx 2 should not have initiated until the Tx 1's ready future completed"); - } + try { + verify(mockActorContext, times(1)).findPrimaryShardAsync(eq(DefaultShardStrategy.DEFAULT_SHARD)); + } catch (AssertionError e) { + fail("Tx 2 should not have initiated until the Tx 1's ready future completed"); + } - batchedReplyPromise1.success(new BatchedModificationsReply(1, txActorRef1.path().toString())); + batchedReplyPromise1.success(readyTxReply(txActorRef1.path().toString()).value().get().get()); - // Tx 2 should've proceeded to find the primary shard. - verify(mockActorContext, timeout(5000).times(2)).findPrimaryShardAsync(eq(DefaultShardStrategy.DEFAULT_SHARD)); + // Tx 2 should've proceeded to find the primary shard. + verify(mockActorContext, timeout(5000).times(2)).findPrimaryShardAsync( + eq(DefaultShardStrategy.DEFAULT_SHARD)); + } } /** @@ -187,43 +187,41 @@ public class TransactionChainProxyTest extends AbstractTransactionProxyTest { * initiated until the first one completes its read future. */ @Test + @SuppressWarnings("checkstyle:IllegalCatch") public void testChainedReadWriteTransactions() throws Exception { - TransactionChainProxy txChainProxy = new TransactionChainProxy(mockActorContext); + try (TransactionChainProxy txChainProxy = new TransactionChainProxy(mockComponentFactory, historyId)) { - ActorRef txActorRef1 = setupActorContextWithInitialCreateTransaction(getSystem(), READ_WRITE); + ActorRef txActorRef1 = setupActorContextWithInitialCreateTransaction(getSystem(), READ_WRITE); - expectBatchedModifications(txActorRef1, 1); + expectBatchedModifications(txActorRef1, 1); - Promise readyReplyPromise1 = akka.dispatch.Futures.promise(); - doReturn(readyReplyPromise1.future()).when(mockActorContext).executeOperationAsync( - eq(actorSelection(txActorRef1)), isA(ReadyTransaction.SERIALIZABLE_CLASS)); + Promise readyReplyPromise1 = akka.dispatch.Futures.promise(); + doReturn(readyReplyPromise1.future()).when(mockActorContext).executeOperationAsync( + eq(actorSelection(txActorRef1)), isA(BatchedModifications.class), any(Timeout.class)); - DOMStoreWriteTransaction writeTx1 = txChainProxy.newReadWriteTransaction(); + DOMStoreWriteTransaction writeTx1 = txChainProxy.newReadWriteTransaction(); - NormalizedNode writeNode1 = ImmutableNodes.containerNode(TestModel.TEST_QNAME); - writeTx1.write(TestModel.TEST_PATH, writeNode1); + NormalizedNode writeNode1 = ImmutableNodes.containerNode(TestModel.TEST_QNAME); + writeTx1.write(TestModel.TEST_PATH, writeNode1); - writeTx1.ready(); + writeTx1.ready(); - verifyOneBatchedModification(txActorRef1, new WriteModification(TestModel.TEST_PATH, writeNode1), false); + verifyOneBatchedModification(txActorRef1, new WriteModification(TestModel.TEST_PATH, writeNode1), true); - String tx2MemberName = "tx2MemberName"; - doReturn(tx2MemberName).when(mockActorContext).getCurrentMemberName(); - ActorRef shardActorRef2 = setupActorContextWithoutInitialCreateTransaction(getSystem()); - ActorRef txActorRef2 = setupActorContextWithInitialCreateTransaction(getSystem(), READ_WRITE, - DataStoreVersions.CURRENT_VERSION, tx2MemberName, shardActorRef2); + String tx2MemberName = "mock-member"; + ActorRef shardActorRef2 = setupActorContextWithoutInitialCreateTransaction(getSystem()); + ActorRef txActorRef2 = setupActorContextWithInitialCreateTransaction(getSystem(), READ_WRITE, + DataStoreVersions.CURRENT_VERSION, tx2MemberName, shardActorRef2); - expectBatchedModifications(txActorRef2, 1); + expectBatchedModifications(txActorRef2, 1); - final NormalizedNode writeNode2 = ImmutableNodes.containerNode(TestModel.OUTER_LIST_QNAME); + final NormalizedNode writeNode2 = ImmutableNodes.containerNode(TestModel.OUTER_LIST_QNAME); - final DOMStoreWriteTransaction writeTx2 = txChainProxy.newReadWriteTransaction(); + final DOMStoreWriteTransaction writeTx2 = txChainProxy.newReadWriteTransaction(); - final AtomicReference caughtEx = new AtomicReference<>(); - final CountDownLatch write2Complete = new CountDownLatch(1); - new Thread() { - @Override - public void run() { + final AtomicReference caughtEx = new AtomicReference<>(); + final CountDownLatch write2Complete = new CountDownLatch(1); + new Thread(() -> { try { writeTx2.write(TestModel.OUTER_LIST_PATH, writeNode2); } catch (Exception e) { @@ -231,43 +229,44 @@ public class TransactionChainProxyTest extends AbstractTransactionProxyTest { } finally { write2Complete.countDown(); } - } - }.start(); + }).start(); - assertEquals("Tx 2 write should've completed", true, write2Complete.await(5, TimeUnit.SECONDS)); + assertTrue("Tx 2 write should've completed", write2Complete.await(5, TimeUnit.SECONDS)); - if(caughtEx.get() != null) { - throw caughtEx.get(); - } + if (caughtEx.get() != null) { + throw caughtEx.get(); + } - try { - verify(mockActorContext, never()).executeOperationAsync(eq(getSystem().actorSelection(shardActorRef2.path())), - eqCreateTransaction(tx2MemberName, READ_WRITE)); - } catch (AssertionError e) { - fail("Tx 2 should not have initiated until the Tx 1's ready future completed"); - } + try { + verify(mockActorContext, never()).executeOperationAsync( + eq(getSystem().actorSelection(shardActorRef2.path())), + eqCreateTransaction(tx2MemberName, READ_WRITE)); + } catch (AssertionError e) { + fail("Tx 2 should not have initiated until the Tx 1's ready future completed"); + } - readyReplyPromise1.success(readySerializedTxReply(txActorRef1.path().toString()).value().get().get()); + readyReplyPromise1.success(readyTxReply(txActorRef1.path().toString()).value().get().get()); - verify(mockActorContext, timeout(5000)).executeOperationAsync(eq(getSystem().actorSelection(shardActorRef2.path())), - eqCreateTransaction(tx2MemberName, READ_WRITE)); + verify(mockActorContext, timeout(5000)).executeOperationAsync( + eq(getSystem().actorSelection(shardActorRef2.path())), + eqCreateTransaction(tx2MemberName, READ_WRITE), any(Timeout.class)); + } } - @Test(expected=IllegalStateException.class) - public void testChainedWriteTransactionsWithPreviousTxNotReady() throws Exception { + @Test(expected = IllegalStateException.class) + public void testChainedWriteTransactionsWithPreviousTxNotReady() { ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), WRITE_ONLY); expectBatchedModifications(actorRef, 1); - TransactionChainProxy txChainProxy = new TransactionChainProxy(mockActorContext); - - DOMStoreWriteTransaction writeTx1 = txChainProxy.newWriteOnlyTransaction(); + try (TransactionChainProxy txChainProxy = new TransactionChainProxy(mockComponentFactory, historyId)) { - NormalizedNode writeNode1 = ImmutableNodes.containerNode(TestModel.TEST_QNAME); - writeTx1.write(TestModel.TEST_PATH, writeNode1); + DOMStoreWriteTransaction writeTx1 = txChainProxy.newWriteOnlyTransaction(); - NormalizedNode writeNode2 = ImmutableNodes.containerNode(TestModel.OUTER_LIST_QNAME); + NormalizedNode writeNode1 = ImmutableNodes.containerNode(TestModel.TEST_QNAME); + writeTx1.write(TestModel.TEST_PATH, writeNode1); - txChainProxy.newWriteOnlyTransaction(); + txChainProxy.newWriteOnlyTransaction(); + } } }