2 * Copyright (c) 2015 Brocade Communications 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
8 package org.opendaylight.controller.cluster.datastore.shardmanager;
10 import static org.junit.Assert.assertArrayEquals;
11 import static org.junit.Assert.assertEquals;
13 import akka.actor.ActorRef;
14 import akka.actor.Status.Failure;
15 import akka.actor.Terminated;
16 import akka.testkit.JavaTestKit;
17 import java.util.Arrays;
18 import java.util.List;
19 import java.util.concurrent.TimeUnit;
20 import java.util.concurrent.TimeoutException;
21 import org.junit.After;
22 import org.junit.Test;
23 import org.opendaylight.controller.cluster.access.concepts.MemberName;
24 import org.opendaylight.controller.cluster.datastore.AbstractActorTest;
25 import org.opendaylight.controller.cluster.datastore.identifiers.ShardIdentifier;
26 import org.opendaylight.controller.cluster.datastore.messages.DatastoreSnapshot;
27 import org.opendaylight.controller.cluster.datastore.messages.DatastoreSnapshot.ShardSnapshot;
28 import org.opendaylight.controller.cluster.raft.TestActorFactory;
29 import org.opendaylight.controller.cluster.raft.client.messages.GetSnapshotReply;
30 import scala.concurrent.duration.Duration;
31 import scala.concurrent.duration.FiniteDuration;
34 * Unit tests for ShardManagerGetSnapshotReplyActor.
36 * @author Thomas Pantelis
38 public class ShardManagerGetSnapshotReplyActorTest extends AbstractActorTest {
39 private static final MemberName MEMBER_1 = MemberName.forName("member-1");
41 private final TestActorFactory actorFactory = new TestActorFactory(getSystem());
44 public void tearDown() {
49 public void testSuccess() {
50 JavaTestKit kit = new JavaTestKit(getSystem());
52 byte[] shardManagerSnapshot = new byte[]{0,5,9};
53 ActorRef replyActor = actorFactory.createActor(ShardManagerGetSnapshotReplyActor.props(
54 Arrays.asList("shard1", "shard2", "shard3"), "config",
55 shardManagerSnapshot, kit.getRef(), "shard-manager", Duration.create(100, TimeUnit.SECONDS)),
56 actorFactory.generateActorId("actor"));
58 kit.watch(replyActor);
60 byte[] shard1Snapshot = new byte[]{1,2,3};
61 replyActor.tell(new GetSnapshotReply(ShardIdentifier.create("shard1", MEMBER_1, "config").toString(),
62 shard1Snapshot), ActorRef.noSender());
64 byte[] shard2Snapshot = new byte[]{4,5,6};
65 replyActor.tell(new GetSnapshotReply(ShardIdentifier.create("shard2", MEMBER_1, "config").toString(),
66 shard2Snapshot), ActorRef.noSender());
68 kit.expectNoMsg(FiniteDuration.create(500, TimeUnit.MILLISECONDS));
70 byte[] shard3Snapshot = new byte[]{7,8,9};
71 replyActor.tell(new GetSnapshotReply(ShardIdentifier.create("shard3", MEMBER_1, "config").toString(),
72 shard3Snapshot), ActorRef.noSender());
74 DatastoreSnapshot datastoreSnapshot = kit.expectMsgClass(DatastoreSnapshot.class);
76 assertEquals("getType", "config", datastoreSnapshot.getType());
77 assertArrayEquals("getShardManagerSnapshot", shardManagerSnapshot, datastoreSnapshot.getShardManagerSnapshot());
78 List<ShardSnapshot> shardSnapshots = datastoreSnapshot.getShardSnapshots();
79 assertEquals("ShardSnapshot size", 3, shardSnapshots.size());
80 assertEquals("ShardSnapshot 1 getName", "shard1", shardSnapshots.get(0).getName());
81 assertArrayEquals("ShardSnapshot 1 getSnapshot", shard1Snapshot, shardSnapshots.get(0).getSnapshot());
82 assertEquals("ShardSnapshot 2 getName", "shard2", shardSnapshots.get(1).getName());
83 assertArrayEquals("ShardSnapshot 2 getSnapshot", shard2Snapshot, shardSnapshots.get(1).getSnapshot());
84 assertEquals("ShardSnapshot 3 getName", "shard3", shardSnapshots.get(2).getName());
85 assertArrayEquals("ShardSnapshot 3 getSnapshot", shard3Snapshot, shardSnapshots.get(2).getSnapshot());
87 kit.expectMsgClass(Terminated.class);
91 public void testGetSnapshotFailureReply() {
92 JavaTestKit kit = new JavaTestKit(getSystem());
94 byte[] shardManagerSnapshot = new byte[]{0,5,9};
95 ActorRef replyActor = actorFactory.createActor(ShardManagerGetSnapshotReplyActor.props(
96 Arrays.asList("shard1", "shard2"), "config",
97 shardManagerSnapshot, kit.getRef(), "shard-manager", Duration.create(100, TimeUnit.SECONDS)),
98 actorFactory.generateActorId("actor"));
100 kit.watch(replyActor);
102 replyActor.tell(new GetSnapshotReply(ShardIdentifier.create("shard1", MEMBER_1, "config").toString(),
103 new byte[]{1,2,3}), ActorRef.noSender());
105 replyActor.tell(new Failure(new RuntimeException()), ActorRef.noSender());
107 kit.expectMsgClass(Failure.class);
108 kit.expectTerminated(replyActor);
112 public void testGetSnapshotTimeout() {
113 JavaTestKit kit = new JavaTestKit(getSystem());
115 byte[] shardManagerSnapshot = new byte[]{0,5,9};
116 ActorRef replyActor = actorFactory.createActor(ShardManagerGetSnapshotReplyActor.props(
117 Arrays.asList("shard1"), "config",
118 shardManagerSnapshot, kit.getRef(), "shard-manager", Duration.create(100, TimeUnit.MILLISECONDS)),
119 actorFactory.generateActorId("actor"));
121 kit.watch(replyActor);
123 Failure failure = kit.expectMsgClass(Failure.class);
124 assertEquals("Failure cause type", TimeoutException.class, failure.cause().getClass());
125 kit.expectTerminated(replyActor);