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.messages.CloseTransactionChain;
13 import org.opendaylight.controller.cluster.datastore.messages.CloseTransactionChainReply;
14 import org.opendaylight.controller.cluster.datastore.messages.CreateTransaction;
15 import org.opendaylight.controller.cluster.datastore.messages.CreateTransactionReply;
16 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
17 import org.opendaylight.controller.md.sal.dom.store.impl.InMemoryDOMDataStore;
18 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
20 import static org.junit.Assert.assertEquals;
22 public class ShardTransactionChainTest extends AbstractActorTest {
24 private static ListeningExecutorService storeExecutor = MoreExecutors.listeningDecorator(MoreExecutors.sameThreadExecutor());
26 private static final InMemoryDOMDataStore store = new InMemoryDOMDataStore("OPER", storeExecutor,
27 MoreExecutors.sameThreadExecutor());
29 private static final SchemaContext testSchemaContext = TestModel.createTestContext();
31 private static final ShardContext shardContext = new ShardContext();
34 public static void staticSetup() {
35 store.onGlobalContextUpdated(testSchemaContext);
39 public void testOnReceiveCreateTransaction() throws Exception {
40 new JavaTestKit(getSystem()) {{
41 final Props props = ShardTransactionChain.props(store.createTransactionChain(),
42 testSchemaContext, shardContext);
43 final ActorRef subject = getSystem().actorOf(props, "testCreateTransaction");
45 new Within(duration("1 seconds")) {
47 protected void run() {
49 subject.tell(new CreateTransaction("txn-1", TransactionProxy.TransactionType.READ_ONLY.ordinal() ).toSerializable(), getRef());
51 final String out = new ExpectMsg<String>(duration("1 seconds"), "match hint") {
52 // do not put code outside this method, will run afterwards
54 protected String match(Object in) {
55 if (in.getClass().equals(CreateTransactionReply.SERIALIZABLE_CLASS)) {
56 return CreateTransactionReply.fromSerializable(in).getTransactionPath();
61 }.get(); // this extracts the received message
63 assertEquals("Unexpected transaction path " + out,
64 "akka://test/user/testCreateTransaction/shard-txn-1",
67 // Will wait for the rest of the 3 seconds
77 public void testOnReceiveCloseTransactionChain() throws Exception {
78 new JavaTestKit(getSystem()) {{
79 final Props props = ShardTransactionChain.props(store.createTransactionChain(),
80 testSchemaContext, shardContext);
81 final ActorRef subject = getSystem().actorOf(props, "testCloseTransactionChain");
83 new Within(duration("1 seconds")) {
85 protected void run() {
87 subject.tell(new CloseTransactionChain().toSerializable(), getRef());
89 final String out = new ExpectMsg<String>(duration("1 seconds"), "match hint") {
90 // do not put code outside this method, will run afterwards
92 protected String match(Object in) {
93 if (in.getClass().equals(CloseTransactionChainReply.SERIALIZABLE_CLASS)) {
99 }.get(); // this extracts the received message
101 assertEquals("match", out);
102 // Will wait for the rest of the 3 seconds