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.datastore.identifiers.ShardIdentifier;
17 import org.opendaylight.controller.cluster.datastore.jmx.mbeans.shard.ShardStats;
18 import org.opendaylight.controller.cluster.datastore.messages.DataExists;
19 import org.opendaylight.controller.cluster.datastore.messages.ReadData;
20 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
21 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
22 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
23 import org.opendaylight.yangtools.yang.data.api.schema.tree.TreeType;
24 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
25 import scala.concurrent.Await;
26 import scala.concurrent.Future;
27 import scala.concurrent.duration.Duration;
30 * Covers negative test cases
32 * @author Basheeruddin Ahmed <syedbahm@cisco.com>
34 public class ShardTransactionFailureTest extends AbstractActorTest {
35 private static final SchemaContext testSchemaContext =
36 TestModel.createTestContext();
37 private static final TransactionType RO = TransactionType.READ_ONLY;
38 private static final TransactionType RW = TransactionType.READ_WRITE;
39 private static final TransactionType WO = TransactionType.WRITE_ONLY;
41 private static final ShardDataTree store = new ShardDataTree(testSchemaContext, TreeType.OPERATIONAL);
43 private static final ShardIdentifier SHARD_IDENTIFIER =
44 ShardIdentifier.builder().memberName("member-1")
45 .shardName("inventory").type("operational").build();
47 private final DatastoreContext datastoreContext = DatastoreContext.newBuilder().build();
49 private final ShardStats shardStats = new ShardStats(SHARD_IDENTIFIER.toString(), "DataStore");
51 private ActorRef createShard(){
52 ActorRef shard = getSystem().actorOf(Shard.builder().id(SHARD_IDENTIFIER).datastoreContext(datastoreContext).
53 schemaContext(TestModel.createTestContext()).props());
54 ShardTestKit.waitUntilLeader(shard);
58 @Test(expected = ReadFailedException.class)
59 public void testNegativeReadWithReadOnlyTransactionClosed() throws Throwable {
61 final ActorRef shard = createShard();
62 final Props props = ShardTransaction.props(RO, store.newReadOnlyTransaction("test-txn", null), shard,
63 datastoreContext, shardStats, "txn");
65 final TestActorRef<ShardTransaction> subject = TestActorRef.create(getSystem(), props,
66 "testNegativeReadWithReadOnlyTransactionClosed");
68 Future<Object> future = akka.pattern.Patterns.ask(subject,
69 new ReadData(YangInstanceIdentifier.EMPTY, DataStoreVersions.CURRENT_VERSION), 3000);
70 Await.result(future, Duration.create(3, TimeUnit.SECONDS));
72 subject.underlyingActor().getDOMStoreTransaction().abort();
74 future = akka.pattern.Patterns.ask(subject, new ReadData(YangInstanceIdentifier.EMPTY,
75 DataStoreVersions.CURRENT_VERSION), 3000);
76 Await.result(future, Duration.create(3, TimeUnit.SECONDS));
80 @Test(expected = ReadFailedException.class)
81 public void testNegativeReadWithReadWriteTransactionClosed() throws Throwable {
83 final ActorRef shard = createShard();
84 final Props props = ShardTransaction.props(RW, store.newReadWriteTransaction("test-txn", null), shard,
85 datastoreContext, shardStats, "txn");
87 final TestActorRef<ShardTransaction> subject = TestActorRef.create(getSystem(), props,
88 "testNegativeReadWithReadWriteTransactionClosed");
90 Future<Object> future = akka.pattern.Patterns.ask(subject,
91 new ReadData(YangInstanceIdentifier.EMPTY, DataStoreVersions.CURRENT_VERSION), 3000);
92 Await.result(future, Duration.create(3, TimeUnit.SECONDS));
94 subject.underlyingActor().getDOMStoreTransaction().abort();
96 future = akka.pattern.Patterns.ask(subject, new ReadData(YangInstanceIdentifier.EMPTY,
97 DataStoreVersions.CURRENT_VERSION), 3000);
98 Await.result(future, Duration.create(3, TimeUnit.SECONDS));
101 @Test(expected = ReadFailedException.class)
102 public void testNegativeExistsWithReadWriteTransactionClosed() throws Throwable {
104 final ActorRef shard = createShard();
105 final Props props = ShardTransaction.props(RW, store.newReadWriteTransaction("test-txn", null), shard,
106 datastoreContext, shardStats, "txn");
108 final TestActorRef<ShardTransaction> subject = TestActorRef.create(getSystem(), props,
109 "testNegativeExistsWithReadWriteTransactionClosed");
111 Future<Object> future = akka.pattern.Patterns.ask(subject,
112 new DataExists(YangInstanceIdentifier.EMPTY, DataStoreVersions.CURRENT_VERSION), 3000);
113 Await.result(future, Duration.create(3, TimeUnit.SECONDS));
115 subject.underlyingActor().getDOMStoreTransaction().abort();
117 future = akka.pattern.Patterns.ask(subject,
118 new DataExists(YangInstanceIdentifier.EMPTY, DataStoreVersions.CURRENT_VERSION), 3000);
119 Await.result(future, Duration.create(3, TimeUnit.SECONDS));