Bug 1430: Off-load notifications from single commit thread
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / ShardTransactionChainTest.java
1 package org.opendaylight.controller.cluster.datastore;
2
3 import akka.actor.ActorRef;
4 import akka.actor.Props;
5 import akka.testkit.JavaTestKit;
6 import com.google.common.util.concurrent.ListeningExecutorService;
7 import com.google.common.util.concurrent.MoreExecutors;
8 import org.junit.Test;
9 import org.opendaylight.controller.cluster.datastore.messages.CloseTransactionChain;
10 import org.opendaylight.controller.cluster.datastore.messages.CloseTransactionChainReply;
11 import org.opendaylight.controller.cluster.datastore.messages.CreateTransaction;
12 import org.opendaylight.controller.cluster.datastore.messages.CreateTransactionReply;
13 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
14 import org.opendaylight.controller.md.sal.dom.store.impl.InMemoryDOMDataStore;
15
16 import static org.junit.Assert.assertEquals;
17
18 public class ShardTransactionChainTest extends AbstractActorTest {
19
20   private static ListeningExecutorService storeExecutor = MoreExecutors.listeningDecorator(MoreExecutors.sameThreadExecutor());
21
22   private static final InMemoryDOMDataStore store = new InMemoryDOMDataStore("OPER", storeExecutor,
23           MoreExecutors.sameThreadExecutor());
24
25   static {
26     store.onGlobalContextUpdated(TestModel.createTestContext());
27   }
28   @Test
29   public void testOnReceiveCreateTransaction() throws Exception {
30     new JavaTestKit(getSystem()) {{
31       final Props props = ShardTransactionChain.props(store.createTransactionChain(), TestModel.createTestContext());
32       final ActorRef subject = getSystem().actorOf(props, "testCreateTransaction");
33
34      new Within(duration("1 seconds")) {
35         @Override
36         protected void run() {
37
38           subject.tell(new CreateTransaction("txn-1", TransactionProxy.TransactionType.READ_ONLY.ordinal() ).toSerializable(), getRef());
39
40           final String out = new ExpectMsg<String>(duration("1 seconds"), "match hint") {
41             // do not put code outside this method, will run afterwards
42             @Override
43             protected String match(Object in) {
44               if (in.getClass().equals(CreateTransactionReply.SERIALIZABLE_CLASS)) {
45                 return CreateTransactionReply.fromSerializable(in).getTransactionPath();
46               }else{
47                 throw noMatch();
48               }
49             }
50           }.get(); // this extracts the received message
51
52           assertEquals("Unexpected transaction path " + out,
53               "akka://test/user/testCreateTransaction/shard-txn-1",
54               out);
55
56           // Will wait for the rest of the 3 seconds
57           expectNoMsg();
58         }
59
60
61       };
62     }};
63   }
64
65   @Test
66   public void testOnReceiveCloseTransactionChain() throws Exception {
67     new JavaTestKit(getSystem()) {{
68       final Props props = ShardTransactionChain.props(store.createTransactionChain(), TestModel.createTestContext());
69       final ActorRef subject = getSystem().actorOf(props, "testCloseTransactionChain");
70
71       new Within(duration("1 seconds")) {
72         @Override
73         protected void run() {
74
75           subject.tell(new CloseTransactionChain().toSerializable(), getRef());
76
77           final String out = new ExpectMsg<String>(duration("1 seconds"), "match hint") {
78             // do not put code outside this method, will run afterwards
79             @Override
80             protected String match(Object in) {
81               if (in.getClass().equals(CloseTransactionChainReply.SERIALIZABLE_CLASS)) {
82                 return "match";
83               } else {
84                 throw noMatch();
85               }
86             }
87           }.get(); // this extracts the received message
88
89           assertEquals("match", out);
90           // Will wait for the rest of the 3 seconds
91           expectNoMsg();
92         }
93
94
95       };
96     }};
97   }
98 }