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 DatastoreContext DATA_STORE_CONTEXT = new DatastoreContext();
33 private static final String mockShardName = "mockShardName";
36 public static void staticSetup() {
37 store.onGlobalContextUpdated(testSchemaContext);
41 public void testOnReceiveCreateTransaction() throws Exception {
42 new JavaTestKit(getSystem()) {{
43 final Props props = ShardTransactionChain.props(store.createTransactionChain(),
44 testSchemaContext, DATA_STORE_CONTEXT, mockShardName);
45 final ActorRef subject = getSystem().actorOf(props, "testCreateTransaction");
47 new Within(duration("1 seconds")) {
49 protected void run() {
51 subject.tell(new CreateTransaction("txn-1", TransactionProxy.TransactionType.READ_ONLY.ordinal() ).toSerializable(), getRef());
53 final String out = new ExpectMsg<String>(duration("1 seconds"), "match hint") {
54 // do not put code outside this method, will run afterwards
56 protected String match(Object in) {
57 if (in.getClass().equals(CreateTransactionReply.SERIALIZABLE_CLASS)) {
58 return CreateTransactionReply.fromSerializable(in).getTransactionPath();
63 }.get(); // this extracts the received message
65 assertEquals("Unexpected transaction path " + out,
66 "akka://test/user/testCreateTransaction/shard-txn-1",
69 // Will wait for the rest of the 3 seconds
79 public void testOnReceiveCloseTransactionChain() throws Exception {
80 new JavaTestKit(getSystem()) {{
81 final Props props = ShardTransactionChain.props(store.createTransactionChain(),
82 testSchemaContext, DATA_STORE_CONTEXT,mockShardName );
83 final ActorRef subject = getSystem().actorOf(props, "testCloseTransactionChain");
85 new Within(duration("1 seconds")) {
87 protected void run() {
89 subject.tell(new CloseTransactionChain().toSerializable(), getRef());
91 final String out = new ExpectMsg<String>(duration("1 seconds"), "match hint") {
92 // do not put code outside this method, will run afterwards
94 protected String match(Object in) {
95 if (in.getClass().equals(CloseTransactionChainReply.SERIALIZABLE_CLASS)) {
101 }.get(); // this extracts the received message
103 assertEquals("match", out);
104 // Will wait for the rest of the 3 seconds