2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.controller.cluster.datastore;
11 import akka.actor.ActorPath;
12 import akka.actor.ActorRef;
13 import akka.actor.ActorSelection;
14 import akka.actor.Props;
15 import akka.event.Logging;
16 import akka.testkit.JavaTestKit;
17 import junit.framework.Assert;
18 import org.junit.Test;
19 import org.opendaylight.controller.cluster.datastore.messages.CommitTransaction;
20 import org.opendaylight.controller.cluster.datastore.messages.CreateTransaction;
21 import org.opendaylight.controller.cluster.datastore.messages.CreateTransactionChain;
22 import org.opendaylight.controller.cluster.datastore.messages.CreateTransactionChainReply;
23 import org.opendaylight.controller.cluster.datastore.messages.CreateTransactionReply;
24 import org.opendaylight.controller.cluster.datastore.messages.PreCommitTransaction;
25 import org.opendaylight.controller.cluster.datastore.messages.PreCommitTransactionReply;
26 import org.opendaylight.controller.cluster.datastore.messages.ReadyTransaction;
27 import org.opendaylight.controller.cluster.datastore.messages.ReadyTransactionReply;
28 import org.opendaylight.controller.cluster.datastore.messages.UpdateSchemaContext;
29 import org.opendaylight.controller.cluster.datastore.messages.WriteData;
30 import org.opendaylight.controller.cluster.datastore.messages.WriteDataReply;
31 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
32 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
33 import scala.concurrent.Await;
34 import scala.concurrent.Future;
35 import scala.concurrent.duration.FiniteDuration;
37 import java.util.Collections;
39 import static junit.framework.Assert.assertEquals;
41 public class BasicIntegrationTest extends AbstractActorTest {
44 public void integrationTest() throws Exception{
45 // System.setProperty("shard.persistent", "true");
48 // - initiate a transaction
50 // - read the transaction for commit
51 // - commit the transaction
54 new JavaTestKit(getSystem()) {{
55 final Props props = Shard.props("config", Collections.EMPTY_MAP);
56 final ActorRef shard = getSystem().actorOf(props);
58 new Within(duration("5 seconds")) {
59 protected void run() {
63 new UpdateSchemaContext(TestModel.createTestContext()),
67 // Wait for a specific log message to show up
68 final boolean result =
69 new JavaTestKit.EventFilter<Boolean>(Logging.Info.class
71 protected Boolean run() {
74 }.from(shard.path().toString())
75 .message("Switching from state Candidate to Leader")
76 .occurrences(1).exec();
78 assertEquals(true, result);
80 // 1. Create a TransactionChain
81 shard.tell(new CreateTransactionChain().toSerializable(), getRef());
83 final ActorSelection transactionChain =
84 new ExpectMsg<ActorSelection>(duration("1 seconds"), "CreateTransactionChainReply") {
85 protected ActorSelection match(Object in) {
86 if (in.getClass().equals(CreateTransactionChainReply.SERIALIZABLE_CLASS)) {
87 ActorPath transactionChainPath =
88 CreateTransactionChainReply.fromSerializable(getSystem(),in)
89 .getTransactionChainPath();
91 .actorSelection(transactionChainPath);
96 }.get(); // this extracts the received message
98 Assert.assertNotNull(transactionChain);
100 System.out.println("Successfully created transaction chain");
102 // 2. Create a Transaction on the TransactionChain
103 transactionChain.tell(new CreateTransaction("txn-1", TransactionProxy.TransactionType.WRITE_ONLY.ordinal() ).toSerializable(), getRef());
105 final ActorSelection transaction =
106 new ExpectMsg<ActorSelection>(duration("1 seconds"), "CreateTransactionReply") {
107 protected ActorSelection match(Object in) {
108 if (CreateTransactionReply.SERIALIZABLE_CLASS.equals(in.getClass())) {
109 CreateTransactionReply reply = CreateTransactionReply.fromSerializable(in);
111 .actorSelection(reply
112 .getTransactionPath());
117 }.get(); // this extracts the received message
119 Assert.assertNotNull(transaction);
121 System.out.println("Successfully created transaction");
123 // 3. Write some data
124 transaction.tell(new WriteData(TestModel.TEST_PATH,
125 ImmutableNodes.containerNode(TestModel.TEST_QNAME), TestModel.createTestContext()).toSerializable(),
128 Boolean writeDone = new ExpectMsg<Boolean>(duration("1 seconds"), "WriteDataReply") {
129 protected Boolean match(Object in) {
130 if (in.getClass().equals(WriteDataReply.SERIALIZABLE_CLASS)) {
136 }.get(); // this extracts the received message
138 Assert.assertTrue(writeDone);
140 System.out.println("Successfully wrote data");
142 // 4. Ready the transaction for commit
144 transaction.tell(new ReadyTransaction().toSerializable(), getRef());
146 final ActorSelection cohort =
147 new ExpectMsg<ActorSelection>(duration("1 seconds"), "ReadyTransactionReply") {
148 protected ActorSelection match(Object in) {
149 if (in.getClass().equals(ReadyTransactionReply.SERIALIZABLE_CLASS)) {
150 ActorPath cohortPath =
151 ReadyTransactionReply.fromSerializable(getSystem(),in)
154 .actorSelection(cohortPath);
159 }.get(); // this extracts the received message
161 Assert.assertNotNull(cohort);
163 System.out.println("Successfully readied the transaction");
165 // 5. PreCommit the transaction
167 cohort.tell(new PreCommitTransaction().toSerializable(), getRef());
169 Boolean preCommitDone =
170 new ExpectMsg<Boolean>(duration("1 seconds"), "PreCommitTransactionReply") {
171 protected Boolean match(Object in) {
172 if (in.getClass().equals(PreCommitTransactionReply.SERIALIZABLE_CLASS)) {
178 }.get(); // this extracts the received message
180 Assert.assertTrue(preCommitDone);
182 System.out.println("Successfully pre-committed the transaction");
184 // 6. Commit the transaction
185 cohort.tell(new CommitTransaction().toSerializable(), getRef());
187 // FIXME : Add assertions that the commit worked and that the cohort and transaction actors were terminated
189 System.out.println("TODO : Check Successfully committed the transaction");
196 private ActorRef watchActor(ActorSelection actor) {
197 Future<ActorRef> future = actor
198 .resolveOne(FiniteDuration.apply(100, "milliseconds"));
201 ActorRef actorRef = Await.result(future,
202 FiniteDuration.apply(100, "milliseconds"));
207 } catch (Exception e) {
208 throw new RuntimeException(e);