1 package org.opendaylight.controller.cluster.datastore;
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;
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;
16 import static org.junit.Assert.assertEquals;
18 public class ShardTransactionChainTest extends AbstractActorTest {
20 private static ListeningExecutorService storeExecutor = MoreExecutors.listeningDecorator(MoreExecutors.sameThreadExecutor());
22 private static final InMemoryDOMDataStore store = new InMemoryDOMDataStore("OPER", storeExecutor,
23 MoreExecutors.sameThreadExecutor());
26 store.onGlobalContextUpdated(TestModel.createTestContext());
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");
34 new Within(duration("1 seconds")) {
36 protected void run() {
38 subject.tell(new CreateTransaction("txn-1", TransactionProxy.TransactionType.READ_ONLY.ordinal() ).toSerializable(), getRef());
40 final String out = new ExpectMsg<String>(duration("1 seconds"), "match hint") {
41 // do not put code outside this method, will run afterwards
43 protected String match(Object in) {
44 if (in.getClass().equals(CreateTransactionReply.SERIALIZABLE_CLASS)) {
45 return CreateTransactionReply.fromSerializable(in).getTransactionPath();
50 }.get(); // this extracts the received message
52 assertEquals("Unexpected transaction path " + out,
53 "akka://test/user/testCreateTransaction/shard-txn-1",
56 // Will wait for the rest of the 3 seconds
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");
71 new Within(duration("1 seconds")) {
73 protected void run() {
75 subject.tell(new CloseTransactionChain().toSerializable(), getRef());
77 final String out = new ExpectMsg<String>(duration("1 seconds"), "match hint") {
78 // do not put code outside this method, will run afterwards
80 protected String match(Object in) {
81 if (in.getClass().equals(CloseTransactionChainReply.SERIALIZABLE_CLASS)) {
87 }.get(); // this extracts the received message
89 assertEquals("match", out);
90 // Will wait for the rest of the 3 seconds