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
8 package org.opendaylight.controller.cluster.datastore;
10 import static org.mockito.Mockito.mock;
11 import static org.mockito.Mockito.when;
13 import akka.actor.ActorRef;
14 import akka.actor.Props;
15 import akka.testkit.TestActorRef;
16 import java.util.concurrent.TimeUnit;
17 import org.junit.Before;
18 import org.junit.Test;
19 import org.opendaylight.controller.cluster.access.concepts.MemberName;
20 import org.opendaylight.controller.cluster.datastore.identifiers.ShardIdentifier;
21 import org.opendaylight.controller.cluster.datastore.jmx.mbeans.shard.ShardStats;
22 import org.opendaylight.controller.cluster.datastore.messages.DataExists;
23 import org.opendaylight.controller.cluster.datastore.messages.ReadData;
24 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
25 import org.opendaylight.mdsal.common.api.ReadFailedException;
26 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
27 import org.opendaylight.yangtools.yang.data.api.schema.tree.TreeType;
28 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
29 import scala.concurrent.Await;
30 import scala.concurrent.Future;
31 import scala.concurrent.duration.FiniteDuration;
34 * Covers negative test cases.
36 * @author Basheeruddin Ahmed
38 public class ShardTransactionFailureTest extends AbstractActorTest {
39 private static final EffectiveModelContext TEST_SCHEMA_CONTEXT = TestModel.createTestContext();
40 private static final TransactionType RO = TransactionType.READ_ONLY;
41 private static final TransactionType RW = TransactionType.READ_WRITE;
43 private static final Shard MOCK_SHARD = mock(Shard.class);
45 private static final ShardDataTree STORE = new ShardDataTree(MOCK_SHARD, TEST_SCHEMA_CONTEXT, 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", null);
54 private ActorRef createShard() {
55 ActorRef shard = getSystem().actorOf(Shard.builder().id(SHARD_IDENTIFIER).datastoreContext(datastoreContext)
56 .schemaContextProvider(() -> TEST_SCHEMA_CONTEXT).props());
57 ShardTestKit.waitUntilLeader(shard);
63 ShardStats stats = mock(ShardStats.class);
64 when(MOCK_SHARD.getShardMBean()).thenReturn(stats);
67 @Test(expected = ReadFailedException.class)
68 public void testNegativeReadWithReadOnlyTransactionClosed() throws Exception {
70 final ActorRef shard = createShard();
71 final Props props = ShardTransaction.props(RO, STORE.newReadOnlyTransaction(nextTransactionId()), shard,
72 datastoreContext, shardStats);
74 final TestActorRef<ShardTransaction> subject = TestActorRef.create(getSystem(), props,
75 "testNegativeReadWithReadOnlyTransactionClosed");
77 Future<Object> future = akka.pattern.Patterns.ask(subject,
78 new ReadData(YangInstanceIdentifier.empty(), DataStoreVersions.CURRENT_VERSION), 3000);
79 Await.result(future, FiniteDuration.create(3, TimeUnit.SECONDS));
81 subject.underlyingActor().getDOMStoreTransaction().abortFromTransactionActor();
83 future = akka.pattern.Patterns.ask(subject, new ReadData(YangInstanceIdentifier.empty(),
84 DataStoreVersions.CURRENT_VERSION), 3000);
85 Await.result(future, FiniteDuration.create(3, TimeUnit.SECONDS));
89 @Test(expected = ReadFailedException.class)
90 public void testNegativeReadWithReadWriteTransactionClosed() throws Exception {
92 final ActorRef shard = createShard();
93 final Props props = ShardTransaction.props(RW, STORE.newReadWriteTransaction(nextTransactionId()), shard,
94 datastoreContext, shardStats);
96 final TestActorRef<ShardTransaction> subject = TestActorRef.create(getSystem(), props,
97 "testNegativeReadWithReadWriteTransactionClosed");
99 Future<Object> future = akka.pattern.Patterns.ask(subject,
100 new ReadData(YangInstanceIdentifier.empty(), DataStoreVersions.CURRENT_VERSION), 3000);
101 Await.result(future, FiniteDuration.create(3, TimeUnit.SECONDS));
103 subject.underlyingActor().getDOMStoreTransaction().abortFromTransactionActor();
105 future = akka.pattern.Patterns.ask(subject, new ReadData(YangInstanceIdentifier.empty(),
106 DataStoreVersions.CURRENT_VERSION), 3000);
107 Await.result(future, FiniteDuration.create(3, TimeUnit.SECONDS));
110 @Test(expected = ReadFailedException.class)
111 public void testNegativeExistsWithReadWriteTransactionClosed() throws Exception {
113 final ActorRef shard = createShard();
114 final Props props = ShardTransaction.props(RW, STORE.newReadWriteTransaction(nextTransactionId()), shard,
115 datastoreContext, shardStats);
117 final TestActorRef<ShardTransaction> subject = TestActorRef.create(getSystem(), props,
118 "testNegativeExistsWithReadWriteTransactionClosed");
120 Future<Object> future = akka.pattern.Patterns.ask(subject,
121 new DataExists(YangInstanceIdentifier.empty(), DataStoreVersions.CURRENT_VERSION), 3000);
122 Await.result(future, FiniteDuration.create(3, TimeUnit.SECONDS));
124 subject.underlyingActor().getDOMStoreTransaction().abortFromTransactionActor();
126 future = akka.pattern.Patterns.ask(subject,
127 new DataExists(YangInstanceIdentifier.empty(), DataStoreVersions.CURRENT_VERSION), 3000);
128 Await.result(future, FiniteDuration.create(3, TimeUnit.SECONDS));