2 * Copyright (c) 2014, 2015 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.controller.cluster.datastore;
11 import akka.actor.ActorRef;
12 import akka.actor.Props;
13 import akka.testkit.TestActorRef;
14 import java.util.concurrent.TimeUnit;
15 import org.junit.Test;
16 import org.opendaylight.controller.cluster.access.concepts.MemberName;
17 import org.opendaylight.controller.cluster.datastore.identifiers.ShardIdentifier;
18 import org.opendaylight.controller.cluster.datastore.jmx.mbeans.shard.ShardStats;
19 import org.opendaylight.controller.cluster.datastore.messages.DataExists;
20 import org.opendaylight.controller.cluster.datastore.messages.ReadData;
21 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
22 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
23 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
24 import org.opendaylight.yangtools.yang.data.api.schema.tree.TreeType;
25 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
26 import scala.concurrent.Await;
27 import scala.concurrent.Future;
28 import scala.concurrent.duration.Duration;
31 * Covers negative test cases
33 * @author Basheeruddin Ahmed <syedbahm@cisco.com>
35 public class ShardTransactionFailureTest extends AbstractActorTest {
36 private static final SchemaContext testSchemaContext =
37 TestModel.createTestContext();
38 private static final TransactionType RO = TransactionType.READ_ONLY;
39 private static final TransactionType RW = TransactionType.READ_WRITE;
40 private static final TransactionType WO = TransactionType.WRITE_ONLY;
42 private static final ShardDataTree store = new ShardDataTree(testSchemaContext, TreeType.OPERATIONAL);
44 private static final ShardIdentifier SHARD_IDENTIFIER =
45 ShardIdentifier.builder().memberName(MemberName.forName("member-1"))
46 .shardName("inventory").type("operational").build();
48 private final DatastoreContext datastoreContext = DatastoreContext.newBuilder().build();
50 private final ShardStats shardStats = new ShardStats(SHARD_IDENTIFIER.toString(), "DataStore");
52 private ActorRef createShard(){
53 ActorRef shard = getSystem().actorOf(Shard.builder().id(SHARD_IDENTIFIER).datastoreContext(datastoreContext).
54 schemaContext(TestModel.createTestContext()).props());
55 ShardTestKit.waitUntilLeader(shard);
59 @Test(expected = ReadFailedException.class)
60 public void testNegativeReadWithReadOnlyTransactionClosed() throws Throwable {
62 final ActorRef shard = createShard();
63 final Props props = ShardTransaction.props(RO, store.newReadOnlyTransaction("test-txn", null), shard,
64 datastoreContext, shardStats, "txn");
66 final TestActorRef<ShardTransaction> subject = TestActorRef.create(getSystem(), props,
67 "testNegativeReadWithReadOnlyTransactionClosed");
69 Future<Object> future = akka.pattern.Patterns.ask(subject,
70 new ReadData(YangInstanceIdentifier.EMPTY, DataStoreVersions.CURRENT_VERSION), 3000);
71 Await.result(future, Duration.create(3, TimeUnit.SECONDS));
73 subject.underlyingActor().getDOMStoreTransaction().abort();
75 future = akka.pattern.Patterns.ask(subject, new ReadData(YangInstanceIdentifier.EMPTY,
76 DataStoreVersions.CURRENT_VERSION), 3000);
77 Await.result(future, Duration.create(3, TimeUnit.SECONDS));
81 @Test(expected = ReadFailedException.class)
82 public void testNegativeReadWithReadWriteTransactionClosed() throws Throwable {
84 final ActorRef shard = createShard();
85 final Props props = ShardTransaction.props(RW, store.newReadWriteTransaction("test-txn", null), shard,
86 datastoreContext, shardStats, "txn");
88 final TestActorRef<ShardTransaction> subject = TestActorRef.create(getSystem(), props,
89 "testNegativeReadWithReadWriteTransactionClosed");
91 Future<Object> future = akka.pattern.Patterns.ask(subject,
92 new ReadData(YangInstanceIdentifier.EMPTY, DataStoreVersions.CURRENT_VERSION), 3000);
93 Await.result(future, Duration.create(3, TimeUnit.SECONDS));
95 subject.underlyingActor().getDOMStoreTransaction().abort();
97 future = akka.pattern.Patterns.ask(subject, new ReadData(YangInstanceIdentifier.EMPTY,
98 DataStoreVersions.CURRENT_VERSION), 3000);
99 Await.result(future, Duration.create(3, TimeUnit.SECONDS));
102 @Test(expected = ReadFailedException.class)
103 public void testNegativeExistsWithReadWriteTransactionClosed() throws Throwable {
105 final ActorRef shard = createShard();
106 final Props props = ShardTransaction.props(RW, store.newReadWriteTransaction("test-txn", null), shard,
107 datastoreContext, shardStats, "txn");
109 final TestActorRef<ShardTransaction> subject = TestActorRef.create(getSystem(), props,
110 "testNegativeExistsWithReadWriteTransactionClosed");
112 Future<Object> future = akka.pattern.Patterns.ask(subject,
113 new DataExists(YangInstanceIdentifier.EMPTY, DataStoreVersions.CURRENT_VERSION), 3000);
114 Await.result(future, Duration.create(3, TimeUnit.SECONDS));
116 subject.underlyingActor().getDOMStoreTransaction().abort();
118 future = akka.pattern.Patterns.ask(subject,
119 new DataExists(YangInstanceIdentifier.EMPTY, DataStoreVersions.CURRENT_VERSION), 3000);
120 Await.result(future, Duration.create(3, TimeUnit.SECONDS));