ActorScheduler { @NonNull public static ScheduledMessageSender after(@IntRange(from = 0) long millis) { return after(millis, ActorSystemInstance.getInstance(null)); } ActorScheduler(); @NonNull static ScheduledMessageSender after(@IntRange(from = 0) long millis); static void cancel(@NonNull Class<?> actorAddress);  }
@Test public void scheduleMessageToRegisteredActorAfterDelay() throws Exception { final ActorSystemInstance actorSystem = ActorSystemInstance .getInstance("removeOldActorAfterRegisteringNewActorThenKeepNewActor"); MockActor actor = new TestAsync<MockActor>().apply(new Function<CountDownLatch, MockActor>() { @Override public MockActor apply(@NonNull CountDownLatch countDownLatch) { MockActor actor = new MockActor(countDownLatch); actorSystem.register(actor); ActorScheduler.after(10, actorSystem).send(message, MockActor.class); return actor; } }); actorSystem.unregister(MockActor.class); assertTrue(actor.message.getId() == 1); }
@Test public void scheduleMessageIdToRegisteredActorAfterDelay() throws Exception { final ActorSystemInstance actorSystem = ActorSystemInstance .getInstance("scheduleMessageIdToRegisteredActorAfterDelay"); MockActor actor = new TestAsync<MockActor>().apply(new Function<CountDownLatch, MockActor>() { @Override public MockActor apply(@NonNull CountDownLatch countDownLatch) { MockActor actor = new MockActorEleven(countDownLatch); actorSystem.register(actor); ActorScheduler.after(10, actorSystem).send(1, MockActorEleven.class); return actor; } }); actorSystem.unregister(MockActorEleven.class); assertTrue(actor.message.getId() == 1); }
@Test public void disposeAllScheduledDisposablesAfterSendingMessageToRegisteredActor() throws Exception { final ActorSystemInstance actorSystem = ActorSystemInstance .getInstance("disposeAllScheduledDisposablesAfterSendingMessageToRegisteredActor"); new TestAsync<Void>().apply(new Function<CountDownLatch, Void>() { @Override public Void apply(@NonNull CountDownLatch countDownLatch) { MockActorTwo actor = new MockActorTwo(countDownLatch); actorSystem.register(actor); ActorScheduler.after(10, actorSystem).send(message, MockActorTwo.class); return null; } }); actorSystem.unregister(MockActorTwo.class); assertTrue(ActorScheduler.schedules.get(MockActorTwo.class) == null); }
@Test public void scheduleMessageToRegisteredCrashingActorAndNotAffected() throws Exception { final ActorSystemInstance actorSystem = ActorSystemInstance .getInstance("scheduleMessageToRegisteredCrashingActorAndNotAffected"); new TestAsync<Void>().apply(new Function<CountDownLatch, Void>() { @Override public Void apply(@NonNull CountDownLatch countDownLatch) { MockActorThree actor = new MockActorThree(countDownLatch); actorSystem.register(actor); actor.exception = new UnsupportedOperationException(); ActorScheduler.after(10, actorSystem).send(message, MockActorThree.class); return null; } }); actorSystem.unregister(MockActorThree.class); assertTrue(true); }
@Test public void scheduleMessageToNotRegisteredActorAndNotCrash() throws Exception { final ActorSystemInstance actorSystem = ActorSystemInstance .getInstance("scheduleMessageToNotRegisteredActorAndNotCrash"); new TestAsync<Void>(1, TimeUnit.SECONDS).apply(new Function<CountDownLatch, Void>() { @Override public Void apply(@NonNull CountDownLatch countDownLatch) { ActorScheduler.after(10, actorSystem).send(message, Actor.class); return null; } }); assertTrue(true); }
@Test public void sendTheSameMessageTwiceBeforeTheFirstOneReceivedAndIgnoreTheSecond() throws Exception { final ActorSystemInstance actorSystem = ActorSystemInstance .getInstance("sendTheSameMessageTwiceBeforeTheFirstOneReceivedAndIgnoreTheSecond"); MockActor actor = new TestAsync<MockActor>().apply(new Function<CountDownLatch, MockActor>() { @Override public MockActor apply(@NonNull CountDownLatch countDownLatch) { Message message = new Message(1, 1); Message messageTwo = new Message(1, 2); MockActorNine actor = new MockActorNine(countDownLatch); actorSystem.register(actor); ActorScheduler.after(50, actorSystem).send(message, MockActorNine.class); ActorScheduler.after(20, actorSystem).send(messageTwo, MockActorNine.class); return actor; } }); actorSystem.unregister(MockActorNine.class); assertTrue(actor.message.getContent().equals(1)); }
@Test public void sendTheSameMessageTwiceAfterTheFirstOneReceivedAndAcceptBoth() throws Exception { final ActorSystemInstance actorSystem = ActorSystemInstance .getInstance("sendTheSameMessageTwiceAfterTheFirstOneReceivedAndAcceptBoth"); final MockActorTen actor = new MockActorTen(); new TestAsync<Void>().apply(new Function<CountDownLatch, Void>() { @Override public Void apply(@NonNull CountDownLatch countDownLatch) throws Exception { actor.setCountDownLatch(countDownLatch); actorSystem.register(actor); ActorScheduler.after(10, actorSystem).send(new Message(1, 1), MockActorTen.class); return null; } }); new TestAsync<Void>().apply(new Function<CountDownLatch, Void>() { @Override public Void apply(@NonNull CountDownLatch countDownLatch) throws Exception { actor.setCountDownLatch(countDownLatch); ActorScheduler.after(10, actorSystem).send(new Message(1, 2), MockActorTen.class); return null; } }); actorSystem.unregister(MockActorTen.class); assertTrue(actor.message.getContent().equals(2)); }
@Test public void sendMessageToPostponedActorThenReceiveItWhenTheActorRegisters() throws Exception { final ActorSystemInstance actorSystem = ActorSystemInstance .getInstance("sendMessageToPostponedActorThenReceiveItWhenTheActorRegisters"); MockActor actor = new TestAsync<MockActor>(50).apply(new Function<CountDownLatch, MockActor>() { @Override public MockActor apply(@NonNull CountDownLatch countDownLatch) { Message message = new Message(1, 1); MockActorNine actor = new MockActorNine(countDownLatch); actorSystem.register(actor); actorSystem.postpone(actor); ActorScheduler.after(10, actorSystem).send(message, MockActorNine.class); return actor; } }); actorSystem.register(actor); assertTrue(actor.message.getContent().equals(1)); }