+ @Test
+ public void testOnReceiveSwitchShardBehavior() throws Exception {
+ new JavaTestKit(getSystem()) {{
+ final ActorRef shardManager = getSystem().actorOf(newPropsShardMgrWithMockShardActor());
+
+ shardManager.tell(new UpdateSchemaContext(TestModel.createTestContext()), getRef());
+ shardManager.tell(new ActorInitialized(), mockShardActor);
+
+ shardManager.tell(new SwitchShardBehavior(mockShardName, "Leader", 1000), getRef());
+
+ SwitchBehavior switchBehavior = MessageCollectorActor.expectFirstMatching(mockShardActor, SwitchBehavior.class);
+
+ assertEquals(RaftState.Leader, switchBehavior.getNewState());
+ assertEquals(1000, switchBehavior.getNewTerm());
+ }};
+ }
+
+ public void testOnReceiveCreateShard() {
+ new JavaTestKit(getSystem()) {{
+ datastoreContextBuilder.shardInitializationTimeout(1, TimeUnit.MINUTES).persistent(true);
+
+ ActorRef shardManager = getSystem().actorOf(newShardMgrProps(false));
+
+ SchemaContext schemaContext = TestModel.createTestContext();
+ shardManager.tell(new UpdateSchemaContext(schemaContext), ActorRef.noSender());
+
+ DatastoreContext datastoreContext = DatastoreContext.newBuilder().shardElectionTimeoutFactor(100).
+ persistent(false).build();
+ TestShardPropsCreator shardPropsCreator = new TestShardPropsCreator();
+
+ shardManager.tell(new CreateShard("foo", Arrays.asList("member-1", "member-5", "member-6"), shardPropsCreator,
+ datastoreContext), getRef());
+
+ expectMsgClass(duration("5 seconds"), CreateShardReply.class);
+
+ shardManager.tell(new FindLocalShard("foo", true), getRef());
+
+ expectMsgClass(duration("5 seconds"), LocalShardFound.class);
+
+ assertEquals("isRecoveryApplicable", false, shardPropsCreator.datastoreContext.isPersistent());
+ assertEquals("peerMembers", Sets.newHashSet(new ShardIdentifier("foo", "member-5", shardMrgIDSuffix).toString(),
+ new ShardIdentifier("foo", "member-6", shardMrgIDSuffix).toString()),
+ shardPropsCreator.peerAddresses.keySet());
+ assertEquals("ShardIdentifier", new ShardIdentifier("foo", "member-1", shardMrgIDSuffix),
+ shardPropsCreator.shardId);
+ assertSame("schemaContext", schemaContext, shardPropsCreator.schemaContext);
+
+ // Send CreateShard with same name - should fail.
+
+ shardManager.tell(new CreateShard("foo", Collections.<String>emptyList(), shardPropsCreator, null), getRef());
+
+ expectMsgClass(duration("5 seconds"), akka.actor.Status.Failure.class);
+ }};
+ }
+
+ @Test
+ public void testOnReceiveCreateShardWithNoInitialSchemaContext() {
+ new JavaTestKit(getSystem()) {{
+ ActorRef shardManager = getSystem().actorOf(newShardMgrProps(false));
+
+ TestShardPropsCreator shardPropsCreator = new TestShardPropsCreator();
+
+ shardManager.tell(new CreateShard("foo", Arrays.asList("member-1"), shardPropsCreator, null), getRef());
+
+ expectMsgClass(duration("5 seconds"), CreateShardReply.class);
+
+ SchemaContext schemaContext = TestModel.createTestContext();
+ shardManager.tell(new UpdateSchemaContext(schemaContext), ActorRef.noSender());
+
+ shardManager.tell(new FindLocalShard("foo", true), getRef());
+
+ expectMsgClass(duration("5 seconds"), LocalShardFound.class);
+
+ assertSame("schemaContext", schemaContext, shardPropsCreator.schemaContext);
+ assertNotNull("schemaContext is null", shardPropsCreator.datastoreContext);
+ }};
+ }
+
+ private static class TestShardPropsCreator implements ShardPropsCreator {
+ ShardIdentifier shardId;
+ Map<String, String> peerAddresses;
+ SchemaContext schemaContext;
+ DatastoreContext datastoreContext;
+
+ @Override
+ public Props newProps(ShardIdentifier shardId, Map<String, String> peerAddresses,
+ DatastoreContext datastoreContext, SchemaContext schemaContext) {
+ this.shardId = shardId;
+ this.peerAddresses = peerAddresses;
+ this.schemaContext = schemaContext;
+ this.datastoreContext = datastoreContext;
+ return Shard.props(shardId, peerAddresses, datastoreContext, schemaContext);
+ }
+
+ }
+