// Wait for leader's install snapshot and capture it
- Object installSnapshot = expectFirstMatching(newFollowerCollectorActor, InstallSnapshot.class);
+ InstallSnapshot installSnapshot = expectFirstMatching(newFollowerCollectorActor, InstallSnapshot.class);
// Send a second AddServer - should get queued
JavaTestKit testKit2 = new JavaTestKit(getSystem());
}
<T> T getCapturedMessage(Class<T> msgClass) throws Exception {
- Object message = getFirstMatching(getSelf(), msgClass);
+ T message = getFirstMatching(getSelf(), msgClass);
assertNotNull("Message of type " + msgClass + " not received", message);
- return (T) message;
+ return message;
}
}
* and is available at http://www.eclipse.org/legal/epl-v10.html
*/
-package org.opendaylight.controller.cluster.datastore.utils;
+package org.opendaylight.controller.cluster.raft.utils;
import akka.actor.UntypedActor;
/**
* The EchoActor simply responds back with the same message that it receives
*/
-public class EchoActor extends UntypedActor{
+public class EchoActor extends UntypedActor {
- @Override public void onReceive(Object message) throws Exception {
+ @Override
+ public void onReceive(Object message) throws Exception {
getSender().tell(message, getSelf());
}
}
public class MessageCollectorActor extends UntypedActor {
private static final String ARE_YOU_READY = "ARE_YOU_READY";
- private static final String GET_ALL_MESSAGES = "get-all-messages";
+ public static final String GET_ALL_MESSAGES = "messages";
private static final String CLEAR_MESSAGES = "clear-messages";
private final List<Object> messages = new ArrayList<>();
messages.clear();
}
- public static List<Object> getAllMessages(ActorRef actor) throws Exception {
+ private static List<Object> getAllMessages(ActorRef actor) throws Exception {
FiniteDuration operationDuration = Duration.create(5, TimeUnit.SECONDS);
Timeout operationTimeout = new Timeout(operationDuration);
Future<Object> future = Patterns.ask(actor, GET_ALL_MESSAGES, operationTimeout);
for(Object message : allMessages){
if(message.getClass().equals(clazz)){
- return (T) message;
+ return clazz.cast(message);
}
}
for(Object message : allMessages){
if(message.getClass().equals(clazz)){
- output.add((T) message);
+ output.add(clazz.cast(message));
}
}
import org.opendaylight.controller.cluster.datastore.shardstrategy.ShardStrategy;
import org.opendaylight.controller.cluster.datastore.shardstrategy.ShardStrategyFactory;
import org.opendaylight.controller.cluster.datastore.utils.ActorContext;
-import org.opendaylight.controller.cluster.datastore.utils.DoNothingActor;
import org.opendaylight.controller.cluster.datastore.utils.MockConfiguration;
+import org.opendaylight.controller.cluster.raft.utils.DoNothingActor;
import org.opendaylight.controller.md.cluster.datastore.model.CarsModel;
import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
import org.junit.Test;
import org.opendaylight.controller.cluster.datastore.messages.DataChanged;
import org.opendaylight.controller.cluster.datastore.utils.ActorContext;
-import org.opendaylight.controller.cluster.datastore.utils.DoNothingActor;
-import org.opendaylight.controller.cluster.datastore.utils.MessageCollectorActor;
import org.opendaylight.controller.cluster.datastore.utils.MockClusterWrapper;
import org.opendaylight.controller.cluster.datastore.utils.MockConfiguration;
+import org.opendaylight.controller.cluster.raft.utils.DoNothingActor;
+import org.opendaylight.controller.cluster.raft.utils.MessageCollectorActor;
import org.opendaylight.controller.md.cluster.datastore.model.CompositeModel;
import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
ActorContext
testContext = new ActorContext(getSystem(), getSystem().actorOf(Props.create(DoNothingActor.class)), new MockClusterWrapper(), new MockConfiguration());
Object messages = testContext
- .executeOperation(actorRef, "messages");
+ .executeOperation(actorRef, MessageCollectorActor.GET_ALL_MESSAGES);
Assert.assertNotNull(messages);
import org.opendaylight.controller.cluster.datastore.messages.RegisterChangeListenerReply;
import org.opendaylight.controller.cluster.datastore.utils.ActorContext;
import org.opendaylight.controller.cluster.datastore.utils.Dispatchers;
-import org.opendaylight.controller.cluster.datastore.utils.DoNothingActor;
+import org.opendaylight.controller.cluster.raft.utils.DoNothingActor;
import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker;
import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
import org.opendaylight.controller.cluster.datastore.messages.RegisterDataTreeChangeListenerReply;
import org.opendaylight.controller.cluster.datastore.utils.ActorContext;
import org.opendaylight.controller.cluster.datastore.utils.Dispatchers;
-import org.opendaylight.controller.cluster.datastore.utils.DoNothingActor;
+import org.opendaylight.controller.cluster.raft.utils.DoNothingActor;
import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
import org.opendaylight.controller.md.sal.dom.api.ClusteredDOMDataTreeChangeListener;
import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeChangeListener;
import org.junit.Test;
import org.mockito.Mockito;
import org.opendaylight.controller.cluster.datastore.messages.DataTreeChanged;
-import org.opendaylight.controller.cluster.datastore.utils.MessageCollectorActor;
+import org.opendaylight.controller.cluster.raft.utils.MessageCollectorActor;
import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
public class ForwardingDataTreeChangeListenerTest extends AbstractActorTest {
Collection<DataTreeCandidate> expected = Arrays.asList(Mockito.mock(DataTreeCandidate.class));
forwardingListener.onDataTreeChanged(expected);
- DataTreeChanged actual = MessageCollectorActor.expectFirstMatching(actorRef, DataTreeChanged.class);
+ DataTreeChanged actual = MessageCollectorActor.expectFirstMatching(actorRef, DataTreeChanged.class, 5000);
Assert.assertSame(expected, actual.getChanges());
}
}
import akka.testkit.JavaTestKit;
import akka.testkit.TestActorRef;
import org.junit.Test;
-import org.opendaylight.controller.cluster.datastore.utils.MessageCollectorActor;
import org.opendaylight.controller.cluster.notifications.LeaderStateChanged;
import org.opendaylight.controller.cluster.notifications.RegisterRoleChangeListener;
import org.opendaylight.controller.cluster.notifications.RegisterRoleChangeListenerReply;
import org.opendaylight.controller.cluster.notifications.RoleChangeNotifier;
import org.opendaylight.controller.cluster.notifications.RoleChanged;
import org.opendaylight.controller.cluster.raft.RaftState;
+import org.opendaylight.controller.cluster.raft.utils.MessageCollectorActor;
public class RoleChangeNotifierTest extends AbstractActorTest {
notifierTestActorRef.tell(new RegisterRoleChangeListener(), listenerActor);
- RegisterRoleChangeListenerReply reply = (RegisterRoleChangeListenerReply)
+ RegisterRoleChangeListenerReply reply =
MessageCollectorActor.getFirstMatching(listenerActor, RegisterRoleChangeListenerReply.class);
assertNotNull(reply);
- RoleChangeNotification notification = (RoleChangeNotification)
+ RoleChangeNotification notification =
MessageCollectorActor.getFirstMatching(listenerActor, RoleChangeNotification.class);
assertNull(notification);
}};
// listener registers after role has been changed, ensure we sent the latest role change after a reply
notifierTestActorRef.tell(new RegisterRoleChangeListener(), listenerActor);
- RegisterRoleChangeListenerReply reply = (RegisterRoleChangeListenerReply)
+ RegisterRoleChangeListenerReply reply =
MessageCollectorActor.getFirstMatching(listenerActor, RegisterRoleChangeListenerReply.class);
assertNotNull(reply);
- RoleChangeNotification notification = (RoleChangeNotification)
+ RoleChangeNotification notification =
MessageCollectorActor.getFirstMatching(listenerActor, RoleChangeNotification.class);
assertNotNull(notification);
assertEquals(RaftState.Candidate.name(), notification.getOldRole());
import org.opendaylight.controller.cluster.datastore.messages.PreCommitTransactionReply;
import org.opendaylight.controller.cluster.datastore.messages.SerializableMessage;
import org.opendaylight.controller.cluster.datastore.utils.ActorContext;
-import org.opendaylight.controller.cluster.datastore.utils.DoNothingActor;
+import org.opendaylight.controller.cluster.raft.utils.DoNothingActor;
import scala.concurrent.Future;
import scala.concurrent.duration.Duration;
import org.opendaylight.controller.cluster.datastore.modification.MergeModification;
import org.opendaylight.controller.cluster.datastore.modification.WriteModification;
import org.opendaylight.controller.cluster.datastore.shardstrategy.DefaultShardStrategy;
-import org.opendaylight.controller.cluster.datastore.utils.DoNothingActor;
import org.opendaylight.controller.cluster.datastore.utils.NormalizedNodeAggregatorTest;
+import org.opendaylight.controller.cluster.raft.utils.DoNothingActor;
import org.opendaylight.controller.md.cluster.datastore.model.CarsModel;
import org.opendaylight.controller.md.cluster.datastore.model.SchemaContextHelper;
import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
import com.google.common.base.Optional;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
-import com.google.common.util.concurrent.Uninterruptibles;
import com.typesafe.config.ConfigFactory;
import java.util.Arrays;
import java.util.Map;
import org.opendaylight.controller.cluster.datastore.messages.LocalShardNotFound;
import org.opendaylight.controller.cluster.datastore.messages.PrimaryShardInfo;
import org.opendaylight.controller.cluster.datastore.messages.RemotePrimaryShardFound;
+import org.opendaylight.controller.cluster.raft.utils.EchoActor;
+import org.opendaylight.controller.cluster.raft.utils.MessageCollectorActor;
import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
actorContext.broadcast(new TestMessage());
- expectFirstMatching(shardActorRef1, TestMessage.class);
- expectFirstMatching(shardActorRef2, TestMessage.class);
+ MessageCollectorActor.expectFirstMatching(shardActorRef1, TestMessage.class);
+ MessageCollectorActor.expectFirstMatching(shardActorRef2, TestMessage.class);
}};
}
- private static <T> T expectFirstMatching(ActorRef actor, Class<T> clazz) {
- int count = 5000 / 50;
- for(int i = 0; i < count; i++) {
- try {
- @SuppressWarnings("unchecked")
- T message = (T) MessageCollectorActor.getFirstMatching(actor, clazz);
- if(message != null) {
- return message;
- }
- } catch (Exception e) {}
-
- Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS);
- }
-
- Assert.fail("Did not receive message of type " + clazz);
- return null;
- }
}
+++ /dev/null
-/*
- * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
- *
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License v1.0 which accompanies this distribution,
- * and is available at http://www.eclipse.org/legal/epl-v10.html
- */
-
-package org.opendaylight.controller.cluster.datastore.utils;
-
-import akka.actor.UntypedActor;
-
-public class DoNothingActor extends UntypedActor {
-
- @Override public void onReceive(Object message) throws Exception {
- }
-}
+++ /dev/null
-/*
- * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
- *
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License v1.0 which accompanies this distribution,
- * and is available at http://www.eclipse.org/legal/epl-v10.html
- */
-
-package org.opendaylight.controller.cluster.datastore.utils;
-
-import akka.actor.ActorRef;
-import akka.actor.UntypedActor;
-import akka.pattern.Patterns;
-import akka.util.Timeout;
-import com.google.common.collect.Lists;
-import com.google.common.util.concurrent.Uninterruptibles;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.concurrent.TimeUnit;
-import org.junit.Assert;
-import scala.concurrent.Await;
-import scala.concurrent.Future;
-import scala.concurrent.duration.Duration;
-import scala.concurrent.duration.FiniteDuration;
-
-/**
- * MessageCollectorActor collects messages as it receives them. It can send
- * those collected messages to any sender which sends it the "messages" message
- * <p>
- * This class would be useful as a mock to test whether messages were sent
- * to a remote actor or not.
- * </p>
- */
-public class MessageCollectorActor extends UntypedActor {
- private final List<Object> messages = new ArrayList<>();
-
- @Override public void onReceive(Object message) throws Exception {
- if(message instanceof String){
- if("messages".equals(message)){
- getSender().tell(new ArrayList<>(messages), getSelf());
- }
- } else {
- messages.add(message);
- }
- }
-
- public void clear() {
- messages.clear();
- }
-
- public static List<Object> getAllMessages(ActorRef actor) throws Exception {
- FiniteDuration operationDuration = Duration.create(5, TimeUnit.SECONDS);
- Timeout operationTimeout = new Timeout(operationDuration);
- Future<Object> future = Patterns.ask(actor, "messages", operationTimeout);
-
- try {
- return (List<Object>) Await.result(future, operationDuration);
- } catch (Exception e) {
- throw e;
- }
- }
-
- /**
- * Get the first message that matches the specified class
- * @param actor
- * @param clazz
- * @return
- */
- public static Object getFirstMatching(ActorRef actor, Class<?> clazz) throws Exception {
- List<Object> allMessages = getAllMessages(actor);
-
- for(Object message : allMessages){
- if(message.getClass().equals(clazz)){
- return message;
- }
- }
-
- return null;
- }
-
- public static List<Object> getAllMatching(ActorRef actor, Class<?> clazz) throws Exception {
- List<Object> allMessages = getAllMessages(actor);
-
- List<Object> output = Lists.newArrayList();
-
- for(Object message : allMessages){
- if(message.getClass().equals(clazz)){
- output.add(message);
- }
- }
-
- return output;
- }
-
- public static <T> T expectFirstMatching(ActorRef actor, Class<T> clazz) {
- int count = 5000 / 50;
- for(int i = 0; i < count; i++) {
- try {
- T message = (T) getFirstMatching(actor, clazz);
- if(message != null) {
- return message;
- }
- } catch (Exception e) {}
-
- Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS);
- }
-
- Assert.fail("Did not receive message of type " + clazz);
- return null;
- }
-}
import akka.actor.Props;
import java.util.List;
import org.junit.Assert;
+import org.opendaylight.controller.cluster.raft.utils.DoNothingActor;
+import org.opendaylight.controller.cluster.raft.utils.MessageCollectorActor;
public class TestUtils {
ActorContext testContext = new ActorContext(actorSystem, actorSystem.actorOf(
Props.create(DoNothingActor.class)), new MockClusterWrapper(), new MockConfiguration());
Object messages = testContext
- .executeOperation(actorRef, "messages");
+ .executeOperation(actorRef, MessageCollectorActor.GET_ALL_MESSAGES);
Assert.assertNotNull(messages);