import akka.actor.Props;
import akka.dispatch.Dispatchers;
import akka.japi.Creator;
+import akka.pattern.Patterns;
import akka.testkit.TestActorRef;
+import akka.util.Timeout;
import com.google.common.base.Function;
import com.google.common.base.Optional;
import com.google.common.util.concurrent.Futures;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicInteger;
import org.junit.After;
import org.junit.Assert;
import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
import org.opendaylight.yangtools.yang.data.impl.schema.tree.InMemoryDataTreeFactory;
import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+import scala.concurrent.Await;
+import scala.concurrent.Future;
+import scala.concurrent.duration.Duration;
/**
* Abstract base for shard unit tests.
public static void writeToStore(final TestActorRef<Shard> shard, final YangInstanceIdentifier id,
final NormalizedNode<?,?> node) throws InterruptedException, ExecutionException {
- writeToStore(shard.underlyingActor().getDataStore(), id, node);
+ Future<Object> future = Patterns.ask(shard, newBatchedModifications("tx", id, node, true, true, 1),
+ new Timeout(5, TimeUnit.SECONDS));
+ try {
+ Await.ready(future, Duration.create(5, TimeUnit.SECONDS));
+ } catch(TimeoutException e) {
+ throw new ExecutionException(e);
+ }
}
public static void writeToStore(final ShardDataTree store, final YangInstanceIdentifier id,
}
};
+ setupInMemorySnapshotStore();
+
final MockDataChangeListener listener = new MockDataChangeListener(1);
final ActorRef dclActor = actorFactory.createActor(DataChangeListener.props(listener),
"testRegisterChangeListenerWhenNotLeaderInitially-DataChangeListener");
Props.create(new DelegatingShardCreator(creator)).withDispatcher(Dispatchers.DefaultDispatcherId()),
"testRegisterChangeListenerWhenNotLeaderInitially");
- // Write initial data into the in-memory store.
final YangInstanceIdentifier path = TestModel.TEST_PATH;
- writeToStore(shard, path, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
// Wait until the shard receives the first ElectionTimeout message.
assertEquals("Got first ElectionTimeout", true,
@Override
public Shard create() throws Exception {
- return new Shard(Shard.builder().id(shardID).datastoreContext(
- dataStoreContextBuilder.persistent(false).build()).schemaContext(SCHEMA_CONTEXT)) {
+ return new Shard(newShardBuilder()) {
@Override
public void onReceiveCommand(final Object message) throws Exception {
if(message instanceof ElectionTimeout && firstElectionTimeout) {
}
};
+ setupInMemorySnapshotStore();
+
final MockDataTreeChangeListener listener = new MockDataTreeChangeListener(1);
final ActorRef dclActor = actorFactory.createActor(DataTreeChangeListenerActor.props(listener),
"testDataTreeChangeListenerNotifiedWhenNotTheLeaderOnRegistration-DataChangeListener");
"testDataTreeChangeListenerNotifiedWhenNotTheLeaderOnRegistration");
final YangInstanceIdentifier path = TestModel.TEST_PATH;
- writeToStore(shard, path, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
assertEquals("Got first ElectionTimeout", true,
onFirstElectionTimeout.await(5, TimeUnit.SECONDS));
expectMsgClass(duration("5 seconds"), FindLeaderReply.class);
assertFalse("Expected the shard not to be the leader", findLeadeReply.getLeaderActor().isPresent());
- writeToStore(shard, path, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
onChangeListenerRegistered.countDown();
public void testClusteredDataChangeListenerDelayedRegistration() throws Exception {
new ShardTestKit(getSystem()) {{
String testName = "testClusteredDataChangeListenerDelayedRegistration";
- dataStoreContextBuilder.shardElectionTimeoutFactor(1000);
+ dataStoreContextBuilder.shardElectionTimeoutFactor(1000).
+ customRaftPolicyImplementation(DisableElectionsRaftPolicy.class.getName());
final MockDataChangeListener listener = new MockDataChangeListener(1);
final ActorRef dclActor = actorFactory.createActor(DataChangeListener.props(listener),
actorFactory.generateActorId(testName + "-DataChangeListener"));
+ setupInMemorySnapshotStore();
+
final TestActorRef<Shard> shard = actorFactory.createTestActor(
newShardBuilder().props().withDispatcher(Dispatchers.DefaultDispatcherId()),
actorFactory.generateActorId(testName + "-shard"));
RegisterChangeListenerReply.class);
assertNotNull("getListenerRegistrationPath", reply.getListenerRegistrationPath());
- writeToStore(shard, path, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
-
- shard.tell(new ElectionTimeout(), ActorRef.noSender());
+ shard.tell(DatastoreContext.newBuilderFrom(dataStoreContextBuilder.build()).
+ customRaftPolicyImplementation(null).build(), ActorRef.noSender());
listener.waitForChangeEvents();
}};
public void testClusteredDataTreeChangeListenerDelayedRegistration() throws Exception {
new ShardTestKit(getSystem()) {{
String testName = "testClusteredDataTreeChangeListenerDelayedRegistration";
- dataStoreContextBuilder.shardElectionTimeoutFactor(1000);
+ dataStoreContextBuilder.shardElectionTimeoutFactor(1000).
+ customRaftPolicyImplementation(DisableElectionsRaftPolicy.class.getName());
final MockDataTreeChangeListener listener = new MockDataTreeChangeListener(1);
final ActorRef dclActor = actorFactory.createActor(DataTreeChangeListenerActor.props(listener),
actorFactory.generateActorId(testName + "-DataTreeChangeListener"));
+ setupInMemorySnapshotStore();
+
final TestActorRef<Shard> shard = actorFactory.createTestActor(
newShardBuilder().props().withDispatcher(Dispatchers.DefaultDispatcherId()),
actorFactory.generateActorId(testName + "-shard"));
RegisterDataTreeChangeListenerReply.class);
assertNotNull("getListenerRegistrationPath", reply.getListenerRegistrationPath());
- writeToStore(shard, path, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
-
- shard.tell(new ElectionTimeout(), ActorRef.noSender());
+ shard.tell(DatastoreContext.newBuilderFrom(dataStoreContextBuilder.build()).
+ customRaftPolicyImplementation(null).build(), ActorRef.noSender());
listener.waitForChangeEvents();
}};
import com.google.common.util.concurrent.Uninterruptibles;
import java.util.Arrays;
import java.util.Collection;
-import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class MockDataTreeChangeListener implements DOMDataTreeChangeListener {
private final List<Collection<DataTreeCandidate>> changeList =
- Collections.synchronizedList(Lists.<Collection<DataTreeCandidate>>newArrayList());
+ Lists.<Collection<DataTreeCandidate>>newArrayList();
private volatile CountDownLatch changeLatch;
private int expChangeEventCount;
public void reset(int expChangeEventCount) {
changeLatch = new CountDownLatch(expChangeEventCount);
this.expChangeEventCount = expChangeEventCount;
- changeList.clear();
+ synchronized(changeList) {
+ changeList.clear();
+ }
}
@Override
public void onDataTreeChanged(@Nonnull final Collection<DataTreeCandidate> changes) {
- changeList.add(changes);
+ synchronized(changeList) {
+ changeList.add(changes);
+ }
changeLatch.countDown();
}
public void verifyNotifiedData(YangInstanceIdentifier... paths) {
Set<YangInstanceIdentifier> pathSet = new HashSet<>(Arrays.asList(paths));
- for(Collection<DataTreeCandidate> list: changeList) {
- for(DataTreeCandidate c: list) {
- pathSet.remove(c.getRootPath());
+ synchronized(changeList) {
+ for(Collection<DataTreeCandidate> list: changeList) {
+ for(DataTreeCandidate c: list) {
+ pathSet.remove(c.getRootPath());
+ }
}
}
public void expectNoMoreChanges(String assertMsg) {
Uninterruptibles.sleepUninterruptibly(500, TimeUnit.MILLISECONDS);
- assertEquals(assertMsg, expChangeEventCount, changeList.size());
+ synchronized(changeList) {
+ assertEquals(assertMsg, expChangeEventCount, changeList.size());
+ }
}
public void verifyNoNotifiedData(YangInstanceIdentifier... paths) {
Set<YangInstanceIdentifier> pathSet = new HashSet<>(Arrays.asList(paths));
- for(Collection<DataTreeCandidate> list: changeList) {
- for(DataTreeCandidate c: list) {
- assertFalse("Unexpected " + c.getRootPath() + " present in DataTreeCandidate",
- pathSet.contains(c.getRootPath()));
+ synchronized(changeList) {
+ for(Collection<DataTreeCandidate> list: changeList) {
+ for(DataTreeCandidate c: list) {
+ assertFalse("Unexpected " + c.getRootPath() + " present in DataTreeCandidate",
+ pathSet.contains(c.getRootPath()));
+ }
}
}
}