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.jmx.mbeans.shard.ShardStats;
17 import org.opendaylight.controller.cluster.datastore.messages.CloseTransaction;
18 import org.opendaylight.controller.cluster.datastore.messages.CloseTransactionReply;
19 import org.opendaylight.controller.cluster.datastore.messages.DataExists;
20 import org.opendaylight.controller.cluster.datastore.messages.DataExistsReply;
21 import org.opendaylight.controller.cluster.datastore.messages.DeleteData;
22 import org.opendaylight.controller.cluster.datastore.messages.DeleteDataReply;
23 import org.opendaylight.controller.cluster.datastore.messages.MergeData;
24 import org.opendaylight.controller.cluster.datastore.messages.MergeDataReply;
25 import org.opendaylight.controller.cluster.datastore.messages.ReadData;
26 import org.opendaylight.controller.cluster.datastore.messages.ReadDataReply;
27 import org.opendaylight.controller.cluster.datastore.messages.ReadyTransaction;
28 import org.opendaylight.controller.cluster.datastore.messages.ReadyTransactionReply;
29 import org.opendaylight.controller.cluster.datastore.messages.WriteData;
30 import org.opendaylight.controller.cluster.datastore.messages.WriteDataReply;
31 import org.opendaylight.controller.cluster.datastore.modification.CompositeModification;
32 import org.opendaylight.controller.cluster.datastore.modification.DeleteModification;
33 import org.opendaylight.controller.cluster.datastore.modification.MergeModification;
34 import org.opendaylight.controller.cluster.datastore.modification.Modification;
35 import org.opendaylight.controller.cluster.datastore.modification.WriteModification;
36 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
37 import org.opendaylight.controller.md.sal.dom.store.impl.InMemoryDOMDataStore;
38 import org.opendaylight.controller.md.sal.dom.store.impl.InMemoryDOMDataStoreConfigProperties;
39 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
40 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
41 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
43 import scala.concurrent.duration.Duration;
45 import java.util.Collections;
46 import java.util.concurrent.TimeUnit;
48 import static org.junit.Assert.assertEquals;
49 import static org.junit.Assert.assertTrue;
51 public class ShardTransactionTest extends AbstractActorTest {
52 private static ListeningExecutorService storeExecutor =
53 MoreExecutors.listeningDecorator(MoreExecutors.sameThreadExecutor());
55 private static final InMemoryDOMDataStore store =
56 new InMemoryDOMDataStore("OPER", storeExecutor, MoreExecutors.sameThreadExecutor());
58 private static final SchemaContext testSchemaContext = TestModel.createTestContext();
60 private static final ShardIdentifier SHARD_IDENTIFIER =
61 ShardIdentifier.builder().memberName("member-1")
62 .shardName("inventory").type("config").build();
64 private DatastoreContext datastoreContext = new DatastoreContext();
66 private final ShardStats shardStats = new ShardStats(SHARD_IDENTIFIER.toString(), "DataStore");
69 public static void staticSetup() {
70 store.onGlobalContextUpdated(testSchemaContext);
74 public void testOnReceiveReadData() throws Exception {
75 new JavaTestKit(getSystem()) {{
76 final ActorRef shard = getSystem().actorOf(Shard.props(SHARD_IDENTIFIER,
77 Collections.EMPTY_MAP, new DatastoreContext()));
78 final Props props = ShardTransaction.props(store.newReadOnlyTransaction(), shard,
79 testSchemaContext, datastoreContext, shardStats);
80 final ActorRef subject = getSystem().actorOf(props, "testReadData");
82 new Within(duration("1 seconds")) {
84 protected void run() {
87 new ReadData(YangInstanceIdentifier.builder().build()).toSerializable(),
90 final String out = new ExpectMsg<String>(duration("1 seconds"), "match hint") {
91 // do not put code outside this method, will run afterwards
93 protected String match(Object in) {
94 if (in.getClass().equals(ReadDataReply.SERIALIZABLE_CLASS)) {
95 if (ReadDataReply.fromSerializable(testSchemaContext,YangInstanceIdentifier.builder().build(), in)
96 .getNormalizedNode()!= null) {
104 }.get(); // this extracts the received message
106 assertEquals("match", out);
117 public void testOnReceiveReadDataWhenDataNotFound() throws Exception {
118 new JavaTestKit(getSystem()) {{
119 final ActorRef shard = getSystem().actorOf(Shard.props(SHARD_IDENTIFIER,
120 Collections.EMPTY_MAP, new DatastoreContext()));
121 final Props props = ShardTransaction.props( store.newReadOnlyTransaction(), shard,
122 testSchemaContext, datastoreContext, shardStats);
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 = getSystem().actorOf(Shard.props(SHARD_IDENTIFIER,
164 Collections.EMPTY_MAP, new DatastoreContext()));
165 final Props props = ShardTransaction.props(store.newReadOnlyTransaction(), shard,
166 testSchemaContext, datastoreContext, shardStats);
167 final ActorRef subject = getSystem().actorOf(props, "testDataExistsPositive");
169 new Within(duration("1 seconds")) {
171 protected void run() {
174 new DataExists(YangInstanceIdentifier.builder().build()).toSerializable(),
177 final String out = new ExpectMsg<String>(duration("1 seconds"), "match hint") {
178 // do not put code outside this method, will run afterwards
180 protected String match(Object in) {
181 if (in.getClass().equals(DataExistsReply.SERIALIZABLE_CLASS)) {
182 if (DataExistsReply.fromSerializable(in)
191 }.get(); // this extracts the received message
193 assertEquals("match", out);
204 public void testOnReceiveDataExistsNegative() throws Exception {
205 new JavaTestKit(getSystem()) {{
206 final ActorRef shard = getSystem().actorOf(Shard.props(SHARD_IDENTIFIER,
207 Collections.EMPTY_MAP, new DatastoreContext()));
208 final Props props = ShardTransaction.props(store.newReadOnlyTransaction(), shard,
209 testSchemaContext, datastoreContext, shardStats);
210 final ActorRef subject = getSystem().actorOf(props, "testDataExistsNegative");
212 new Within(duration("1 seconds")) {
214 protected void run() {
217 new DataExists(TestModel.TEST_PATH).toSerializable(),
220 final String out = new ExpectMsg<String>(duration("1 seconds"), "match hint") {
221 // do not put code outside this method, will run afterwards
223 protected String match(Object in) {
224 if (in.getClass().equals(DataExistsReply.SERIALIZABLE_CLASS)) {
225 if (!DataExistsReply.fromSerializable(in)
234 }.get(); // this extracts the received message
236 assertEquals("match", out);
246 private void assertModification(final ActorRef subject,
247 final Class<? extends Modification> modificationType) {
248 new JavaTestKit(getSystem()) {{
249 new Within(duration("1 seconds")) {
251 protected void run() {
253 .tell(new ShardTransaction.GetCompositedModification(),
256 final CompositeModification compositeModification =
257 new ExpectMsg<CompositeModification>(duration("1 seconds"), "match hint") {
258 // do not put code outside this method, will run afterwards
260 protected CompositeModification match(Object in) {
261 if (in instanceof ShardTransaction.GetCompositeModificationReply) {
262 return ((ShardTransaction.GetCompositeModificationReply) in)
268 }.get(); // this extracts the received message
271 compositeModification.getModifications().size() == 1);
272 assertEquals(modificationType,
273 compositeModification.getModifications().get(0)
282 public void testOnReceiveWriteData() throws Exception {
283 new JavaTestKit(getSystem()) {{
284 final ActorRef shard = getSystem().actorOf(Shard.props(SHARD_IDENTIFIER,
285 Collections.EMPTY_MAP, new DatastoreContext()));
286 final Props props = ShardTransaction.props(store.newWriteOnlyTransaction(), shard,
287 testSchemaContext, datastoreContext, shardStats);
288 final ActorRef subject =
289 getSystem().actorOf(props, "testWriteData");
291 new Within(duration("1 seconds")) {
293 protected void run() {
295 subject.tell(new WriteData(TestModel.TEST_PATH,
296 ImmutableNodes.containerNode(TestModel.TEST_QNAME), TestModel.createTestContext()).toSerializable(),
299 final String out = new ExpectMsg<String>(duration("1 seconds"), "match hint") {
300 // do not put code outside this method, will run afterwards
302 protected String match(Object in) {
303 if (in.getClass().equals(WriteDataReply.SERIALIZABLE_CLASS)) {
309 }.get(); // this extracts the received message
311 assertEquals("match", out);
313 assertModification(subject, WriteModification.class);
323 public void testOnReceiveMergeData() throws Exception {
324 new JavaTestKit(getSystem()) {{
325 final ActorRef shard = getSystem().actorOf(Shard.props(SHARD_IDENTIFIER,
326 Collections.EMPTY_MAP, new DatastoreContext()));
327 final Props props = ShardTransaction.props(store.newReadWriteTransaction(), shard,
328 testSchemaContext, datastoreContext, shardStats);
329 final ActorRef subject =
330 getSystem().actorOf(props, "testMergeData");
332 new Within(duration("1 seconds")) {
334 protected void run() {
336 subject.tell(new MergeData(TestModel.TEST_PATH,
337 ImmutableNodes.containerNode(TestModel.TEST_QNAME), testSchemaContext).toSerializable(),
340 final String out = new ExpectMsg<String>(duration("500 milliseconds"), "match hint") {
341 // do not put code outside this method, will run afterwards
343 protected String match(Object in) {
344 if (in.getClass().equals(MergeDataReply.SERIALIZABLE_CLASS)) {
350 }.get(); // this extracts the received message
352 assertEquals("match", out);
354 assertModification(subject, MergeModification.class);
365 public void testOnReceiveDeleteData() throws Exception {
366 new JavaTestKit(getSystem()) {{
367 final ActorRef shard = getSystem().actorOf(Shard.props(SHARD_IDENTIFIER,
368 Collections.EMPTY_MAP, new DatastoreContext()));
369 final Props props = ShardTransaction.props( store.newWriteOnlyTransaction(), shard,
370 testSchemaContext, datastoreContext, shardStats);
371 final ActorRef subject =
372 getSystem().actorOf(props, "testDeleteData");
374 new Within(duration("1 seconds")) {
376 protected void run() {
378 subject.tell(new DeleteData(TestModel.TEST_PATH).toSerializable(), getRef());
380 final String out = new ExpectMsg<String>(duration("1 seconds"), "match hint") {
381 // do not put code outside this method, will run afterwards
383 protected String match(Object in) {
384 if (in.getClass().equals(DeleteDataReply.SERIALIZABLE_CLASS)) {
390 }.get(); // this extracts the received message
392 assertEquals("match", out);
394 assertModification(subject, DeleteModification.class);
405 public void testOnReceiveReadyTransaction() throws Exception {
406 new JavaTestKit(getSystem()) {{
407 final ActorRef shard = getSystem().actorOf(Shard.props(SHARD_IDENTIFIER,
408 Collections.EMPTY_MAP, new DatastoreContext()));
409 final Props props = ShardTransaction.props( store.newReadWriteTransaction(), shard,
410 testSchemaContext, datastoreContext, shardStats);
411 final ActorRef subject =
412 getSystem().actorOf(props, "testReadyTransaction");
414 new Within(duration("1 seconds")) {
416 protected void run() {
418 subject.tell(new ReadyTransaction().toSerializable(), getRef());
420 final String out = new ExpectMsg<String>(duration("1 seconds"), "match hint") {
421 // do not put code outside this method, will run afterwards
423 protected String match(Object in) {
424 if (in.getClass().equals(ReadyTransactionReply.SERIALIZABLE_CLASS)) {
430 }.get(); // this extracts the received message
432 assertEquals("match", out);
444 public void testOnReceiveCloseTransaction() throws Exception {
445 new JavaTestKit(getSystem()) {{
446 final ActorRef shard = getSystem().actorOf(Shard.props(SHARD_IDENTIFIER,
447 Collections.EMPTY_MAP, new DatastoreContext()));
448 final Props props = ShardTransaction.props(store.newReadWriteTransaction(), shard,
449 testSchemaContext, datastoreContext, shardStats);
450 final ActorRef subject =
451 getSystem().actorOf(props, "testCloseTransaction");
455 new Within(duration("6 seconds")) {
457 protected void run() {
459 subject.tell(new CloseTransaction().toSerializable(), getRef());
461 final String out = new ExpectMsg<String>(duration("3 seconds"), "match hint") {
462 // do not put code outside this method, will run afterwards
464 protected String match(Object in) {
465 System.out.println("!!!IN match 1: "+(in!=null?in.getClass():"NULL"));
466 if (in.getClass().equals(CloseTransactionReply.SERIALIZABLE_CLASS)) {
472 }.get(); // this extracts the received message
474 assertEquals("match", out);
476 final String termination = new ExpectMsg<String>(duration("3 seconds"), "match hint") {
477 // do not put code outside this method, will run afterwards
479 protected String match(Object in) {
480 System.out.println("!!!IN match 2: "+(in!=null?in.getClass():"NULL"));
481 if (in instanceof Terminated) {
487 }.get(); // this extracts the received message
489 assertEquals("match", termination);
495 @Test(expected=UnknownMessageException.class)
496 public void testNegativePerformingWriteOperationOnReadTransaction() throws Exception {
497 final ActorRef shard = getSystem().actorOf(Shard.props(SHARD_IDENTIFIER,
498 Collections.EMPTY_MAP, new DatastoreContext()));
499 final Props props = ShardTransaction.props(store.newReadOnlyTransaction(), shard,
500 testSchemaContext, datastoreContext, shardStats);
501 final TestActorRef subject = TestActorRef.apply(props,getSystem());
503 subject.receive(new DeleteData(TestModel.TEST_PATH).toSerializable(), ActorRef.noSender());
507 public void testShardTransactionInactivity() {
509 datastoreContext = new DatastoreContext("Test",
510 InMemoryDOMDataStoreConfigProperties.getDefault(),
511 Duration.create(500, TimeUnit.MILLISECONDS), 5);
513 new JavaTestKit(getSystem()) {{
514 final ActorRef shard = getSystem().actorOf(Shard.props(SHARD_IDENTIFIER,
515 Collections.EMPTY_MAP, new DatastoreContext()));
516 final Props props = ShardTransaction.props(store.newReadWriteTransaction(), shard,
517 testSchemaContext, datastoreContext, shardStats);
518 final ActorRef subject =
519 getSystem().actorOf(props, "testShardTransactionInactivity");
523 // The shard Tx actor should receive a ReceiveTimeout message and self-destruct.
525 final String termination = new ExpectMsg<String>(duration("3 seconds"), "match hint") {
526 // do not put code outside this method, will run afterwards
528 protected String match(Object in) {
529 if (in instanceof Terminated) {
535 }.get(); // this extracts the received message
537 assertEquals("match", termination);