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 org.junit.Test;
18 import org.opendaylight.controller.cluster.datastore.identifiers.ShardIdentifier;
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;
40 import static junit.framework.Assert.assertTrue;
41 import static org.junit.Assert.assertNotNull;
43 public class BasicIntegrationTest extends AbstractActorTest {
46 public void integrationTest() throws Exception{
47 // System.setProperty("shard.persistent", "true");
50 // - initiate a transaction
52 // - read the transaction for commit
53 // - commit the transaction
56 new JavaTestKit(getSystem()) {{
57 final ShardIdentifier identifier =
58 ShardIdentifier.builder().memberName("member-1")
59 .shardName("inventory").type("config").build();
61 final Props props = Shard.props(identifier, Collections.EMPTY_MAP);
62 final ActorRef shard = getSystem().actorOf(props);
64 new Within(duration("5 seconds")) {
65 protected void run() {
69 new UpdateSchemaContext(TestModel.createTestContext()),
73 // Wait for a specific log message to show up
74 final boolean result =
75 new JavaTestKit.EventFilter<Boolean>(Logging.Info.class
77 protected Boolean run() {
80 }.from(shard.path().toString())
81 .message("Switching from state Candidate to Leader")
82 .occurrences(1).exec();
84 assertEquals(true, result);
86 // 1. Create a TransactionChain
87 shard.tell(new CreateTransactionChain().toSerializable(), getRef());
89 final ActorSelection transactionChain =
90 new ExpectMsg<ActorSelection>(duration("1 seconds"), "CreateTransactionChainReply") {
91 protected ActorSelection match(Object in) {
92 if (in.getClass().equals(CreateTransactionChainReply.SERIALIZABLE_CLASS)) {
93 ActorPath transactionChainPath =
94 CreateTransactionChainReply.fromSerializable(getSystem(),in)
95 .getTransactionChainPath();
97 .actorSelection(transactionChainPath);
102 }.get(); // this extracts the received message
104 assertNotNull(transactionChain);
106 System.out.println("Successfully created transaction chain");
108 // 2. Create a Transaction on the TransactionChain
109 transactionChain.tell(new CreateTransaction("txn-1", TransactionProxy.TransactionType.WRITE_ONLY.ordinal() ).toSerializable(), getRef());
111 final ActorSelection transaction =
112 new ExpectMsg<ActorSelection>(duration("1 seconds"), "CreateTransactionReply") {
113 protected ActorSelection match(Object in) {
114 if (CreateTransactionReply.SERIALIZABLE_CLASS.equals(in.getClass())) {
115 CreateTransactionReply reply = CreateTransactionReply.fromSerializable(in);
117 .actorSelection(reply
118 .getTransactionPath());
123 }.get(); // this extracts the received message
125 assertNotNull(transaction);
127 System.out.println("Successfully created transaction");
129 // 3. Write some data
130 transaction.tell(new WriteData(TestModel.TEST_PATH,
131 ImmutableNodes.containerNode(TestModel.TEST_QNAME), TestModel.createTestContext()).toSerializable(),
134 Boolean writeDone = new ExpectMsg<Boolean>(duration("1 seconds"), "WriteDataReply") {
135 protected Boolean match(Object in) {
136 if (in.getClass().equals(WriteDataReply.SERIALIZABLE_CLASS)) {
142 }.get(); // this extracts the received message
144 assertTrue(writeDone);
146 System.out.println("Successfully wrote data");
148 // 4. Ready the transaction for commit
150 transaction.tell(new ReadyTransaction().toSerializable(), getRef());
152 final ActorSelection cohort =
153 new ExpectMsg<ActorSelection>(duration("1 seconds"), "ReadyTransactionReply") {
154 protected ActorSelection match(Object in) {
155 if (in.getClass().equals(ReadyTransactionReply.SERIALIZABLE_CLASS)) {
156 ActorPath cohortPath =
157 ReadyTransactionReply.fromSerializable(getSystem(),in)
160 .actorSelection(cohortPath);
165 }.get(); // this extracts the received message
167 assertNotNull(cohort);
169 System.out.println("Successfully readied the transaction");
171 // 5. PreCommit the transaction
173 cohort.tell(new PreCommitTransaction().toSerializable(), getRef());
175 Boolean preCommitDone =
176 new ExpectMsg<Boolean>(duration("1 seconds"), "PreCommitTransactionReply") {
177 protected Boolean match(Object in) {
178 if (in.getClass().equals(PreCommitTransactionReply.SERIALIZABLE_CLASS)) {
184 }.get(); // this extracts the received message
186 assertTrue(preCommitDone);
188 System.out.println("Successfully pre-committed the transaction");
190 // 6. Commit the transaction
191 cohort.tell(new CommitTransaction().toSerializable(), getRef());
193 // FIXME : Add assertions that the commit worked and that the cohort and transaction actors were terminated
195 System.out.println("TODO : Check Successfully committed the transaction");
202 private ActorRef watchActor(ActorSelection actor) {
203 Future<ActorRef> future = actor
204 .resolveOne(FiniteDuration.apply(100, "milliseconds"));
207 ActorRef actorRef = Await.result(future,
208 FiniteDuration.apply(100, "milliseconds"));
213 } catch (Exception e) {
214 throw new RuntimeException(e);