1 package org.opendaylight.controller.cluster.datastore;
3 import akka.actor.ActorRef;
4 import akka.actor.Props;
5 import akka.actor.Terminated;
6 import akka.testkit.JavaTestKit;
7 import akka.testkit.TestActorRef;
9 import com.google.common.util.concurrent.ListeningExecutorService;
10 import com.google.common.util.concurrent.MoreExecutors;
12 import org.junit.BeforeClass;
13 import org.junit.Test;
14 import org.opendaylight.controller.cluster.datastore.exceptions.UnknownMessageException;
15 import org.opendaylight.controller.cluster.datastore.identifiers.ShardIdentifier;
16 import org.opendaylight.controller.cluster.datastore.messages.CloseTransaction;
17 import org.opendaylight.controller.cluster.datastore.messages.CloseTransactionReply;
18 import org.opendaylight.controller.cluster.datastore.messages.DataExists;
19 import org.opendaylight.controller.cluster.datastore.messages.DataExistsReply;
20 import org.opendaylight.controller.cluster.datastore.messages.DeleteData;
21 import org.opendaylight.controller.cluster.datastore.messages.DeleteDataReply;
22 import org.opendaylight.controller.cluster.datastore.messages.MergeData;
23 import org.opendaylight.controller.cluster.datastore.messages.MergeDataReply;
24 import org.opendaylight.controller.cluster.datastore.messages.ReadData;
25 import org.opendaylight.controller.cluster.datastore.messages.ReadDataReply;
26 import org.opendaylight.controller.cluster.datastore.messages.ReadyTransaction;
27 import org.opendaylight.controller.cluster.datastore.messages.ReadyTransactionReply;
28 import org.opendaylight.controller.cluster.datastore.messages.WriteData;
29 import org.opendaylight.controller.cluster.datastore.messages.WriteDataReply;
30 import org.opendaylight.controller.cluster.datastore.modification.CompositeModification;
31 import org.opendaylight.controller.cluster.datastore.modification.DeleteModification;
32 import org.opendaylight.controller.cluster.datastore.modification.MergeModification;
33 import org.opendaylight.controller.cluster.datastore.modification.Modification;
34 import org.opendaylight.controller.cluster.datastore.modification.WriteModification;
35 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
36 import org.opendaylight.controller.md.sal.dom.store.impl.InMemoryDOMDataStore;
37 import org.opendaylight.controller.md.sal.dom.store.impl.InMemoryDOMDataStoreConfigProperties;
38 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
39 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
40 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
42 import scala.concurrent.duration.Duration;
44 import java.util.Collections;
45 import java.util.concurrent.TimeUnit;
47 import static org.junit.Assert.assertEquals;
48 import static org.junit.Assert.assertTrue;
50 public class ShardTransactionTest extends AbstractActorTest {
51 private static ListeningExecutorService storeExecutor =
52 MoreExecutors.listeningDecorator(MoreExecutors.sameThreadExecutor());
54 private static final InMemoryDOMDataStore store =
55 new InMemoryDOMDataStore("OPER", storeExecutor, MoreExecutors.sameThreadExecutor());
57 private static final SchemaContext testSchemaContext = TestModel.createTestContext();
59 private static final ShardIdentifier SHARD_IDENTIFIER =
60 ShardIdentifier.builder().memberName("member-1")
61 .shardName("inventory").type("config").build();
63 private DatastoreContext datastoreContext = new DatastoreContext();
66 public static void staticSetup() {
67 store.onGlobalContextUpdated(testSchemaContext);
70 private ActorRef createShard(){
71 return getSystem().actorOf(Shard.props(SHARD_IDENTIFIER,
72 Collections.EMPTY_MAP, new DatastoreContext(), TestModel.createTestContext()));
76 public void testOnReceiveReadData() throws Exception {
77 new JavaTestKit(getSystem()) {{
78 final ActorRef shard = createShard();
79 final Props props = ShardTransaction.props(store.newReadOnlyTransaction(), shard,
80 testSchemaContext, datastoreContext,SHARD_IDENTIFIER.toString());
81 final ActorRef subject = getSystem().actorOf(props, "testReadData");
83 new Within(duration("1 seconds")) {
85 protected void run() {
88 new ReadData(YangInstanceIdentifier.builder().build()).toSerializable(),
91 final String out = new ExpectMsg<String>(duration("1 seconds"), "match hint") {
92 // do not put code outside this method, will run afterwards
94 protected String match(Object in) {
95 if (in.getClass().equals(ReadDataReply.SERIALIZABLE_CLASS)) {
96 if (ReadDataReply.fromSerializable(testSchemaContext,YangInstanceIdentifier.builder().build(), in)
97 .getNormalizedNode()!= null) {
105 }.get(); // this extracts the received message
107 assertEquals("match", out);
118 public void testOnReceiveReadDataWhenDataNotFound() throws Exception {
119 new JavaTestKit(getSystem()) {{
120 final ActorRef shard = createShard();
121 final Props props = ShardTransaction.props( store.newReadOnlyTransaction(), shard,
122 testSchemaContext, datastoreContext,SHARD_IDENTIFIER.toString());
123 final ActorRef subject = getSystem().actorOf(props, "testReadDataWhenDataNotFound");
125 new Within(duration("1 seconds")) {
127 protected void run() {
130 new ReadData(TestModel.TEST_PATH).toSerializable(),
133 final String out = new ExpectMsg<String>(duration("1 seconds"), "match hint") {
134 // do not put code outside this method, will run afterwards
136 protected String match(Object in) {
137 if (in.getClass().equals(ReadDataReply.SERIALIZABLE_CLASS)) {
138 if (ReadDataReply.fromSerializable(testSchemaContext,TestModel.TEST_PATH, in)
148 }.get(); // this extracts the received message
150 assertEquals("match", out);
161 public void testOnReceiveDataExistsPositive() throws Exception {
162 new JavaTestKit(getSystem()) {{
163 final ActorRef shard = createShard();
164 final Props props = ShardTransaction.props(store.newReadOnlyTransaction(), shard,
165 testSchemaContext, datastoreContext,SHARD_IDENTIFIER.toString());
166 final ActorRef subject = getSystem().actorOf(props, "testDataExistsPositive");
168 new Within(duration("1 seconds")) {
170 protected void run() {
173 new DataExists(YangInstanceIdentifier.builder().build()).toSerializable(),
176 final String out = new ExpectMsg<String>(duration("1 seconds"), "match hint") {
177 // do not put code outside this method, will run afterwards
179 protected String match(Object in) {
180 if (in.getClass().equals(DataExistsReply.SERIALIZABLE_CLASS)) {
181 if (DataExistsReply.fromSerializable(in)
190 }.get(); // this extracts the received message
192 assertEquals("match", out);
203 public void testOnReceiveDataExistsNegative() throws Exception {
204 new JavaTestKit(getSystem()) {{
205 final ActorRef shard = createShard();
206 final Props props = ShardTransaction.props(store.newReadOnlyTransaction(), shard,
207 testSchemaContext, datastoreContext,SHARD_IDENTIFIER.toString());
208 final ActorRef subject = getSystem().actorOf(props, "testDataExistsNegative");
210 new Within(duration("1 seconds")) {
212 protected void run() {
215 new DataExists(TestModel.TEST_PATH).toSerializable(),
218 final String out = new ExpectMsg<String>(duration("1 seconds"), "match hint") {
219 // do not put code outside this method, will run afterwards
221 protected String match(Object in) {
222 if (in.getClass().equals(DataExistsReply.SERIALIZABLE_CLASS)) {
223 if (!DataExistsReply.fromSerializable(in)
232 }.get(); // this extracts the received message
234 assertEquals("match", out);
244 private void assertModification(final ActorRef subject,
245 final Class<? extends Modification> modificationType) {
246 new JavaTestKit(getSystem()) {{
247 new Within(duration("1 seconds")) {
249 protected void run() {
251 .tell(new ShardTransaction.GetCompositedModification(),
254 final CompositeModification compositeModification =
255 new ExpectMsg<CompositeModification>(duration("1 seconds"), "match hint") {
256 // do not put code outside this method, will run afterwards
258 protected CompositeModification match(Object in) {
259 if (in instanceof ShardTransaction.GetCompositeModificationReply) {
260 return ((ShardTransaction.GetCompositeModificationReply) in)
266 }.get(); // this extracts the received message
269 compositeModification.getModifications().size() == 1);
270 assertEquals(modificationType,
271 compositeModification.getModifications().get(0)
280 public void testOnReceiveWriteData() throws Exception {
281 new JavaTestKit(getSystem()) {{
282 final ActorRef shard = createShard();
283 final Props props = ShardTransaction.props(store.newWriteOnlyTransaction(), shard,
284 testSchemaContext, datastoreContext,SHARD_IDENTIFIER.toString());
285 final ActorRef subject =
286 getSystem().actorOf(props, "testWriteData");
288 new Within(duration("1 seconds")) {
290 protected void run() {
292 subject.tell(new WriteData(TestModel.TEST_PATH,
293 ImmutableNodes.containerNode(TestModel.TEST_QNAME), TestModel.createTestContext()).toSerializable(),
296 final String out = new ExpectMsg<String>(duration("1 seconds"), "match hint") {
297 // do not put code outside this method, will run afterwards
299 protected String match(Object in) {
300 if (in.getClass().equals(WriteDataReply.SERIALIZABLE_CLASS)) {
306 }.get(); // this extracts the received message
308 assertEquals("match", out);
310 assertModification(subject, WriteModification.class);
320 public void testOnReceiveMergeData() throws Exception {
321 new JavaTestKit(getSystem()) {{
322 final ActorRef shard = createShard();
323 final Props props = ShardTransaction.props(store.newReadWriteTransaction(), shard,
324 testSchemaContext, datastoreContext,SHARD_IDENTIFIER.toString());
325 final ActorRef subject =
326 getSystem().actorOf(props, "testMergeData");
328 new Within(duration("1 seconds")) {
330 protected void run() {
332 subject.tell(new MergeData(TestModel.TEST_PATH,
333 ImmutableNodes.containerNode(TestModel.TEST_QNAME), testSchemaContext).toSerializable(),
336 final String out = new ExpectMsg<String>(duration("500 milliseconds"), "match hint") {
337 // do not put code outside this method, will run afterwards
339 protected String match(Object in) {
340 if (in.getClass().equals(MergeDataReply.SERIALIZABLE_CLASS)) {
346 }.get(); // this extracts the received message
348 assertEquals("match", out);
350 assertModification(subject, MergeModification.class);
361 public void testOnReceiveDeleteData() throws Exception {
362 new JavaTestKit(getSystem()) {{
363 final ActorRef shard = createShard();
364 final Props props = ShardTransaction.props( store.newWriteOnlyTransaction(), shard,
365 testSchemaContext, datastoreContext,SHARD_IDENTIFIER.toString());
366 final ActorRef subject =
367 getSystem().actorOf(props, "testDeleteData");
369 new Within(duration("1 seconds")) {
371 protected void run() {
373 subject.tell(new DeleteData(TestModel.TEST_PATH).toSerializable(), getRef());
375 final String out = new ExpectMsg<String>(duration("1 seconds"), "match hint") {
376 // do not put code outside this method, will run afterwards
378 protected String match(Object in) {
379 if (in.getClass().equals(DeleteDataReply.SERIALIZABLE_CLASS)) {
385 }.get(); // this extracts the received message
387 assertEquals("match", out);
389 assertModification(subject, DeleteModification.class);
400 public void testOnReceiveReadyTransaction() throws Exception {
401 new JavaTestKit(getSystem()) {{
402 final ActorRef shard = createShard();
403 final Props props = ShardTransaction.props( store.newReadWriteTransaction(), shard,
404 testSchemaContext, datastoreContext,SHARD_IDENTIFIER.toString());
405 final ActorRef subject =
406 getSystem().actorOf(props, "testReadyTransaction");
408 new Within(duration("1 seconds")) {
410 protected void run() {
412 subject.tell(new ReadyTransaction().toSerializable(), getRef());
414 final String out = new ExpectMsg<String>(duration("1 seconds"), "match hint") {
415 // do not put code outside this method, will run afterwards
417 protected String match(Object in) {
418 if (in.getClass().equals(ReadyTransactionReply.SERIALIZABLE_CLASS)) {
424 }.get(); // this extracts the received message
426 assertEquals("match", out);
438 public void testOnReceiveCloseTransaction() throws Exception {
439 new JavaTestKit(getSystem()) {{
440 final ActorRef shard = createShard();
441 final Props props = ShardTransaction.props(store.newReadWriteTransaction(), shard,
442 testSchemaContext, datastoreContext,SHARD_IDENTIFIER.toString());
443 final ActorRef subject =
444 getSystem().actorOf(props, "testCloseTransaction");
448 new Within(duration("6 seconds")) {
450 protected void run() {
452 subject.tell(new CloseTransaction().toSerializable(), getRef());
454 final String out = new ExpectMsg<String>(duration("3 seconds"), "match hint") {
455 // do not put code outside this method, will run afterwards
457 protected String match(Object in) {
458 System.out.println("!!!IN match 1: "+(in!=null?in.getClass():"NULL"));
459 if (in.getClass().equals(CloseTransactionReply.SERIALIZABLE_CLASS)) {
465 }.get(); // this extracts the received message
467 assertEquals("match", out);
469 final String termination = new ExpectMsg<String>(duration("3 seconds"), "match hint") {
470 // do not put code outside this method, will run afterwards
472 protected String match(Object in) {
473 System.out.println("!!!IN match 2: "+(in!=null?in.getClass():"NULL"));
474 if (in instanceof Terminated) {
480 }.get(); // this extracts the received message
482 assertEquals("match", termination);
488 @Test(expected=UnknownMessageException.class)
489 public void testNegativePerformingWriteOperationOnReadTransaction() throws Exception {
490 final ActorRef shard = createShard();
491 final Props props = ShardTransaction.props(store.newReadOnlyTransaction(), shard,
492 testSchemaContext, datastoreContext,SHARD_IDENTIFIER.toString());
493 final TestActorRef subject = TestActorRef.apply(props,getSystem());
495 subject.receive(new DeleteData(TestModel.TEST_PATH).toSerializable(), ActorRef.noSender());
499 public void testShardTransactionInactivity() {
501 datastoreContext = new DatastoreContext(InMemoryDOMDataStoreConfigProperties.getDefault(),
502 Duration.create(500, TimeUnit.MILLISECONDS));
504 new JavaTestKit(getSystem()) {{
505 final ActorRef shard = createShard();
506 final Props props = ShardTransaction.props(store.newReadWriteTransaction(), shard,
507 testSchemaContext, datastoreContext,SHARD_IDENTIFIER.toString());
508 final ActorRef subject =
509 getSystem().actorOf(props, "testShardTransactionInactivity");
513 // The shard Tx actor should receive a ReceiveTimeout message and self-destruct.
515 final String termination = new ExpectMsg<String>(duration("3 seconds"), "match hint") {
516 // do not put code outside this method, will run afterwards
518 protected String match(Object in) {
519 if (in instanceof Terminated) {
525 }.get(); // this extracts the received message
527 assertEquals("match", termination);