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.mockito.Mockito;
17 import org.opendaylight.controller.cluster.access.concepts.MemberName;
18 import org.opendaylight.controller.cluster.datastore.identifiers.ShardIdentifier;
19 import org.opendaylight.controller.cluster.datastore.jmx.mbeans.shard.ShardStats;
20 import org.opendaylight.controller.cluster.datastore.messages.DataExists;
21 import org.opendaylight.controller.cluster.datastore.messages.ReadData;
22 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
23 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
24 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
25 import org.opendaylight.yangtools.yang.data.api.schema.tree.TreeType;
26 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
27 import scala.concurrent.Await;
28 import scala.concurrent.Future;
29 import scala.concurrent.duration.Duration;
32 * Covers negative test cases
34 * @author Basheeruddin Ahmed <syedbahm@cisco.com>
36 public class ShardTransactionFailureTest extends AbstractActorTest {
37 private static final SchemaContext testSchemaContext =
38 TestModel.createTestContext();
39 private static final TransactionType RO = TransactionType.READ_ONLY;
40 private static final TransactionType RW = TransactionType.READ_WRITE;
41 private static final TransactionType WO = TransactionType.WRITE_ONLY;
43 private static final Shard mockShard = Mockito.mock(Shard.class);
45 private static final ShardDataTree store = new ShardDataTree(mockShard, testSchemaContext, TreeType.OPERATIONAL);
47 private static final ShardIdentifier SHARD_IDENTIFIER =
48 ShardIdentifier.create("inventory", MemberName.forName("member-1"), "operational");
50 private final DatastoreContext datastoreContext = DatastoreContext.newBuilder().build();
52 private final ShardStats shardStats = new ShardStats(SHARD_IDENTIFIER.toString(), "DataStore");
54 private ActorRef createShard(){
55 ActorRef shard = getSystem().actorOf(Shard.builder().id(SHARD_IDENTIFIER).datastoreContext(datastoreContext).
56 schemaContext(TestModel.createTestContext()).props());
57 ShardTestKit.waitUntilLeader(shard);
61 @Test(expected = ReadFailedException.class)
62 public void testNegativeReadWithReadOnlyTransactionClosed() throws Throwable {
64 final ActorRef shard = createShard();
65 final Props props = ShardTransaction.props(RO, store.newReadOnlyTransaction(nextTransactionId()), shard,
66 datastoreContext, shardStats);
68 final TestActorRef<ShardTransaction> subject = TestActorRef.create(getSystem(), props,
69 "testNegativeReadWithReadOnlyTransactionClosed");
71 Future<Object> future = akka.pattern.Patterns.ask(subject,
72 new ReadData(YangInstanceIdentifier.EMPTY, DataStoreVersions.CURRENT_VERSION), 3000);
73 Await.result(future, Duration.create(3, TimeUnit.SECONDS));
75 subject.underlyingActor().getDOMStoreTransaction().abort();
77 future = akka.pattern.Patterns.ask(subject, new ReadData(YangInstanceIdentifier.EMPTY,
78 DataStoreVersions.CURRENT_VERSION), 3000);
79 Await.result(future, Duration.create(3, TimeUnit.SECONDS));
83 @Test(expected = ReadFailedException.class)
84 public void testNegativeReadWithReadWriteTransactionClosed() throws Throwable {
86 final ActorRef shard = createShard();
87 final Props props = ShardTransaction.props(RW, store.newReadWriteTransaction(nextTransactionId()), shard,
88 datastoreContext, shardStats);
90 final TestActorRef<ShardTransaction> subject = TestActorRef.create(getSystem(), props,
91 "testNegativeReadWithReadWriteTransactionClosed");
93 Future<Object> future = akka.pattern.Patterns.ask(subject,
94 new ReadData(YangInstanceIdentifier.EMPTY, DataStoreVersions.CURRENT_VERSION), 3000);
95 Await.result(future, Duration.create(3, TimeUnit.SECONDS));
97 subject.underlyingActor().getDOMStoreTransaction().abort();
99 future = akka.pattern.Patterns.ask(subject, new ReadData(YangInstanceIdentifier.EMPTY,
100 DataStoreVersions.CURRENT_VERSION), 3000);
101 Await.result(future, Duration.create(3, TimeUnit.SECONDS));
104 @Test(expected = ReadFailedException.class)
105 public void testNegativeExistsWithReadWriteTransactionClosed() throws Throwable {
107 final ActorRef shard = createShard();
108 final Props props = ShardTransaction.props(RW, store.newReadWriteTransaction(nextTransactionId()), shard,
109 datastoreContext, shardStats);
111 final TestActorRef<ShardTransaction> subject = TestActorRef.create(getSystem(), props,
112 "testNegativeExistsWithReadWriteTransactionClosed");
114 Future<Object> future = akka.pattern.Patterns.ask(subject,
115 new DataExists(YangInstanceIdentifier.EMPTY, DataStoreVersions.CURRENT_VERSION), 3000);
116 Await.result(future, Duration.create(3, TimeUnit.SECONDS));
118 subject.underlyingActor().getDOMStoreTransaction().abort();
120 future = akka.pattern.Patterns.ask(subject,
121 new DataExists(YangInstanceIdentifier.EMPTY, DataStoreVersions.CURRENT_VERSION), 3000);
122 Await.result(future, Duration.create(3, TimeUnit.SECONDS));