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;
18 import org.junit.Test;
19 import org.opendaylight.controller.cluster.datastore.identifiers.ShardIdentifier;
20 import org.opendaylight.controller.cluster.datastore.messages.CommitTransaction;
21 import org.opendaylight.controller.cluster.datastore.messages.CreateTransaction;
22 import org.opendaylight.controller.cluster.datastore.messages.CreateTransactionChain;
23 import org.opendaylight.controller.cluster.datastore.messages.CreateTransactionChainReply;
24 import org.opendaylight.controller.cluster.datastore.messages.CreateTransactionReply;
25 import org.opendaylight.controller.cluster.datastore.messages.PreCommitTransaction;
26 import org.opendaylight.controller.cluster.datastore.messages.PreCommitTransactionReply;
27 import org.opendaylight.controller.cluster.datastore.messages.ReadyTransaction;
28 import org.opendaylight.controller.cluster.datastore.messages.ReadyTransactionReply;
29 import org.opendaylight.controller.cluster.datastore.messages.UpdateSchemaContext;
30 import org.opendaylight.controller.cluster.datastore.messages.WriteData;
31 import org.opendaylight.controller.cluster.datastore.messages.WriteDataReply;
32 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
33 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
34 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
36 import scala.concurrent.Await;
37 import scala.concurrent.Future;
38 import scala.concurrent.duration.FiniteDuration;
40 import java.util.Collections;
42 import static junit.framework.Assert.assertEquals;
43 import static junit.framework.Assert.assertTrue;
44 import static org.junit.Assert.assertNotNull;
46 public class BasicIntegrationTest extends AbstractActorTest {
49 public void integrationTest() throws Exception{
50 // System.setProperty("shard.persistent", "true");
53 // - initiate a transaction
55 // - read the transaction for commit
56 // - commit the transaction
59 new JavaTestKit(getSystem()) {{
60 final ShardIdentifier identifier =
61 ShardIdentifier.builder().memberName("member-1")
62 .shardName("inventory").type("config").build();
64 final SchemaContext schemaContext = TestModel.createTestContext();
65 DatastoreContext datastoreContext = new DatastoreContext();
67 final Props props = Shard.props(identifier, Collections.EMPTY_MAP, datastoreContext);
68 final ActorRef shard = getSystem().actorOf(props);
70 new Within(duration("10 seconds")) {
72 protected void run() {
73 shard.tell(new UpdateSchemaContext(schemaContext), getRef());
76 // Wait for a specific log message to show up
77 final boolean result =
78 new JavaTestKit.EventFilter<Boolean>(Logging.Info.class
81 protected Boolean run() {
84 }.from(shard.path().toString())
85 .message("Switching from state Candidate to Leader")
86 .occurrences(1).exec();
88 assertEquals(true, result);
90 // 1. Create a TransactionChain
91 shard.tell(new CreateTransactionChain().toSerializable(), getRef());
93 final ActorSelection transactionChain =
94 new ExpectMsg<ActorSelection>(duration("3 seconds"), "CreateTransactionChainReply") {
96 protected ActorSelection match(Object in) {
97 if (in.getClass().equals(CreateTransactionChainReply.SERIALIZABLE_CLASS)) {
98 ActorPath transactionChainPath =
99 CreateTransactionChainReply.fromSerializable(getSystem(),in)
100 .getTransactionChainPath();
102 .actorSelection(transactionChainPath);
107 }.get(); // this extracts the received message
109 assertNotNull(transactionChain);
111 System.out.println("Successfully created transaction chain");
113 // 2. Create a Transaction on the TransactionChain
114 transactionChain.tell(new CreateTransaction("txn-1", TransactionProxy.TransactionType.WRITE_ONLY.ordinal() ).toSerializable(), getRef());
116 final ActorSelection transaction =
117 new ExpectMsg<ActorSelection>(duration("3 seconds"), "CreateTransactionReply") {
119 protected ActorSelection match(Object in) {
120 if (CreateTransactionReply.SERIALIZABLE_CLASS.equals(in.getClass())) {
121 CreateTransactionReply reply = CreateTransactionReply.fromSerializable(in);
123 .actorSelection(reply
124 .getTransactionPath());
129 }.get(); // this extracts the received message
131 assertNotNull(transaction);
133 System.out.println("Successfully created transaction");
135 // 3. Write some data
136 transaction.tell(new WriteData(TestModel.TEST_PATH,
137 ImmutableNodes.containerNode(TestModel.TEST_QNAME), schemaContext).toSerializable(),
140 Boolean writeDone = new ExpectMsg<Boolean>(duration("3 seconds"), "WriteDataReply") {
142 protected Boolean match(Object in) {
143 if (in.getClass().equals(WriteDataReply.SERIALIZABLE_CLASS)) {
149 }.get(); // this extracts the received message
151 assertTrue(writeDone);
153 System.out.println("Successfully wrote data");
155 // 4. Ready the transaction for commit
157 transaction.tell(new ReadyTransaction().toSerializable(), getRef());
159 final ActorSelection cohort =
160 new ExpectMsg<ActorSelection>(duration("3 seconds"), "ReadyTransactionReply") {
162 protected ActorSelection match(Object in) {
163 if (in.getClass().equals(ReadyTransactionReply.SERIALIZABLE_CLASS)) {
164 ActorPath cohortPath =
165 ReadyTransactionReply.fromSerializable(getSystem(),in)
168 .actorSelection(cohortPath);
173 }.get(); // this extracts the received message
175 assertNotNull(cohort);
177 System.out.println("Successfully readied the transaction");
179 // 5. PreCommit the transaction
181 cohort.tell(new PreCommitTransaction().toSerializable(), getRef());
183 Boolean preCommitDone =
184 new ExpectMsg<Boolean>(duration("3 seconds"), "PreCommitTransactionReply") {
186 protected Boolean match(Object in) {
187 if (in.getClass().equals(PreCommitTransactionReply.SERIALIZABLE_CLASS)) {
193 }.get(); // this extracts the received message
195 assertTrue(preCommitDone);
197 System.out.println("Successfully pre-committed the transaction");
199 // 6. Commit the transaction
200 cohort.tell(new CommitTransaction().toSerializable(), getRef());
202 // FIXME : Add assertions that the commit worked and that the cohort and transaction actors were terminated
204 System.out.println("TODO : Check Successfully committed the transaction");
211 private ActorRef watchActor(ActorSelection actor) {
212 Future<ActorRef> future = actor
213 .resolveOne(FiniteDuration.apply(100, "milliseconds"));
216 ActorRef actorRef = Await.result(future,
217 FiniteDuration.apply(100, "milliseconds"));
222 } catch (Exception e) {
223 throw new RuntimeException(e);