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.assertEquals;
12 import akka.actor.ActorRef;
13 import akka.actor.Status.Failure;
14 import akka.actor.Terminated;
15 import akka.testkit.JavaTestKit;
16 import java.util.Arrays;
17 import java.util.Collections;
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.persisted.DatastoreSnapshot;
26 import org.opendaylight.controller.cluster.datastore.persisted.DatastoreSnapshot.ShardSnapshot;
27 import org.opendaylight.controller.cluster.datastore.persisted.ShardManagerSnapshot;
28 import org.opendaylight.controller.cluster.raft.ReplicatedLogEntry;
29 import org.opendaylight.controller.cluster.raft.client.messages.GetSnapshotReply;
30 import org.opendaylight.controller.cluster.raft.persisted.ByteState;
31 import org.opendaylight.controller.cluster.raft.persisted.Snapshot;
32 import scala.concurrent.duration.Duration;
33 import scala.concurrent.duration.FiniteDuration;
36 * Unit tests for ShardManagerGetSnapshotReplyActor.
38 * @author Thomas Pantelis
40 public class ShardManagerGetSnapshotReplyActorTest extends AbstractActorTest {
41 private static final MemberName MEMBER_1 = MemberName.forName("member-1");
44 public void testSuccess() {
45 JavaTestKit kit = new JavaTestKit(getSystem());
47 List<String> shardList = Arrays.asList("shard1", "shard2", "shard3");
48 ShardManagerSnapshot shardManagerSnapshot = new ShardManagerSnapshot(shardList, Collections.emptyMap());
49 ActorRef replyActor = getSystem().actorOf(ShardManagerGetSnapshotReplyActor.props(
50 shardList, "config", shardManagerSnapshot, kit.getRef(),
51 "shard-manager", Duration.create(100, TimeUnit.SECONDS)), "testSuccess");
53 kit.watch(replyActor);
55 ByteState shard1SnapshotState = ByteState.of(new byte[]{1,2,3});
56 replyActor.tell(new GetSnapshotReply(ShardIdentifier.create("shard1", MEMBER_1, "config").toString(),
57 Snapshot.create(shard1SnapshotState, Collections.<ReplicatedLogEntry>emptyList(),
58 2, 1, 2, 1, 1, "member-1", null)), ActorRef.noSender());
60 ByteState shard2SnapshotState = ByteState.of(new byte[]{4,5,6});
61 replyActor.tell(new GetSnapshotReply(ShardIdentifier.create("shard2", MEMBER_1, "config").toString(),
62 Snapshot.create(shard2SnapshotState, Collections.<ReplicatedLogEntry>emptyList(),
63 2, 1, 2, 1, 1, "member-1", null)), ActorRef.noSender());
65 kit.expectNoMsg(FiniteDuration.create(500, TimeUnit.MILLISECONDS));
67 ByteState shard3SnapshotState = ByteState.of(new byte[]{7,8,9});
68 replyActor.tell(new GetSnapshotReply(ShardIdentifier.create("shard3", MEMBER_1, "config").toString(),
69 Snapshot.create(shard3SnapshotState, Collections.<ReplicatedLogEntry>emptyList(),
70 2, 1, 2, 1, 1, "member-1", null)), ActorRef.noSender());
72 DatastoreSnapshot datastoreSnapshot = kit.expectMsgClass(DatastoreSnapshot.class);
74 assertEquals("getType", "config", datastoreSnapshot.getType());
75 assertEquals("getShardManagerSnapshot", shardManagerSnapshot.getShardList(),
76 datastoreSnapshot.getShardManagerSnapshot().getShardList());
77 List<ShardSnapshot> shardSnapshots = datastoreSnapshot.getShardSnapshots();
78 assertEquals("ShardSnapshot size", 3, shardSnapshots.size());
79 assertEquals("ShardSnapshot 1 getName", "shard1", shardSnapshots.get(0).getName());
80 assertEquals("ShardSnapshot 1 getSnapshot", shard1SnapshotState,
81 shardSnapshots.get(0).getSnapshot().getState());
82 assertEquals("ShardSnapshot 2 getName", "shard2", shardSnapshots.get(1).getName());
83 assertEquals("ShardSnapshot 2 getSnapshot", shard2SnapshotState,
84 shardSnapshots.get(1).getSnapshot().getState());
85 assertEquals("ShardSnapshot 3 getName", "shard3", shardSnapshots.get(2).getName());
86 assertEquals("ShardSnapshot 3 getSnapshot", shard3SnapshotState,
87 shardSnapshots.get(2).getSnapshot().getState());
89 kit.expectMsgClass(Terminated.class);
93 public void testGetSnapshotFailureReply() {
94 JavaTestKit kit = new JavaTestKit(getSystem());
96 ActorRef replyActor = getSystem().actorOf(ShardManagerGetSnapshotReplyActor.props(
97 Arrays.asList("shard1", "shard2"), "config", null, kit.getRef(), "shard-manager",
98 Duration.create(100, TimeUnit.SECONDS)), "testGetSnapshotFailureReply");
100 kit.watch(replyActor);
102 replyActor.tell(new GetSnapshotReply(ShardIdentifier.create("shard1", MEMBER_1, "config").toString(),
103 Snapshot.create(ByteState.of(new byte[]{1,2,3}), Collections.<ReplicatedLogEntry>emptyList(),
104 2, 1, 2, 1, 1, "member-1", null)), ActorRef.noSender());
106 replyActor.tell(new Failure(new RuntimeException()), ActorRef.noSender());
108 kit.expectMsgClass(Failure.class);
109 kit.expectTerminated(replyActor);
113 public void testGetSnapshotTimeout() {
114 JavaTestKit kit = new JavaTestKit(getSystem());
116 ActorRef replyActor = getSystem().actorOf(ShardManagerGetSnapshotReplyActor.props(
117 Arrays.asList("shard1"), "config", null, kit.getRef(), "shard-manager",
118 Duration.create(100, TimeUnit.MILLISECONDS)), "testGetSnapshotTimeout");
120 kit.watch(replyActor);
122 Failure failure = kit.expectMsgClass(Failure.class);
123 assertEquals("Failure cause type", TimeoutException.class, failure.cause().getClass());
124 kit.expectTerminated(replyActor);