1 package org.opendaylight.controller.cluster.datastore;
3 import akka.actor.ActorRef;
4 import akka.actor.Props;
5 import akka.testkit.JavaTestKit;
7 import com.google.common.util.concurrent.ListeningExecutorService;
8 import com.google.common.util.concurrent.MoreExecutors;
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;
21 import static org.junit.Assert.assertEquals;
23 public class ShardTransactionChainTest extends AbstractActorTest {
25 private static ListeningExecutorService storeExecutor = MoreExecutors.listeningDecorator(MoreExecutors.sameThreadExecutor());
27 private static final InMemoryDOMDataStore store = new InMemoryDOMDataStore("OPER", storeExecutor,
28 MoreExecutors.sameThreadExecutor());
30 private static final SchemaContext testSchemaContext = TestModel.createTestContext();
32 private static final DatastoreContext DATA_STORE_CONTEXT = new DatastoreContext();
34 private static final String mockShardName = "mockShardName";
36 private final ShardStats shardStats = new ShardStats(mockShardName, "DataStore");
39 public static void staticSetup() {
40 store.onGlobalContextUpdated(testSchemaContext);
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");
50 new Within(duration("1 seconds")) {
52 protected void run() {
54 subject.tell(new CreateTransaction("txn-1", TransactionProxy.TransactionType.READ_ONLY.ordinal() ).toSerializable(), getRef());
56 final String out = new ExpectMsg<String>(duration("1 seconds"), "match hint") {
57 // do not put code outside this method, will run afterwards
59 protected String match(Object in) {
60 if (in.getClass().equals(CreateTransactionReply.SERIALIZABLE_CLASS)) {
61 return CreateTransactionReply.fromSerializable(in).getTransactionPath();
66 }.get(); // this extracts the received message
68 assertEquals("Unexpected transaction path " + out,
69 "akka://test/user/testCreateTransaction/shard-txn-1",
72 // Will wait for the rest of the 3 seconds
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");
88 new Within(duration("1 seconds")) {
90 protected void run() {
92 subject.tell(new CloseTransactionChain().toSerializable(), getRef());
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(CloseTransactionChainReply.SERIALIZABLE_CLASS)) {
104 }.get(); // this extracts the received message
106 assertEquals("match", out);
107 // Will wait for the rest of the 3 seconds