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.doReturn;
11 import static org.mockito.Mockito.mock;
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.messages.DataExists;
22 import org.opendaylight.controller.cluster.datastore.messages.ReadData;
23 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
24 import org.opendaylight.mdsal.common.api.ReadFailedException;
25 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
26 import org.opendaylight.yangtools.yang.data.tree.api.TreeType;
27 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
28 import scala.concurrent.Await;
29 import scala.concurrent.Future;
30 import scala.concurrent.duration.FiniteDuration;
33 * Covers negative test cases.
35 * @author Basheeruddin Ahmed
37 @Deprecated(since = "9.0.0", forRemoval = true)
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 doReturn(new ShardStats("inventory", "mxBeanType", MOCK_SHARD)).when(MOCK_SHARD).getShardMBean();
66 @Test(expected = ReadFailedException.class)
67 public void testNegativeReadWithReadOnlyTransactionClosed() throws Exception {
69 final ActorRef shard = createShard();
70 final Props props = ShardTransaction.props(RO, STORE.newReadOnlyTransaction(nextTransactionId()), shard,
71 datastoreContext, shardStats);
73 final TestActorRef<ShardTransaction> subject = TestActorRef.create(getSystem(), props,
74 "testNegativeReadWithReadOnlyTransactionClosed");
76 Future<Object> future = akka.pattern.Patterns.ask(subject,
77 new ReadData(YangInstanceIdentifier.of(), DataStoreVersions.CURRENT_VERSION), 3000);
78 Await.result(future, FiniteDuration.create(3, TimeUnit.SECONDS));
80 subject.underlyingActor().getDOMStoreTransaction().abortFromTransactionActor();
82 future = akka.pattern.Patterns.ask(subject, new ReadData(YangInstanceIdentifier.of(),
83 DataStoreVersions.CURRENT_VERSION), 3000);
84 Await.result(future, FiniteDuration.create(3, TimeUnit.SECONDS));
88 @Test(expected = ReadFailedException.class)
89 public void testNegativeReadWithReadWriteTransactionClosed() throws Exception {
91 final ActorRef shard = createShard();
92 final Props props = ShardTransaction.props(RW, STORE.newReadWriteTransaction(nextTransactionId()), shard,
93 datastoreContext, shardStats);
95 final TestActorRef<ShardTransaction> subject = TestActorRef.create(getSystem(), props,
96 "testNegativeReadWithReadWriteTransactionClosed");
98 Future<Object> future = akka.pattern.Patterns.ask(subject,
99 new ReadData(YangInstanceIdentifier.of(), DataStoreVersions.CURRENT_VERSION), 3000);
100 Await.result(future, FiniteDuration.create(3, TimeUnit.SECONDS));
102 subject.underlyingActor().getDOMStoreTransaction().abortFromTransactionActor();
104 future = akka.pattern.Patterns.ask(subject, new ReadData(YangInstanceIdentifier.of(),
105 DataStoreVersions.CURRENT_VERSION), 3000);
106 Await.result(future, FiniteDuration.create(3, TimeUnit.SECONDS));
109 @Test(expected = ReadFailedException.class)
110 public void testNegativeExistsWithReadWriteTransactionClosed() throws Exception {
112 final ActorRef shard = createShard();
113 final Props props = ShardTransaction.props(RW, STORE.newReadWriteTransaction(nextTransactionId()), shard,
114 datastoreContext, shardStats);
116 final TestActorRef<ShardTransaction> subject = TestActorRef.create(getSystem(), props,
117 "testNegativeExistsWithReadWriteTransactionClosed");
119 Future<Object> future = akka.pattern.Patterns.ask(subject,
120 new DataExists(YangInstanceIdentifier.of(), DataStoreVersions.CURRENT_VERSION), 3000);
121 Await.result(future, FiniteDuration.create(3, TimeUnit.SECONDS));
123 subject.underlyingActor().getDOMStoreTransaction().abortFromTransactionActor();
125 future = akka.pattern.Patterns.ask(subject,
126 new DataExists(YangInstanceIdentifier.of(), DataStoreVersions.CURRENT_VERSION), 3000);
127 Await.result(future, FiniteDuration.create(3, TimeUnit.SECONDS));