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.Test;
22 import org.opendaylight.controller.cluster.access.concepts.MemberName;
23 import org.opendaylight.controller.cluster.datastore.AbstractActorTest;
24 import org.opendaylight.controller.cluster.datastore.identifiers.ShardIdentifier;
25 import org.opendaylight.controller.cluster.datastore.messages.DatastoreSnapshot;
26 import org.opendaylight.controller.cluster.datastore.messages.DatastoreSnapshot.ShardSnapshot;
27 import org.opendaylight.controller.cluster.raft.client.messages.GetSnapshotReply;
28 import scala.concurrent.duration.Duration;
29 import scala.concurrent.duration.FiniteDuration;
32 * Unit tests for ShardManagerGetSnapshotReplyActor.
34 * @author Thomas Pantelis
36 public class ShardManagerGetSnapshotReplyActorTest extends AbstractActorTest {
37 private static final MemberName MEMBER_1 = MemberName.forName("member-1");
40 public void testSuccess() {
41 JavaTestKit kit = new JavaTestKit(getSystem());
43 byte[] shardManagerSnapshot = new byte[]{0,5,9};
44 ActorRef replyActor = getSystem().actorOf(ShardManagerGetSnapshotReplyActor.props(
45 Arrays.asList("shard1", "shard2", "shard3"), "config", shardManagerSnapshot, kit.getRef(),
46 "shard-manager", Duration.create(100, TimeUnit.SECONDS)), "testSuccess");
48 kit.watch(replyActor);
50 byte[] shard1Snapshot = new byte[]{1,2,3};
51 replyActor.tell(new GetSnapshotReply(ShardIdentifier.create("shard1", MEMBER_1, "config").toString(),
52 shard1Snapshot), ActorRef.noSender());
54 byte[] shard2Snapshot = new byte[]{4,5,6};
55 replyActor.tell(new GetSnapshotReply(ShardIdentifier.create("shard2", MEMBER_1, "config").toString(),
56 shard2Snapshot), ActorRef.noSender());
58 kit.expectNoMsg(FiniteDuration.create(500, TimeUnit.MILLISECONDS));
60 byte[] shard3Snapshot = new byte[]{7,8,9};
61 replyActor.tell(new GetSnapshotReply(ShardIdentifier.create("shard3", MEMBER_1, "config").toString(),
62 shard3Snapshot), ActorRef.noSender());
64 DatastoreSnapshot datastoreSnapshot = kit.expectMsgClass(DatastoreSnapshot.class);
66 assertEquals("getType", "config", datastoreSnapshot.getType());
67 assertArrayEquals("getShardManagerSnapshot", shardManagerSnapshot, datastoreSnapshot.getShardManagerSnapshot());
68 List<ShardSnapshot> shardSnapshots = datastoreSnapshot.getShardSnapshots();
69 assertEquals("ShardSnapshot size", 3, shardSnapshots.size());
70 assertEquals("ShardSnapshot 1 getName", "shard1", shardSnapshots.get(0).getName());
71 assertArrayEquals("ShardSnapshot 1 getSnapshot", shard1Snapshot, shardSnapshots.get(0).getSnapshot());
72 assertEquals("ShardSnapshot 2 getName", "shard2", shardSnapshots.get(1).getName());
73 assertArrayEquals("ShardSnapshot 2 getSnapshot", shard2Snapshot, shardSnapshots.get(1).getSnapshot());
74 assertEquals("ShardSnapshot 3 getName", "shard3", shardSnapshots.get(2).getName());
75 assertArrayEquals("ShardSnapshot 3 getSnapshot", shard3Snapshot, shardSnapshots.get(2).getSnapshot());
77 kit.expectMsgClass(Terminated.class);
81 public void testGetSnapshotFailureReply() {
82 JavaTestKit kit = new JavaTestKit(getSystem());
84 byte[] shardManagerSnapshot = new byte[]{0,5,9};
85 ActorRef replyActor = getSystem().actorOf(ShardManagerGetSnapshotReplyActor.props(
86 Arrays.asList("shard1", "shard2"), "config", shardManagerSnapshot, kit.getRef(), "shard-manager",
87 Duration.create(100, TimeUnit.SECONDS)), "testGetSnapshotFailureReply");
89 kit.watch(replyActor);
91 replyActor.tell(new GetSnapshotReply(ShardIdentifier.create("shard1", MEMBER_1, "config").toString(),
92 new byte[]{1,2,3}), ActorRef.noSender());
94 replyActor.tell(new Failure(new RuntimeException()), ActorRef.noSender());
96 kit.expectMsgClass(Failure.class);
97 kit.expectTerminated(replyActor);
101 public void testGetSnapshotTimeout() {
102 JavaTestKit kit = new JavaTestKit(getSystem());
104 byte[] shardManagerSnapshot = new byte[]{0,5,9};
105 ActorRef replyActor = getSystem().actorOf(ShardManagerGetSnapshotReplyActor.props(
106 Arrays.asList("shard1"), "config", shardManagerSnapshot, kit.getRef(), "shard-manager",
107 Duration.create(100, TimeUnit.MILLISECONDS)), "testGetSnapshotTimeout");
109 kit.watch(replyActor);
111 Failure failure = kit.expectMsgClass(Failure.class);
112 assertEquals("Failure cause type", TimeoutException.class, failure.cause().getClass());
113 kit.expectTerminated(replyActor);