Bug 1446: Add JMX stats for clustered data store
[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
7 import com.google.common.util.concurrent.ListeningExecutorService;
8 import com.google.common.util.concurrent.MoreExecutors;
9
10 import org.junit.BeforeClass;
11 import org.junit.Test;
12 import org.opendaylight.controller.cluster.datastore.jmx.mbeans.shard.ShardStats;
13 import org.opendaylight.controller.cluster.datastore.messages.CloseTransactionChain;
14 import org.opendaylight.controller.cluster.datastore.messages.CloseTransactionChainReply;
15 import org.opendaylight.controller.cluster.datastore.messages.CreateTransaction;
16 import org.opendaylight.controller.cluster.datastore.messages.CreateTransactionReply;
17 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
18 import org.opendaylight.controller.md.sal.dom.store.impl.InMemoryDOMDataStore;
19 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
20
21 import static org.junit.Assert.assertEquals;
22
23 public class ShardTransactionChainTest extends AbstractActorTest {
24
25     private static ListeningExecutorService storeExecutor = MoreExecutors.listeningDecorator(MoreExecutors.sameThreadExecutor());
26
27     private static final InMemoryDOMDataStore store = new InMemoryDOMDataStore("OPER", storeExecutor,
28             MoreExecutors.sameThreadExecutor());
29
30     private static final SchemaContext testSchemaContext = TestModel.createTestContext();
31
32     private static final DatastoreContext DATA_STORE_CONTEXT = new DatastoreContext();
33
34     private static final String mockShardName = "mockShardName";
35
36     private final ShardStats shardStats = new ShardStats(mockShardName, "DataStore");
37
38     @BeforeClass
39     public static void staticSetup() {
40         store.onGlobalContextUpdated(testSchemaContext);
41     }
42
43     @Test
44     public void testOnReceiveCreateTransaction() throws Exception {
45         new JavaTestKit(getSystem()) {{
46             final Props props = ShardTransactionChain.props(store.createTransactionChain(),
47                     testSchemaContext, DATA_STORE_CONTEXT, shardStats);
48             final ActorRef subject = getSystem().actorOf(props, "testCreateTransaction");
49
50             new Within(duration("1 seconds")) {
51                 @Override
52                 protected void run() {
53
54                     subject.tell(new CreateTransaction("txn-1", TransactionProxy.TransactionType.READ_ONLY.ordinal() ).toSerializable(), getRef());
55
56                     final String out = new ExpectMsg<String>(duration("1 seconds"), "match hint") {
57                         // do not put code outside this method, will run afterwards
58                         @Override
59                         protected String match(Object in) {
60                             if (in.getClass().equals(CreateTransactionReply.SERIALIZABLE_CLASS)) {
61                                 return CreateTransactionReply.fromSerializable(in).getTransactionPath();
62                             }else{
63                                 throw noMatch();
64                             }
65                         }
66                     }.get(); // this extracts the received message
67
68                     assertEquals("Unexpected transaction path " + out,
69                             "akka://test/user/testCreateTransaction/shard-txn-1",
70                             out);
71
72                     // Will wait for the rest of the 3 seconds
73                     expectNoMsg();
74                 }
75
76
77             };
78         }};
79     }
80
81     @Test
82     public void testOnReceiveCloseTransactionChain() throws Exception {
83         new JavaTestKit(getSystem()) {{
84             final Props props = ShardTransactionChain.props(store.createTransactionChain(),
85                     testSchemaContext, DATA_STORE_CONTEXT, shardStats );
86             final ActorRef subject = getSystem().actorOf(props, "testCloseTransactionChain");
87
88             new Within(duration("1 seconds")) {
89                 @Override
90                 protected void run() {
91
92                     subject.tell(new CloseTransactionChain().toSerializable(), getRef());
93
94                     final String out = new ExpectMsg<String>(duration("1 seconds"), "match hint") {
95                         // do not put code outside this method, will run afterwards
96                         @Override
97                         protected String match(Object in) {
98                             if (in.getClass().equals(CloseTransactionChainReply.SERIALIZABLE_CLASS)) {
99                                 return "match";
100                             } else {
101                                 throw noMatch();
102                             }
103                         }
104                     }.get(); // this extracts the received message
105
106                     assertEquals("match", out);
107                     // Will wait for the rest of the 3 seconds
108                     expectNoMsg();
109                 }
110
111
112             };
113         }};
114     }
115 }