Merge "Custom mailbox that is bounded and instrumented."
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / ShardTransactionFailureTest.java
1 /*
2  *
3  *  Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
4  *
5  *  This program and the accompanying materials are made available under the
6  *  terms of the Eclipse Public License v1.0 which accompanies this distribution,
7  *  and is available at http://www.eclipse.org/legal/epl-v10.html
8  *
9  */
10
11 package org.opendaylight.controller.cluster.datastore;
12
13 import akka.actor.ActorRef;
14 import akka.actor.Props;
15 import akka.testkit.TestActorRef;
16 import com.google.common.util.concurrent.ListeningExecutorService;
17 import com.google.common.util.concurrent.MoreExecutors;
18 import org.junit.Test;
19 import org.opendaylight.controller.cluster.datastore.identifiers.ShardIdentifier;
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.controller.md.sal.dom.store.impl.InMemoryDOMDataStore;
23 import org.opendaylight.controller.protobuff.messages.common.NormalizedNodeMessages;
24 import org.opendaylight.controller.protobuff.messages.transaction.ShardTransactionMessages;
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;
29
30 import java.util.Collections;
31
32 import static org.junit.Assert.assertTrue;
33
34 /**
35  * Covers negative test cases
36  *
37  * @author Basheeruddin Ahmed <syedbahm@cisco.com>
38  */
39 public class ShardTransactionFailureTest extends AbstractActorTest {
40     private static ListeningExecutorService storeExecutor =
41         MoreExecutors.listeningDecorator(MoreExecutors.sameThreadExecutor());
42
43     private static final InMemoryDOMDataStore store =
44         new InMemoryDOMDataStore("OPER", storeExecutor,
45             MoreExecutors.sameThreadExecutor());
46
47     private static final SchemaContext testSchemaContext =
48         TestModel.createTestContext();
49
50     private static final ShardIdentifier SHARD_IDENTIFIER =
51         ShardIdentifier.builder().memberName("member-1")
52             .shardName("inventory").type("operational").build();
53
54     static {
55         store.onGlobalContextUpdated(testSchemaContext);
56     }
57
58
59     @Test(expected = ReadFailedException.class)
60     public void testNegativeReadWithReadOnlyTransactionClosed()
61         throws Throwable {
62
63         final ActorRef shard =
64             getSystem().actorOf(Shard.props(SHARD_IDENTIFIER, Collections.EMPTY_MAP, null));
65         final Props props =
66             ShardTransaction.props(store.newReadOnlyTransaction(), shard,
67                 TestModel.createTestContext());
68
69         final TestActorRef<ShardTransaction> subject = TestActorRef
70             .create(getSystem(), props,
71                 "testNegativeReadWithReadOnlyTransactionClosed");
72
73         ShardTransactionMessages.ReadData readData =
74             ShardTransactionMessages.ReadData.newBuilder()
75                 .setInstanceIdentifierPathArguments(
76                     NormalizedNodeMessages.InstanceIdentifier.newBuilder()
77                         .build()
78                 ).build();
79         Future<Object> future =
80             akka.pattern.Patterns.ask(subject, readData, 3000);
81         assertTrue(future.isCompleted());
82         Await.result(future, Duration.Zero());
83
84         ((ShardReadTransaction) subject.underlyingActor())
85             .forUnitTestOnlyExplicitTransactionClose();
86
87         future = akka.pattern.Patterns.ask(subject, readData, 3000);
88         Await.result(future, Duration.Zero());
89
90
91     }
92
93
94     @Test(expected = ReadFailedException.class)
95     public void testNegativeReadWithReadWriteOnlyTransactionClosed()
96         throws Throwable {
97
98         final ActorRef shard =
99             getSystem().actorOf(Shard.props(SHARD_IDENTIFIER, Collections.EMPTY_MAP,null));
100         final Props props =
101             ShardTransaction.props(store.newReadWriteTransaction(), shard,
102                 TestModel.createTestContext());
103
104         final TestActorRef<ShardTransaction> subject = TestActorRef
105             .create(getSystem(), props,
106                 "testNegativeReadWithReadWriteOnlyTransactionClosed");
107
108         ShardTransactionMessages.ReadData readData =
109             ShardTransactionMessages.ReadData.newBuilder()
110                 .setInstanceIdentifierPathArguments(
111                     NormalizedNodeMessages.InstanceIdentifier.newBuilder()
112                         .build()
113                 ).build();
114         Future<Object> future =
115             akka.pattern.Patterns.ask(subject, readData, 3000);
116         assertTrue(future.isCompleted());
117         Await.result(future, Duration.Zero());
118
119         ((ShardReadWriteTransaction) subject.underlyingActor())
120             .forUnitTestOnlyExplicitTransactionClose();
121
122         future = akka.pattern.Patterns.ask(subject, readData, 3000);
123         Await.result(future, Duration.Zero());
124
125
126     }
127
128     @Test(expected = ReadFailedException.class)
129     public void testNegativeExistsWithReadWriteOnlyTransactionClosed()
130         throws Throwable {
131
132         final ActorRef shard =
133             getSystem().actorOf(Shard.props(SHARD_IDENTIFIER, Collections.EMPTY_MAP,null));
134         final Props props =
135             ShardTransaction.props(store.newReadWriteTransaction(), shard,
136                 TestModel.createTestContext());
137
138         final TestActorRef<ShardTransaction> subject = TestActorRef
139             .create(getSystem(), props,
140                 "testNegativeExistsWithReadWriteOnlyTransactionClosed");
141
142         ShardTransactionMessages.DataExists dataExists =
143             ShardTransactionMessages.DataExists.newBuilder()
144                 .setInstanceIdentifierPathArguments(
145                     NormalizedNodeMessages.InstanceIdentifier.newBuilder()
146                         .build()
147                 ).build();
148
149         Future<Object> future =
150             akka.pattern.Patterns.ask(subject, dataExists, 3000);
151         assertTrue(future.isCompleted());
152         Await.result(future, Duration.Zero());
153
154         ((ShardReadWriteTransaction) subject.underlyingActor())
155             .forUnitTestOnlyExplicitTransactionClose();
156
157         future = akka.pattern.Patterns.ask(subject, dataExists, 3000);
158         Await.result(future, Duration.Zero());
159
160
161     }
162
163     @Test(expected = IllegalStateException.class)
164     public void testNegativeWriteWithTransactionReady() throws Exception {
165
166
167         final ActorRef shard =
168             getSystem().actorOf(Shard.props(SHARD_IDENTIFIER, Collections.EMPTY_MAP,null));
169         final Props props =
170             ShardTransaction.props(store.newWriteOnlyTransaction(), shard,
171                 TestModel.createTestContext());
172
173         final TestActorRef<ShardTransaction> subject = TestActorRef
174             .create(getSystem(), props,
175                 "testNegativeWriteWithTransactionReady");
176
177         ShardTransactionMessages.ReadyTransaction readyTransaction =
178             ShardTransactionMessages.ReadyTransaction.newBuilder().build();
179
180         Future<Object> future =
181             akka.pattern.Patterns.ask(subject, readyTransaction, 3000);
182         assertTrue(future.isCompleted());
183         Await.result(future, Duration.Zero());
184
185         ShardTransactionMessages.WriteData writeData =
186             ShardTransactionMessages.WriteData.newBuilder()
187                 .setInstanceIdentifierPathArguments(
188                     NormalizedNodeMessages.InstanceIdentifier.newBuilder()
189                         .build()).setNormalizedNode(
190                 NormalizedNodeMessages.Node.newBuilder().build()
191
192             ).build();
193
194         future = akka.pattern.Patterns.ask(subject, writeData, 3000);
195         assertTrue(future.isCompleted());
196         Await.result(future, Duration.Zero());
197
198
199     }
200
201
202     @Test(expected = IllegalStateException.class)
203     public void testNegativeReadWriteWithTransactionReady() throws Exception {
204
205
206         final ActorRef shard =
207             getSystem().actorOf(Shard.props(SHARD_IDENTIFIER, Collections.EMPTY_MAP,null));
208         final Props props =
209             ShardTransaction.props(store.newReadWriteTransaction(), shard,
210                 TestModel.createTestContext());
211
212         final TestActorRef<ShardTransaction> subject = TestActorRef
213             .create(getSystem(), props,
214                 "testNegativeReadWriteWithTransactionReady");
215
216         ShardTransactionMessages.ReadyTransaction readyTransaction =
217             ShardTransactionMessages.ReadyTransaction.newBuilder().build();
218
219         Future<Object> future =
220             akka.pattern.Patterns.ask(subject, readyTransaction, 3000);
221         assertTrue(future.isCompleted());
222         Await.result(future, Duration.Zero());
223
224         ShardTransactionMessages.WriteData writeData =
225             ShardTransactionMessages.WriteData.newBuilder()
226                 .setInstanceIdentifierPathArguments(
227                     NormalizedNodeMessages.InstanceIdentifier.newBuilder()
228                         .build()).setNormalizedNode(
229                 NormalizedNodeMessages.Node.newBuilder().build()
230
231             ).build();
232
233         future = akka.pattern.Patterns.ask(subject, writeData, 3000);
234         assertTrue(future.isCompleted());
235         Await.result(future, Duration.Zero());
236
237
238     }
239
240     @Test(expected = IllegalStateException.class)
241     public void testNegativeMergeTransactionReady() throws Exception {
242
243
244         final ActorRef shard =
245             getSystem().actorOf(Shard.props(SHARD_IDENTIFIER, Collections.EMPTY_MAP,null));
246         final Props props =
247             ShardTransaction.props(store.newReadWriteTransaction(), shard,
248                 TestModel.createTestContext());
249
250         final TestActorRef<ShardTransaction> subject = TestActorRef
251             .create(getSystem(), props, "testNegativeMergeTransactionReady");
252
253         ShardTransactionMessages.ReadyTransaction readyTransaction =
254             ShardTransactionMessages.ReadyTransaction.newBuilder().build();
255
256         Future<Object> future =
257             akka.pattern.Patterns.ask(subject, readyTransaction, 3000);
258         assertTrue(future.isCompleted());
259         Await.result(future, Duration.Zero());
260
261         ShardTransactionMessages.MergeData mergeData =
262             ShardTransactionMessages.MergeData.newBuilder()
263                 .setInstanceIdentifierPathArguments(
264                     NormalizedNodeMessages.InstanceIdentifier.newBuilder()
265                         .build()).setNormalizedNode(
266                 NormalizedNodeMessages.Node.newBuilder().build()
267
268             ).build();
269
270         future = akka.pattern.Patterns.ask(subject, mergeData, 3000);
271         assertTrue(future.isCompleted());
272         Await.result(future, Duration.Zero());
273
274
275     }
276
277
278     @Test(expected = IllegalStateException.class)
279     public void testNegativeDeleteDataWhenTransactionReady() throws Exception {
280
281
282         final ActorRef shard =
283             getSystem().actorOf(Shard.props(SHARD_IDENTIFIER, Collections.EMPTY_MAP,null));
284         final Props props =
285             ShardTransaction.props(store.newReadWriteTransaction(), shard,
286                 TestModel.createTestContext());
287
288         final TestActorRef<ShardTransaction> subject = TestActorRef
289             .create(getSystem(), props,
290                 "testNegativeDeleteDataWhenTransactionReady");
291
292         ShardTransactionMessages.ReadyTransaction readyTransaction =
293             ShardTransactionMessages.ReadyTransaction.newBuilder().build();
294
295         Future<Object> future =
296             akka.pattern.Patterns.ask(subject, readyTransaction, 3000);
297         assertTrue(future.isCompleted());
298         Await.result(future, Duration.Zero());
299
300         ShardTransactionMessages.DeleteData deleteData =
301             ShardTransactionMessages.DeleteData.newBuilder()
302                 .setInstanceIdentifierPathArguments(
303                     NormalizedNodeMessages.InstanceIdentifier.newBuilder()
304                         .build()).build();
305
306         future = akka.pattern.Patterns.ask(subject, deleteData, 3000);
307         assertTrue(future.isCompleted());
308         Await.result(future, Duration.Zero());
309
310
311     }
312 }