MessageAggregator extends AbstractActorWithTimers { public static <T> Props props(final ActorRef initialReceiver, final Class<T> messageClass, final int expectedMessages, final Duration timeout) { return Props.create(MessageAggregator.class, initialReceiver, messageClass, expectedMessages, timeout); } @SuppressWarnings("unused") private  MessageAggregator(
            final ActorRef initialReceiver,
            final Class<T> messageClass,
            final int expectedMessages,
            final Duration timeout); static Props props(final ActorRef initialReceiver, final Class<T> messageClass,
            final int expectedMessages, final Duration timeout); @Override Receive createReceive();  }
@Test public void stopSelfIfExpectNoMessage() { new TestKit(actorSystem) {{ final TestProbe initialReceiver = TestProbe.apply(actorSystem); final ActorRef underTest = actorSystem.actorOf(MessageAggregator.props(initialReceiver.ref(), Integer.class, 0, ONE_DAY)); watch(underTest); underTest.tell(true, getRef()); initialReceiver.expectMsg(true); expectMsg(Collections.emptyList()); expectTerminated(underTest); }}; }
@Test public void aggregateMessagesAndStopSelf() { new TestKit(actorSystem) {{ final TestProbe initialReceiver = TestProbe.apply(actorSystem); final ActorRef underTest = actorSystem.actorOf(MessageAggregator.props(initialReceiver.ref(), Integer.class, 3, ONE_DAY)); watch(underTest); underTest.tell(true, getRef()); underTest.tell(0, getRef()); underTest.tell(1, getRef()); underTest.tell(2, getRef()); initialReceiver.expectMsg(true); expectMsg(Arrays.asList(0, 1, 2)); expectTerminated(underTest); }}; }
@Test public void ignoreIrrelevantMessages() { new TestKit(actorSystem) {{ final TestProbe initialReceiver = TestProbe.apply(actorSystem); final ActorRef underTest = actorSystem.actorOf(MessageAggregator.props(initialReceiver.ref(), Integer.class, 3, ONE_DAY)); watch(underTest); underTest.tell(true, getRef()); underTest.tell("hello", getRef()); underTest.tell(0, getRef()); underTest.tell(new Object(), getRef()); underTest.tell(1, getRef()); underTest.tell(false, getRef()); underTest.tell(2, getRef()); initialReceiver.expectMsg(true); expectMsg(Arrays.asList(0, 1, 2)); expectTerminated(underTest); }}; }
@Test public void reportPartialMessagesOnTimeout() { new TestKit(actorSystem) {{ final TestProbe initialReceiver = TestProbe.apply(actorSystem); final ActorRef underTest = actorSystem.actorOf(MessageAggregator.props(initialReceiver.ref(), Integer.class, 3, ONE_DAY)); watch(underTest); underTest.tell(true, getRef()); underTest.tell(0, getRef()); underTest.tell(1, getRef()); underTest.tell(MessageAggregator.TIMEOUT, getRef()); initialReceiver.expectMsg(true); expectMsg(Arrays.asList(0, 1)); expectTerminated(underTest); }}; }