import junit.framework.Assert;
import org.junit.Test;
import org.opendaylight.controller.cluster.datastore.messages.CommitTransaction;
-import org.opendaylight.controller.cluster.datastore.messages.CommitTransactionReply;
import org.opendaylight.controller.cluster.datastore.messages.CreateTransaction;
import org.opendaylight.controller.cluster.datastore.messages.CreateTransactionChain;
import org.opendaylight.controller.cluster.datastore.messages.CreateTransactionChainReply;
import org.opendaylight.controller.cluster.datastore.messages.WriteDataReply;
import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
+import scala.concurrent.Await;
+import scala.concurrent.Future;
+import scala.concurrent.duration.FiniteDuration;
+
+import java.util.Collections;
public class BasicIntegrationTest extends AbstractActorTest {
@Test
- public void integrationTest() {
+ public void integrationTest() throws Exception{
+ // System.setProperty("shard.persistent", "true");
// This test will
// - create a Shard
// - initiate a transaction
new JavaTestKit(getSystem()) {{
- final Props props = Shard.props("config");
+ final Props props = Shard.props("config", Collections.EMPTY_MAP);
final ActorRef shard = getSystem().actorOf(props);
new Within(duration("5 seconds")) {
protected void run() {
+
shard.tell(
new UpdateSchemaContext(TestModel.createTestContext()),
getRef());
- shard.tell(new CreateTransactionChain(), getRef());
+
+ // Wait for Shard to become a Leader
+ try {
+ Thread.sleep(1000);
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+ // 1. Create a TransactionChain
+ shard.tell(new CreateTransactionChain().toSerializable(), getRef());
final ActorSelection transactionChain =
- new ExpectMsg<ActorSelection>("match hint") {
+ new ExpectMsg<ActorSelection>("CreateTransactionChainReply") {
protected ActorSelection match(Object in) {
- if (in instanceof CreateTransactionChainReply) {
+ if (in.getClass().equals(CreateTransactionChainReply.SERIALIZABLE_CLASS)) {
ActorPath transactionChainPath =
- ((CreateTransactionChainReply) in)
+ CreateTransactionChainReply.fromSerializable(getSystem(),in)
.getTransactionChainPath();
return getSystem()
.actorSelection(transactionChainPath);
Assert.assertNotNull(transactionChain);
- transactionChain.tell(new CreateTransaction(), getRef());
+ System.out.println("Successfully created transaction chain");
+
+ // 2. Create a Transaction on the TransactionChain
+ transactionChain.tell(new CreateTransaction("txn-1").toSerializable(), getRef());
final ActorSelection transaction =
- new ExpectMsg<ActorSelection>("match hint") {
+ new ExpectMsg<ActorSelection>("CreateTransactionReply") {
protected ActorSelection match(Object in) {
- if (in instanceof CreateTransactionReply) {
- ActorPath transactionPath =
- ((CreateTransactionReply) in)
- .getTransactionPath();
+ if (CreateTransactionReply.SERIALIZABLE_CLASS.equals(in.getClass())) {
+ CreateTransactionReply reply = CreateTransactionReply.fromSerializable(in);
return getSystem()
- .actorSelection(transactionPath);
+ .actorSelection(reply
+ .getTransactionPath());
} else {
throw noMatch();
}
Assert.assertNotNull(transaction);
+ System.out.println("Successfully created transaction");
+
+ // 3. Write some data
transaction.tell(new WriteData(TestModel.TEST_PATH,
- ImmutableNodes.containerNode(TestModel.TEST_QNAME)),
+ ImmutableNodes.containerNode(TestModel.TEST_QNAME), TestModel.createTestContext()).toSerializable(),
getRef());
- Boolean writeDone = new ExpectMsg<Boolean>("match hint") {
+ Boolean writeDone = new ExpectMsg<Boolean>("WriteDataReply") {
protected Boolean match(Object in) {
- if (in instanceof WriteDataReply) {
+ if (in.getClass().equals(WriteDataReply.SERIALIZABLE_CLASS)) {
return true;
} else {
throw noMatch();
Assert.assertTrue(writeDone);
- transaction.tell(new ReadyTransaction(), getRef());
+ System.out.println("Successfully wrote data");
+
+ // 4. Ready the transaction for commit
+
+ transaction.tell(new ReadyTransaction().toSerializable(), getRef());
final ActorSelection cohort =
- new ExpectMsg<ActorSelection>("match hint") {
+ new ExpectMsg<ActorSelection>("ReadyTransactionReply") {
protected ActorSelection match(Object in) {
- if (in instanceof ReadyTransactionReply) {
+ if (in.getClass().equals(ReadyTransactionReply.SERIALIZABLE_CLASS)) {
ActorPath cohortPath =
- ((ReadyTransactionReply) in)
+ ReadyTransactionReply.fromSerializable(getSystem(),in)
.getCohortPath();
return getSystem()
.actorSelection(cohortPath);
Assert.assertNotNull(cohort);
- cohort.tell(new PreCommitTransaction(), getRef());
+ System.out.println("Successfully readied the transaction");
+
+ // 5. PreCommit the transaction
+
+ cohort.tell(new PreCommitTransaction().toSerializable(), getRef());
Boolean preCommitDone =
- new ExpectMsg<Boolean>("match hint") {
+ new ExpectMsg<Boolean>("PreCommitTransactionReply") {
protected Boolean match(Object in) {
- if (in instanceof PreCommitTransactionReply) {
+ if (in.getClass().equals(PreCommitTransactionReply.SERIALIZABLE_CLASS)) {
return true;
} else {
throw noMatch();
Assert.assertTrue(preCommitDone);
- cohort.tell(new CommitTransaction(), getRef());
+ System.out.println("Successfully pre-committed the transaction");
- final Boolean commitDone =
- new ExpectMsg<Boolean>("match hint") {
- protected Boolean match(Object in) {
- if (in instanceof CommitTransactionReply) {
- return true;
- } else {
- throw noMatch();
- }
- }
- }.get(); // this extracts the received message
+ // 6. Commit the transaction
+ cohort.tell(new CommitTransaction().toSerializable(), getRef());
- Assert.assertTrue(commitDone);
+ // FIXME : Add assertions that the commit worked and that the cohort and transaction actors were terminated
+ System.out.println("TODO : Check Successfully committed the transaction");
}
};
- }};
+ }
+
+ private ActorRef watchActor(ActorSelection actor) {
+ Future<ActorRef> future = actor
+ .resolveOne(FiniteDuration.apply(100, "milliseconds"));
+
+ try {
+ ActorRef actorRef = Await.result(future,
+ FiniteDuration.apply(100, "milliseconds"));
+
+ watch(actorRef);
+
+ return actorRef;
+ } catch (Exception e) {
+ throw new RuntimeException(e);
+ }
+
+ }
+ };
}