X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-distributed-datastore%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fcluster%2Fdatastore%2FShardTransactionFailureTest.java;h=e45389f5f36fbd151a24ae9e4d5a02a4995a2772;hb=9c34ce103df5efac991297dc25a64c9b8d6019f3;hp=16b73040a5b6e5375a24570fb5b1617240eadb04;hpb=51e91f6bdcc88c5aa96f956e516d31dbb5e5d5e0;p=controller.git diff --git a/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/ShardTransactionFailureTest.java b/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/ShardTransactionFailureTest.java index 16b73040a5..e45389f5f3 100644 --- a/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/ShardTransactionFailureTest.java +++ b/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/ShardTransactionFailureTest.java @@ -12,59 +12,60 @@ package org.opendaylight.controller.cluster.datastore; import akka.actor.ActorRef; import akka.actor.Props; +import akka.pattern.AskTimeoutException; import akka.testkit.TestActorRef; -import com.google.common.util.concurrent.ListeningExecutorService; -import com.google.common.util.concurrent.MoreExecutors; +import java.util.Collections; +import java.util.concurrent.TimeUnit; import org.junit.Test; +import org.opendaylight.controller.cluster.datastore.TransactionProxy.TransactionType; import org.opendaylight.controller.cluster.datastore.identifiers.ShardIdentifier; +import org.opendaylight.controller.cluster.datastore.jmx.mbeans.shard.ShardStats; +import org.opendaylight.controller.cluster.datastore.node.utils.serialization.NormalizedNodeSerializer; import org.opendaylight.controller.md.cluster.datastore.model.TestModel; import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException; -import org.opendaylight.controller.md.sal.dom.store.impl.InMemoryDOMDataStore; import org.opendaylight.controller.protobuff.messages.common.NormalizedNodeMessages; import org.opendaylight.controller.protobuff.messages.transaction.ShardTransactionMessages; +import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier; +import org.opendaylight.yangtools.yang.data.impl.schema.Builders; import org.opendaylight.yangtools.yang.model.api.SchemaContext; import scala.concurrent.Await; import scala.concurrent.Future; import scala.concurrent.duration.Duration; -import java.util.Collections; - -import static org.junit.Assert.assertTrue; - /** * Covers negative test cases * * @author Basheeruddin Ahmed */ public class ShardTransactionFailureTest extends AbstractActorTest { - private static ListeningExecutorService storeExecutor = - MoreExecutors.listeningDecorator(MoreExecutors.sameThreadExecutor()); - - private static final InMemoryDOMDataStore store = - new InMemoryDOMDataStore("OPER", storeExecutor, - MoreExecutors.sameThreadExecutor()); - private static final SchemaContext testSchemaContext = - TestModel.createTestContext(); + TestModel.createTestContext(); + private static final TransactionType RO = TransactionType.READ_ONLY; + private static final TransactionType RW = TransactionType.READ_WRITE; + private static final TransactionType WO = TransactionType.WRITE_ONLY; + + private static final ShardDataTree store = new ShardDataTree(testSchemaContext); private static final ShardIdentifier SHARD_IDENTIFIER = ShardIdentifier.builder().memberName("member-1") .shardName("inventory").type("operational").build(); - static { - store.onGlobalContextUpdated(testSchemaContext); - } + private final DatastoreContext datastoreContext = DatastoreContext.newBuilder().build(); + private final ShardStats shardStats = new ShardStats(SHARD_IDENTIFIER.toString(), "DataStore"); + + private ActorRef createShard(){ + return getSystem().actorOf(Shard.props(SHARD_IDENTIFIER, Collections.emptyMap(), datastoreContext, + TestModel.createTestContext())); + } @Test(expected = ReadFailedException.class) public void testNegativeReadWithReadOnlyTransactionClosed() throws Throwable { - final ActorRef shard = - getSystem().actorOf(Shard.props(SHARD_IDENTIFIER, Collections.EMPTY_MAP, null)); - final Props props = - ShardTransaction.props(store.newReadOnlyTransaction(), shard, - TestModel.createTestContext()); + final ActorRef shard = createShard(); + final Props props = ShardTransaction.props(RO, store.newReadOnlyTransaction("test-txn", null), shard, + datastoreContext, shardStats, "txn", DataStoreVersions.CURRENT_VERSION); final TestActorRef subject = TestActorRef .create(getSystem(), props, @@ -78,32 +79,26 @@ public class ShardTransactionFailureTest extends AbstractActorTest { ).build(); Future future = akka.pattern.Patterns.ask(subject, readData, 3000); - assertTrue(future.isCompleted()); - Await.result(future, Duration.Zero()); + Await.result(future, Duration.create(3, TimeUnit.SECONDS)); - ((ShardReadTransaction) subject.underlyingActor()) - .forUnitTestOnlyExplicitTransactionClose(); + subject.underlyingActor().getDOMStoreTransaction().abort(); future = akka.pattern.Patterns.ask(subject, readData, 3000); - Await.result(future, Duration.Zero()); - - + Await.result(future, Duration.create(3, TimeUnit.SECONDS)); } @Test(expected = ReadFailedException.class) - public void testNegativeReadWithReadWriteOnlyTransactionClosed() + public void testNegativeReadWithReadWriteTransactionClosed() throws Throwable { - final ActorRef shard = - getSystem().actorOf(Shard.props(SHARD_IDENTIFIER, Collections.EMPTY_MAP,null)); - final Props props = - ShardTransaction.props(store.newReadWriteTransaction(), shard, - TestModel.createTestContext()); + final ActorRef shard = createShard(); + final Props props = ShardTransaction.props(RW, store.newReadWriteTransaction("test-txn", null), shard, + datastoreContext, shardStats, "txn", DataStoreVersions.CURRENT_VERSION); final TestActorRef subject = TestActorRef .create(getSystem(), props, - "testNegativeReadWithReadWriteOnlyTransactionClosed"); + "testNegativeReadWithReadWriteTransactionClosed"); ShardTransactionMessages.ReadData readData = ShardTransactionMessages.ReadData.newBuilder() @@ -111,33 +106,28 @@ public class ShardTransactionFailureTest extends AbstractActorTest { NormalizedNodeMessages.InstanceIdentifier.newBuilder() .build() ).build(); + Future future = akka.pattern.Patterns.ask(subject, readData, 3000); - assertTrue(future.isCompleted()); - Await.result(future, Duration.Zero()); + Await.result(future, Duration.create(3, TimeUnit.SECONDS)); - ((ShardReadWriteTransaction) subject.underlyingActor()) - .forUnitTestOnlyExplicitTransactionClose(); + subject.underlyingActor().getDOMStoreTransaction().abort(); future = akka.pattern.Patterns.ask(subject, readData, 3000); - Await.result(future, Duration.Zero()); - - + Await.result(future, Duration.create(3, TimeUnit.SECONDS)); } @Test(expected = ReadFailedException.class) - public void testNegativeExistsWithReadWriteOnlyTransactionClosed() + public void testNegativeExistsWithReadWriteTransactionClosed() throws Throwable { - final ActorRef shard = - getSystem().actorOf(Shard.props(SHARD_IDENTIFIER, Collections.EMPTY_MAP,null)); - final Props props = - ShardTransaction.props(store.newReadWriteTransaction(), shard, - TestModel.createTestContext()); + final ActorRef shard = createShard(); + final Props props = ShardTransaction.props(RW, store.newReadWriteTransaction("test-txn", null), shard, + datastoreContext, shardStats, "txn", DataStoreVersions.CURRENT_VERSION); final TestActorRef subject = TestActorRef .create(getSystem(), props, - "testNegativeExistsWithReadWriteOnlyTransactionClosed"); + "testNegativeExistsWithReadWriteTransactionClosed"); ShardTransactionMessages.DataExists dataExists = ShardTransactionMessages.DataExists.newBuilder() @@ -148,27 +138,21 @@ public class ShardTransactionFailureTest extends AbstractActorTest { Future future = akka.pattern.Patterns.ask(subject, dataExists, 3000); - assertTrue(future.isCompleted()); - Await.result(future, Duration.Zero()); + Await.result(future, Duration.create(3, TimeUnit.SECONDS)); - ((ShardReadWriteTransaction) subject.underlyingActor()) - .forUnitTestOnlyExplicitTransactionClose(); + subject.underlyingActor().getDOMStoreTransaction().abort(); future = akka.pattern.Patterns.ask(subject, dataExists, 3000); - Await.result(future, Duration.Zero()); - - + Await.result(future, Duration.create(3, TimeUnit.SECONDS)); } - @Test(expected = IllegalStateException.class) + @Test(expected = AskTimeoutException.class) public void testNegativeWriteWithTransactionReady() throws Exception { - final ActorRef shard = - getSystem().actorOf(Shard.props(SHARD_IDENTIFIER, Collections.EMPTY_MAP,null)); - final Props props = - ShardTransaction.props(store.newWriteOnlyTransaction(), shard, - TestModel.createTestContext()); + final ActorRef shard = createShard(); + final Props props = ShardTransaction.props(WO, store.newReadWriteTransaction("test-txn", null), shard, + datastoreContext, shardStats, "txn", DataStoreVersions.CURRENT_VERSION); final TestActorRef subject = TestActorRef .create(getSystem(), props, @@ -179,35 +163,28 @@ public class ShardTransactionFailureTest extends AbstractActorTest { Future future = akka.pattern.Patterns.ask(subject, readyTransaction, 3000); - assertTrue(future.isCompleted()); - Await.result(future, Duration.Zero()); + Await.result(future, Duration.create(3, TimeUnit.SECONDS)); ShardTransactionMessages.WriteData writeData = ShardTransactionMessages.WriteData.newBuilder() .setInstanceIdentifierPathArguments( NormalizedNodeMessages.InstanceIdentifier.newBuilder() .build()).setNormalizedNode( - NormalizedNodeMessages.Node.newBuilder().build() + buildNormalizedNode() ).build(); future = akka.pattern.Patterns.ask(subject, writeData, 3000); - assertTrue(future.isCompleted()); - Await.result(future, Duration.Zero()); - - + Await.result(future, Duration.create(3, TimeUnit.SECONDS)); } - - @Test(expected = IllegalStateException.class) + @Test(expected = AskTimeoutException.class) public void testNegativeReadWriteWithTransactionReady() throws Exception { - final ActorRef shard = - getSystem().actorOf(Shard.props(SHARD_IDENTIFIER, Collections.EMPTY_MAP,null)); - final Props props = - ShardTransaction.props(store.newReadWriteTransaction(), shard, - TestModel.createTestContext()); + final ActorRef shard = createShard(); + final Props props = ShardTransaction.props(RW, store.newReadWriteTransaction("test-txn", null), shard, + datastoreContext, shardStats, "txn", DataStoreVersions.CURRENT_VERSION); final TestActorRef subject = TestActorRef .create(getSystem(), props, @@ -218,34 +195,33 @@ public class ShardTransactionFailureTest extends AbstractActorTest { Future future = akka.pattern.Patterns.ask(subject, readyTransaction, 3000); - assertTrue(future.isCompleted()); - Await.result(future, Duration.Zero()); + Await.result(future, Duration.create(3, TimeUnit.SECONDS)); ShardTransactionMessages.WriteData writeData = ShardTransactionMessages.WriteData.newBuilder() .setInstanceIdentifierPathArguments( NormalizedNodeMessages.InstanceIdentifier.newBuilder() - .build()).setNormalizedNode( - NormalizedNodeMessages.Node.newBuilder().build() - - ).build(); + .build() + ) + .setNormalizedNode(buildNormalizedNode()) + .build(); future = akka.pattern.Patterns.ask(subject, writeData, 3000); - assertTrue(future.isCompleted()); - Await.result(future, Duration.Zero()); - + Await.result(future, Duration.create(3, TimeUnit.SECONDS)); + } + private NormalizedNodeMessages.Node buildNormalizedNode() { + return NormalizedNodeSerializer + .serialize(Builders.containerBuilder().withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TestModel.TEST_QNAME)).build()); } - @Test(expected = IllegalStateException.class) + @Test(expected = AskTimeoutException.class) public void testNegativeMergeTransactionReady() throws Exception { - final ActorRef shard = - getSystem().actorOf(Shard.props(SHARD_IDENTIFIER, Collections.EMPTY_MAP,null)); - final Props props = - ShardTransaction.props(store.newReadWriteTransaction(), shard, - TestModel.createTestContext()); + final ActorRef shard = createShard(); + final Props props = ShardTransaction.props(RW, store.newReadWriteTransaction("test-txn", null), shard, + datastoreContext, shardStats, "txn", DataStoreVersions.CURRENT_VERSION); final TestActorRef subject = TestActorRef .create(getSystem(), props, "testNegativeMergeTransactionReady"); @@ -255,35 +231,29 @@ public class ShardTransactionFailureTest extends AbstractActorTest { Future future = akka.pattern.Patterns.ask(subject, readyTransaction, 3000); - assertTrue(future.isCompleted()); - Await.result(future, Duration.Zero()); + Await.result(future, Duration.create(3, TimeUnit.SECONDS)); ShardTransactionMessages.MergeData mergeData = ShardTransactionMessages.MergeData.newBuilder() .setInstanceIdentifierPathArguments( NormalizedNodeMessages.InstanceIdentifier.newBuilder() .build()).setNormalizedNode( - NormalizedNodeMessages.Node.newBuilder().build() + buildNormalizedNode() ).build(); future = akka.pattern.Patterns.ask(subject, mergeData, 3000); - assertTrue(future.isCompleted()); - Await.result(future, Duration.Zero()); - - + Await.result(future, Duration.create(3, TimeUnit.SECONDS)); } - @Test(expected = IllegalStateException.class) + @Test(expected = AskTimeoutException.class) public void testNegativeDeleteDataWhenTransactionReady() throws Exception { - final ActorRef shard = - getSystem().actorOf(Shard.props(SHARD_IDENTIFIER, Collections.EMPTY_MAP,null)); - final Props props = - ShardTransaction.props(store.newReadWriteTransaction(), shard, - TestModel.createTestContext()); + final ActorRef shard = createShard(); + final Props props = ShardTransaction.props(RW, store.newReadWriteTransaction("test-txn", null), shard, + datastoreContext, shardStats, "txn", DataStoreVersions.CURRENT_VERSION); final TestActorRef subject = TestActorRef .create(getSystem(), props, @@ -294,8 +264,7 @@ public class ShardTransactionFailureTest extends AbstractActorTest { Future future = akka.pattern.Patterns.ask(subject, readyTransaction, 3000); - assertTrue(future.isCompleted()); - Await.result(future, Duration.Zero()); + Await.result(future, Duration.create(3, TimeUnit.SECONDS)); ShardTransactionMessages.DeleteData deleteData = ShardTransactionMessages.DeleteData.newBuilder() @@ -304,9 +273,6 @@ public class ShardTransactionFailureTest extends AbstractActorTest { .build()).build(); future = akka.pattern.Patterns.ask(subject, deleteData, 3000); - assertTrue(future.isCompleted()); - Await.result(future, Duration.Zero()); - - + Await.result(future, Duration.create(3, TimeUnit.SECONDS)); } }