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.Collections;
19 import java.util.List;
20 import java.util.concurrent.TimeUnit;
21 import java.util.concurrent.TimeoutException;
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.persisted.DatastoreSnapshot;
27 import org.opendaylight.controller.cluster.datastore.persisted.DatastoreSnapshot.ShardSnapshot;
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 byte[] shardManagerSnapshot = new byte[]{0,5,9};
48 ActorRef replyActor = getSystem().actorOf(ShardManagerGetSnapshotReplyActor.props(
49 Arrays.asList("shard1", "shard2", "shard3"), "config", shardManagerSnapshot, kit.getRef(),
50 "shard-manager", Duration.create(100, TimeUnit.SECONDS)), "testSuccess");
52 kit.watch(replyActor);
54 ByteState shard1SnapshotState = ByteState.of(new byte[]{1,2,3});
55 replyActor.tell(new GetSnapshotReply(ShardIdentifier.create("shard1", MEMBER_1, "config").toString(),
56 Snapshot.create(shard1SnapshotState, Collections.<ReplicatedLogEntry>emptyList(),
57 2, 1, 2, 1, 1, "member-1", null)), ActorRef.noSender());
59 ByteState shard2SnapshotState = ByteState.of(new byte[]{4,5,6});
60 replyActor.tell(new GetSnapshotReply(ShardIdentifier.create("shard2", MEMBER_1, "config").toString(),
61 Snapshot.create(shard2SnapshotState, Collections.<ReplicatedLogEntry>emptyList(),
62 2, 1, 2, 1, 1, "member-1", null)), ActorRef.noSender());
64 kit.expectNoMsg(FiniteDuration.create(500, TimeUnit.MILLISECONDS));
66 ByteState shard3SnapshotState = ByteState.of(new byte[]{7,8,9});
67 replyActor.tell(new GetSnapshotReply(ShardIdentifier.create("shard3", MEMBER_1, "config").toString(),
68 Snapshot.create(shard3SnapshotState, Collections.<ReplicatedLogEntry>emptyList(),
69 2, 1, 2, 1, 1, "member-1", null)), ActorRef.noSender());
71 DatastoreSnapshot datastoreSnapshot = kit.expectMsgClass(DatastoreSnapshot.class);
73 assertEquals("getType", "config", datastoreSnapshot.getType());
74 assertArrayEquals("getShardManagerSnapshot", shardManagerSnapshot, datastoreSnapshot.getShardManagerSnapshot());
75 List<ShardSnapshot> shardSnapshots = datastoreSnapshot.getShardSnapshots();
76 assertEquals("ShardSnapshot size", 3, shardSnapshots.size());
77 assertEquals("ShardSnapshot 1 getName", "shard1", shardSnapshots.get(0).getName());
78 assertEquals("ShardSnapshot 1 getSnapshot", shard1SnapshotState,
79 shardSnapshots.get(0).getSnapshot().getState());
80 assertEquals("ShardSnapshot 2 getName", "shard2", shardSnapshots.get(1).getName());
81 assertEquals("ShardSnapshot 2 getSnapshot", shard2SnapshotState,
82 shardSnapshots.get(1).getSnapshot().getState());
83 assertEquals("ShardSnapshot 3 getName", "shard3", shardSnapshots.get(2).getName());
84 assertEquals("ShardSnapshot 3 getSnapshot", shard3SnapshotState,
85 shardSnapshots.get(2).getSnapshot().getState());
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 = getSystem().actorOf(ShardManagerGetSnapshotReplyActor.props(
96 Arrays.asList("shard1", "shard2"), "config", shardManagerSnapshot, kit.getRef(), "shard-manager",
97 Duration.create(100, TimeUnit.SECONDS)), "testGetSnapshotFailureReply");
99 kit.watch(replyActor);
101 replyActor.tell(new GetSnapshotReply(ShardIdentifier.create("shard1", MEMBER_1, "config").toString(),
102 Snapshot.create(ByteState.of(new byte[]{1,2,3}), Collections.<ReplicatedLogEntry>emptyList(),
103 2, 1, 2, 1, 1, "member-1", null)), 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 = getSystem().actorOf(ShardManagerGetSnapshotReplyActor.props(
117 Arrays.asList("shard1"), "config", shardManagerSnapshot, 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);