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.CreateTransactionReply;
22 import org.opendaylight.controller.cluster.datastore.messages.PreCommitTransaction;
23 import org.opendaylight.controller.cluster.datastore.messages.PreCommitTransactionReply;
24 import org.opendaylight.controller.cluster.datastore.messages.ReadyTransaction;
25 import org.opendaylight.controller.cluster.datastore.messages.ReadyTransactionReply;
26 import org.opendaylight.controller.cluster.datastore.messages.UpdateSchemaContext;
27 import org.opendaylight.controller.cluster.datastore.messages.WriteData;
28 import org.opendaylight.controller.cluster.datastore.messages.WriteDataReply;
29 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
30 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
31 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
32 import scala.concurrent.Await;
33 import scala.concurrent.Future;
34 import scala.concurrent.duration.FiniteDuration;
36 import java.util.Collections;
38 import static junit.framework.Assert.assertEquals;
39 import static junit.framework.Assert.assertTrue;
40 import static org.junit.Assert.assertNotNull;
42 public class BasicIntegrationTest extends AbstractActorTest {
45 public void integrationTest() throws Exception{
46 // System.setProperty("shard.persistent", "true");
49 // - initiate a transaction
51 // - read the transaction for commit
52 // - commit the transaction
55 new JavaTestKit(getSystem()) {{
56 final ShardIdentifier identifier =
57 ShardIdentifier.builder().memberName("member-1")
58 .shardName("inventory").type("config").build();
60 final SchemaContext schemaContext = TestModel.createTestContext();
61 DatastoreContext datastoreContext = new DatastoreContext();
63 final Props props = Shard.props(identifier, Collections.EMPTY_MAP, datastoreContext, TestModel.createTestContext());
64 final ActorRef shard = getSystem().actorOf(props);
66 new Within(duration("10 seconds")) {
68 protected void run() {
69 shard.tell(new UpdateSchemaContext(schemaContext), getRef());
72 // Wait for a specific log message to show up
73 final boolean result =
74 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 // Create a transaction on the shard
87 shard.tell(new CreateTransaction("txn-1", TransactionProxy.TransactionType.WRITE_ONLY.ordinal() ).toSerializable(), getRef());
89 final ActorSelection transaction =
90 new ExpectMsg<ActorSelection>(duration("3 seconds"), "CreateTransactionReply") {
92 protected ActorSelection match(Object in) {
93 if (CreateTransactionReply.SERIALIZABLE_CLASS.equals(in.getClass())) {
94 CreateTransactionReply reply = CreateTransactionReply.fromSerializable(in);
97 .getTransactionPath());
102 }.get(); // this extracts the received message
104 assertNotNull(transaction);
106 System.out.println("Successfully created transaction");
108 // 3. Write some data
109 transaction.tell(new WriteData(TestModel.TEST_PATH,
110 ImmutableNodes.containerNode(TestModel.TEST_QNAME), schemaContext).toSerializable(),
113 Boolean writeDone = new ExpectMsg<Boolean>(duration("3 seconds"), "WriteDataReply") {
115 protected Boolean match(Object in) {
116 if (in.getClass().equals(WriteDataReply.SERIALIZABLE_CLASS)) {
122 }.get(); // this extracts the received message
124 assertTrue(writeDone);
126 System.out.println("Successfully wrote data");
128 // 4. Ready the transaction for commit
130 transaction.tell(new ReadyTransaction().toSerializable(), getRef());
132 final ActorSelection cohort =
133 new ExpectMsg<ActorSelection>(duration("3 seconds"), "ReadyTransactionReply") {
135 protected ActorSelection match(Object in) {
136 if (in.getClass().equals(ReadyTransactionReply.SERIALIZABLE_CLASS)) {
137 ActorPath cohortPath =
138 ReadyTransactionReply.fromSerializable(getSystem(),in)
141 .actorSelection(cohortPath);
146 }.get(); // this extracts the received message
148 assertNotNull(cohort);
150 System.out.println("Successfully readied the transaction");
152 // 5. PreCommit the transaction
154 cohort.tell(new PreCommitTransaction().toSerializable(), getRef());
156 Boolean preCommitDone =
157 new ExpectMsg<Boolean>(duration("3 seconds"), "PreCommitTransactionReply") {
159 protected Boolean match(Object in) {
160 if (in.getClass().equals(PreCommitTransactionReply.SERIALIZABLE_CLASS)) {
166 }.get(); // this extracts the received message
168 assertTrue(preCommitDone);
170 System.out.println("Successfully pre-committed the transaction");
172 // 6. Commit the transaction
173 cohort.tell(new CommitTransaction().toSerializable(), getRef());
175 // FIXME : Add assertions that the commit worked and that the cohort and transaction actors were terminated
177 System.out.println("TODO : Check Successfully committed the transaction");
184 private ActorRef watchActor(ActorSelection actor) {
185 Future<ActorRef> future = actor
186 .resolveOne(FiniteDuration.apply(100, "milliseconds"));
189 ActorRef actorRef = Await.result(future,
190 FiniteDuration.apply(100, "milliseconds"));
195 } catch (Exception e) {
196 throw new RuntimeException(e);