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);
73 private ActorRef createShard(){
74 return getSystem().actorOf(Shard.props(SHARD_IDENTIFIER,
75 Collections.EMPTY_MAP, new DatastoreContext(), TestModel.createTestContext()));
79 public void testOnReceiveReadData() throws Exception {
80 new JavaTestKit(getSystem()) {{
81 final ActorRef shard = createShard();
82 final Props props = ShardTransaction.props(store.newReadOnlyTransaction(), shard,
83 testSchemaContext, datastoreContext, shardStats);
84 final ActorRef subject = getSystem().actorOf(props, "testReadData");
86 new Within(duration("1 seconds")) {
88 protected void run() {
91 new ReadData(YangInstanceIdentifier.builder().build()).toSerializable(),
94 final String out = new ExpectMsg<String>(duration("1 seconds"), "match hint") {
95 // do not put code outside this method, will run afterwards
97 protected String match(Object in) {
98 if (in.getClass().equals(ReadDataReply.SERIALIZABLE_CLASS)) {
99 if (ReadDataReply.fromSerializable(testSchemaContext,YangInstanceIdentifier.builder().build(), in)
100 .getNormalizedNode()!= null) {
108 }.get(); // this extracts the received message
110 assertEquals("match", out);
121 public void testOnReceiveReadDataWhenDataNotFound() throws Exception {
122 new JavaTestKit(getSystem()) {{
123 final ActorRef shard = createShard();
124 final Props props = ShardTransaction.props( store.newReadOnlyTransaction(), shard,
125 testSchemaContext, datastoreContext, shardStats);
126 final ActorRef subject = getSystem().actorOf(props, "testReadDataWhenDataNotFound");
128 new Within(duration("1 seconds")) {
130 protected void run() {
133 new ReadData(TestModel.TEST_PATH).toSerializable(),
136 final String out = new ExpectMsg<String>(duration("1 seconds"), "match hint") {
137 // do not put code outside this method, will run afterwards
139 protected String match(Object in) {
140 if (in.getClass().equals(ReadDataReply.SERIALIZABLE_CLASS)) {
141 if (ReadDataReply.fromSerializable(testSchemaContext,TestModel.TEST_PATH, in)
151 }.get(); // this extracts the received message
153 assertEquals("match", out);
164 public void testOnReceiveDataExistsPositive() throws Exception {
165 new JavaTestKit(getSystem()) {{
166 final ActorRef shard = createShard();
167 final Props props = ShardTransaction.props(store.newReadOnlyTransaction(), shard,
168 testSchemaContext, datastoreContext, shardStats);
169 final ActorRef subject = getSystem().actorOf(props, "testDataExistsPositive");
171 new Within(duration("1 seconds")) {
173 protected void run() {
176 new DataExists(YangInstanceIdentifier.builder().build()).toSerializable(),
179 final String out = new ExpectMsg<String>(duration("1 seconds"), "match hint") {
180 // do not put code outside this method, will run afterwards
182 protected String match(Object in) {
183 if (in.getClass().equals(DataExistsReply.SERIALIZABLE_CLASS)) {
184 if (DataExistsReply.fromSerializable(in)
193 }.get(); // this extracts the received message
195 assertEquals("match", out);
206 public void testOnReceiveDataExistsNegative() throws Exception {
207 new JavaTestKit(getSystem()) {{
208 final ActorRef shard = createShard();
209 final Props props = ShardTransaction.props(store.newReadOnlyTransaction(), shard,
210 testSchemaContext, datastoreContext, shardStats);
211 final ActorRef subject = getSystem().actorOf(props, "testDataExistsNegative");
213 new Within(duration("1 seconds")) {
215 protected void run() {
218 new DataExists(TestModel.TEST_PATH).toSerializable(),
221 final String out = new ExpectMsg<String>(duration("1 seconds"), "match hint") {
222 // do not put code outside this method, will run afterwards
224 protected String match(Object in) {
225 if (in.getClass().equals(DataExistsReply.SERIALIZABLE_CLASS)) {
226 if (!DataExistsReply.fromSerializable(in)
235 }.get(); // this extracts the received message
237 assertEquals("match", out);
247 private void assertModification(final ActorRef subject,
248 final Class<? extends Modification> modificationType) {
249 new JavaTestKit(getSystem()) {{
250 new Within(duration("1 seconds")) {
252 protected void run() {
254 .tell(new ShardTransaction.GetCompositedModification(),
257 final CompositeModification compositeModification =
258 new ExpectMsg<CompositeModification>(duration("1 seconds"), "match hint") {
259 // do not put code outside this method, will run afterwards
261 protected CompositeModification match(Object in) {
262 if (in instanceof ShardTransaction.GetCompositeModificationReply) {
263 return ((ShardTransaction.GetCompositeModificationReply) in)
269 }.get(); // this extracts the received message
272 compositeModification.getModifications().size() == 1);
273 assertEquals(modificationType,
274 compositeModification.getModifications().get(0)
283 public void testOnReceiveWriteData() throws Exception {
284 new JavaTestKit(getSystem()) {{
285 final ActorRef shard = createShard();
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 = createShard();
326 final Props props = ShardTransaction.props(store.newReadWriteTransaction(), shard,
327 testSchemaContext, datastoreContext, shardStats);
328 final ActorRef subject =
329 getSystem().actorOf(props, "testMergeData");
331 new Within(duration("1 seconds")) {
333 protected void run() {
335 subject.tell(new MergeData(TestModel.TEST_PATH,
336 ImmutableNodes.containerNode(TestModel.TEST_QNAME), testSchemaContext).toSerializable(),
339 final String out = new ExpectMsg<String>(duration("500 milliseconds"), "match hint") {
340 // do not put code outside this method, will run afterwards
342 protected String match(Object in) {
343 if (in.getClass().equals(MergeDataReply.SERIALIZABLE_CLASS)) {
349 }.get(); // this extracts the received message
351 assertEquals("match", out);
353 assertModification(subject, MergeModification.class);
364 public void testOnReceiveDeleteData() throws Exception {
365 new JavaTestKit(getSystem()) {{
366 final ActorRef shard = createShard();
367 final Props props = ShardTransaction.props( store.newWriteOnlyTransaction(), shard,
368 testSchemaContext, datastoreContext, shardStats);
369 final ActorRef subject =
370 getSystem().actorOf(props, "testDeleteData");
372 new Within(duration("1 seconds")) {
374 protected void run() {
376 subject.tell(new DeleteData(TestModel.TEST_PATH).toSerializable(), getRef());
378 final String out = new ExpectMsg<String>(duration("1 seconds"), "match hint") {
379 // do not put code outside this method, will run afterwards
381 protected String match(Object in) {
382 if (in.getClass().equals(DeleteDataReply.SERIALIZABLE_CLASS)) {
388 }.get(); // this extracts the received message
390 assertEquals("match", out);
392 assertModification(subject, DeleteModification.class);
403 public void testOnReceiveReadyTransaction() throws Exception {
404 new JavaTestKit(getSystem()) {{
405 final ActorRef shard = createShard();
406 final Props props = ShardTransaction.props( store.newReadWriteTransaction(), shard,
407 testSchemaContext, datastoreContext, shardStats);
408 final ActorRef subject =
409 getSystem().actorOf(props, "testReadyTransaction");
411 new Within(duration("1 seconds")) {
413 protected void run() {
415 subject.tell(new ReadyTransaction().toSerializable(), getRef());
417 final String out = new ExpectMsg<String>(duration("1 seconds"), "match hint") {
418 // do not put code outside this method, will run afterwards
420 protected String match(Object in) {
421 if (in.getClass().equals(ReadyTransactionReply.SERIALIZABLE_CLASS)) {
427 }.get(); // this extracts the received message
429 assertEquals("match", out);
441 public void testOnReceiveCloseTransaction() throws Exception {
442 new JavaTestKit(getSystem()) {{
443 final ActorRef shard = createShard();
444 final Props props = ShardTransaction.props(store.newReadWriteTransaction(), shard,
445 testSchemaContext, datastoreContext, shardStats);
446 final ActorRef subject =
447 getSystem().actorOf(props, "testCloseTransaction");
451 new Within(duration("6 seconds")) {
453 protected void run() {
455 subject.tell(new CloseTransaction().toSerializable(), getRef());
457 final String out = new ExpectMsg<String>(duration("3 seconds"), "match hint") {
458 // do not put code outside this method, will run afterwards
460 protected String match(Object in) {
461 System.out.println("!!!IN match 1: "+(in!=null?in.getClass():"NULL"));
462 if (in.getClass().equals(CloseTransactionReply.SERIALIZABLE_CLASS)) {
468 }.get(); // this extracts the received message
470 assertEquals("match", out);
472 final String termination = new ExpectMsg<String>(duration("3 seconds"), "match hint") {
473 // do not put code outside this method, will run afterwards
475 protected String match(Object in) {
476 System.out.println("!!!IN match 2: "+(in!=null?in.getClass():"NULL"));
477 if (in instanceof Terminated) {
483 }.get(); // this extracts the received message
485 assertEquals("match", termination);
491 @Test(expected=UnknownMessageException.class)
492 public void testNegativePerformingWriteOperationOnReadTransaction() throws Exception {
493 final ActorRef shard = createShard();
494 final Props props = ShardTransaction.props(store.newReadOnlyTransaction(), shard,
495 testSchemaContext, datastoreContext, shardStats);
496 final TestActorRef subject = TestActorRef.apply(props,getSystem());
498 subject.receive(new DeleteData(TestModel.TEST_PATH).toSerializable(), ActorRef.noSender());
502 public void testShardTransactionInactivity() {
504 datastoreContext = new DatastoreContext("Test",
505 InMemoryDOMDataStoreConfigProperties.getDefault(),
506 Duration.create(500, TimeUnit.MILLISECONDS), 5, 1000, 1000, 500);
508 new JavaTestKit(getSystem()) {{
509 final ActorRef shard = createShard();
510 final Props props = ShardTransaction.props(store.newReadWriteTransaction(), shard,
511 testSchemaContext, datastoreContext, shardStats);
512 final ActorRef subject =
513 getSystem().actorOf(props, "testShardTransactionInactivity");
517 // The shard Tx actor should receive a ReceiveTimeout message and self-destruct.
519 final String termination = new ExpectMsg<String>(duration("3 seconds"), "match hint") {
520 // do not put code outside this method, will run afterwards
522 protected String match(Object in) {
523 if (in instanceof Terminated) {
529 }.get(); // this extracts the received message
531 assertEquals("match", termination);