Java Code Examples for akka.testkit.TestProbe#expectNoMessage()

The following examples show how to use akka.testkit.TestProbe#expectNoMessage() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ConnectionPersistenceActorTest.java    From ditto with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void createConnectionInClosedState() {
    new TestKit(actorSystem) {{
        final TestProbe probe = TestProbe.apply(actorSystem);
        final ActorRef underTest =
                TestConstants.createConnectionSupervisorActor(connectionId, actorSystem, pubSubMediator,
                        conciergeForwarder,
                        (connection, concierge, connectionActor) -> MockClientActor.props(probe.ref()));
        watch(underTest);

        // create connection
        underTest.tell(createClosedConnection, getRef());
        expectMsg(createClosedConnectionResponse);

        // assert that client actor is not called for closed connection
        probe.expectNoMessage();
    }};
}
 
Example 2
Source File: ReconnectActorTest.java    From ditto with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testRecoverConnectionsIsNotStartedTwice() {
    new TestKit(actorSystem) {{
        final TestProbe probe = new TestProbe(actorSystem);
        final Props props = ReconnectActor.props(probe.ref(),
                () -> Source.from(Arrays.asList(
                        ConnectionPersistenceActor.PERSISTENCE_ID_PREFIX + "connection-1",
                        ConnectionPersistenceActor.PERSISTENCE_ID_PREFIX + "connection-2",
                        ConnectionPersistenceActor.PERSISTENCE_ID_PREFIX + "connection-3")));

        final ActorRef reconnectActor = actorSystem.actorOf(props);
        reconnectActor.tell(ReconnectActor.ReconnectMessages.START_RECONNECT, getRef());

        probe.expectMsgClass(RetrieveConnectionStatus.class);
        probe.expectMsgClass(RetrieveConnectionStatus.class);
        probe.expectMsgClass(RetrieveConnectionStatus.class);
        probe.expectNoMessage();
    }};
}
 
Example 3
Source File: PubSubFactoryTest.java    From ditto with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void subscribeAndPublishAndUnsubscribe() {
    new TestKit(system2) {{
        final DistributedPub<String> pub = factory1.startDistributedPub();
        final DistributedSub sub = factory2.startDistributedSub();
        final TestProbe publisher = TestProbe.apply(system1);
        final TestProbe subscriber = TestProbe.apply(system2);

        // WHEN: actor subscribes to a topic with acknowledgement
        final SubUpdater.Acknowledgement subAck =
                sub.subscribeWithAck(singleton("hello"), subscriber.ref()).toCompletableFuture().join();

        // THEN: subscription is acknowledged
        assertThat(subAck.getRequest()).isInstanceOf(SubUpdater.Subscribe.class);
        assertThat(subAck.getRequest().getTopics()).containsExactlyInAnyOrder("hello");

        // WHEN: a message is published on the subscribed topic
        pub.publish("hello", publisher.ref());

        // THEN: the subscriber receives it from the original sender's address
        subscriber.expectMsg("hello");
        assertThat(subscriber.sender().path().address()).isEqualTo(cluster1.selfAddress());
        assertThat(subscriber.sender().path().toStringWithoutAddress())
                .isEqualTo(publisher.ref().path().toStringWithoutAddress());

        // WHEN: subscription is relinquished
        final SubUpdater.Acknowledgement unsubAck =
                sub.unsubscribeWithAck(asList("hello", "world"), subscriber.ref()).toCompletableFuture().join();
        assertThat(unsubAck.getRequest()).isInstanceOf(SubUpdater.Unsubscribe.class);
        assertThat(unsubAck.getRequest().getTopics()).containsExactlyInAnyOrder("hello", "world");

        // THEN: the subscriber does not receive published messages any more
        pub.publish("hello", publisher.ref());
        pub.publish("hello world", publisher.ref());
        subscriber.expectNoMessage();
    }};
}
 
Example 4
Source File: ConnectionPersistenceActorTest.java    From ditto with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void manageConnection() {
    new TestKit(actorSystem) {{
        final TestProbe probe = TestProbe.apply(actorSystem);
        final ActorRef underTest = TestConstants.createConnectionSupervisorActor(
                connectionId, actorSystem, conciergeForwarder,
                (connection, concierge, connectionActor) -> MockClientActor.props(probe.ref()),
                TestConstants.dummyDittoProtocolSub(pubSubMediator, dittoProtocolSubMock),
                pubSubMediator
        );
        watch(underTest);

        // create connection
        underTest.tell(createConnection, getRef());
        probe.expectMsg(openConnection);
        expectMsg(createConnectionResponse);
        expectRemoveSubscriber(1);
        expectSubscribe(TWIN_AND_LIVE_EVENTS, SUBJECTS);

        // close connection
        underTest.tell(closeConnection, getRef());
        probe.expectMsg(closeConnection);
        expectMsg(closeConnectionResponse);
        expectRemoveSubscriber(2);

        // delete connection
        underTest.tell(deleteConnection, getRef());
        expectMsg(deleteConnectionResponse);
        probe.expectNoMessage();
        expectTerminated(underTest);
    }};
}
 
Example 5
Source File: ConnectionPersistenceActorTest.java    From ditto with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void deleteConnectionUpdatesSubscriptions() {
    new TestKit(actorSystem) {{
        final TestProbe probe = TestProbe.apply(actorSystem);
        final ActorRef underTest = TestConstants.createConnectionSupervisorActor(
                connectionId, actorSystem, conciergeForwarder,
                (connection, concierge, connectionActor) -> MockClientActor.props(probe.ref()),
                TestConstants.dummyDittoProtocolSub(pubSubMediator, dittoProtocolSubMock),
                pubSubMediator
        );
        watch(underTest);

        // create connection
        underTest.tell(createConnection, getRef());
        probe.expectMsg(openConnection);
        expectMsg(createConnectionResponse);
        expectRemoveSubscriber(1);
        expectSubscribe(TWIN_AND_LIVE_EVENTS, SUBJECTS);

        // delete connection
        underTest.tell(deleteConnection, getRef());
        expectMsg(deleteConnectionResponse);
        expectRemoveSubscriber(2);
        probe.expectNoMessage();
        expectTerminated(underTest);
    }};

}
 
Example 6
Source File: ConnectionPersistenceActorTest.java    From ditto with Eclipse Public License 2.0 5 votes vote down vote up
@SafeVarargs
private void sendCommandWithEnabledBlacklist(
        final Map.Entry<ConnectivityCommand<?>, Consumer<Object>>... commands) {
    final Config configWithBlacklist =
            TestConstants.CONFIG.withValue("ditto.connectivity.connection.blacklisted-hostnames",
                    ConfigValueFactory.fromAnyRef("127.0.0.1"));
    final ActorSystem systemWithBlacklist = ActorSystem.create(getClass().getSimpleName() + "WithBlacklist",
            configWithBlacklist);
    final ActorRef pubSubMediator = DistributedPubSub.get(systemWithBlacklist).mediator();
    final ActorRef conciergeForwarder =
            systemWithBlacklist.actorOf(TestConstants.ConciergeForwarderActorMock.props());

    try {
        new TestKit(systemWithBlacklist) {{
            final TestProbe probe = TestProbe.apply(systemWithBlacklist);
            final ActorRef underTest =
                    TestConstants.createConnectionSupervisorActor(connectionId, systemWithBlacklist,
                            pubSubMediator,
                            conciergeForwarder,
                            (connection, concierge, connectionActor) -> MockClientActor.props(probe.ref()));
            watch(underTest);

            for (final Map.Entry<ConnectivityCommand<?>, Consumer<Object>> command : commands) {
                underTest.tell(command.getKey(), getRef());
                command.getValue().accept(expectMsgClass(Duration.ofSeconds(10), Object.class));
            }

            // assert that client actor is not called for closed connection
            probe.expectNoMessage();
        }};
    } finally {
        TestKit.shutdownActorSystem(systemWithBlacklist);
    }
}
 
Example 7
Source File: ConnectionPersistenceActorTest.java    From ditto with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void modifyConnectionInClosedState() {
    new TestKit(actorSystem) {{
        final TestProbe probe = TestProbe.apply(actorSystem);
        final ActorRef underTest =
                TestConstants.createConnectionSupervisorActor(connectionId, actorSystem, pubSubMediator,
                        conciergeForwarder,
                        (connection, concierge, connectionActor) -> MockClientActor.props(probe.ref()));
        watch(underTest);

        // create connection
        underTest.tell(createConnection, getRef());
        probe.expectMsg(openConnection);
        expectMsg(createConnectionResponse);

        // create connection
        underTest.tell(closeConnection, getRef());
        probe.expectMsg(closeConnection);
        expectMsg(closeConnectionResponse);

        // modify connection
        underTest.tell(modifyClosedConnection, getRef());
        // client actor is not informed about modification as it is not started
        probe.expectNoMessage();
        expectMsg(modifyConnectionResponse);
    }};
}
 
Example 8
Source File: ConnectionPersistenceActorTest.java    From ditto with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void retrieveMetricsInClosedStateDoesNotStartClientActor() {
    new TestKit(actorSystem) {{
        final TestProbe probe = TestProbe.apply(actorSystem);
        final ActorRef underTest =
                TestConstants.createConnectionSupervisorActor(connectionId, actorSystem, pubSubMediator,
                        conciergeForwarder,
                        (connection, concierge, connectionActor) -> MockClientActor.props(probe.ref()));
        watch(underTest);

        // create connection
        underTest.tell(createClosedConnection, getRef());
        expectMsg(createClosedConnectionResponse);
        probe.expectNoMessage();

        // retrieve metrics
        underTest.tell(RetrieveConnectionMetrics.of(connectionId, DittoHeaders.empty()), getRef());
        probe.expectNoMessage();

        final RetrieveConnectionMetricsResponse metricsResponse =
                RetrieveConnectionMetricsResponse.getBuilder(connectionId, DittoHeaders.empty())
                        .connectionMetrics(ConnectivityModelFactory.emptyConnectionMetrics())
                        .sourceMetrics(ConnectivityModelFactory.emptySourceMetrics())
                        .targetMetrics(ConnectivityModelFactory.emptyTargetMetrics())
                        .build();
        expectMsg(metricsResponse);
    }};
}
 
Example 9
Source File: ConnectionPersistenceActorTest.java    From ditto with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void retrieveLogsInClosedStateDoesNotStartClientActor() {
    new TestKit(actorSystem) {{
        final TestProbe probe = TestProbe.apply(actorSystem);
        final ActorRef underTest =
                TestConstants.createConnectionSupervisorActor(connectionId, actorSystem, pubSubMediator,
                        conciergeForwarder,
                        (connection, concierge, connectionActor) -> MockClientActor.props(probe.ref()));
        watch(underTest);

        // create connection
        underTest.tell(createClosedConnection, getRef());
        expectMsg(createClosedConnectionResponse);
        probe.expectNoMessage();

        // retrieve logs
        underTest.tell(RetrieveConnectionLogs.of(connectionId, DittoHeaders.empty()), getRef());
        probe.expectNoMessage();

        final RetrieveConnectionLogsResponse logsResponse =
                RetrieveConnectionLogsResponse.of(connectionId,
                        Collections.emptyList(),
                        null,
                        null,
                        DittoHeaders.empty());
        expectMsg(logsResponse);
    }};
}
 
Example 10
Source File: AbstractConsumerActorTest.java    From ditto with Eclipse Public License 2.0 5 votes vote down vote up
private void testInboundMessage(final Map.Entry<String, Object> header,
        final int forwardedToConcierge,
        final int respondedToCaller,
        final Consumer<Signal<?>> verifySignal,
        final Consumer<OutboundSignal.Mapped> verifyResponse,
        final PayloadMapping payloadMapping
) {

    new TestKit(actorSystem) {{
        final TestProbe sender = TestProbe.apply(actorSystem);
        final TestProbe concierge = TestProbe.apply(actorSystem);
        final TestProbe clientActor = TestProbe.apply(actorSystem);

        final ActorRef mappingActor = setupMessageMappingProcessorActor(clientActor.ref(), concierge.ref());

        final ActorRef underTest = actorSystem.actorOf(getConsumerActorProps(mappingActor, payloadMapping));

        underTest.tell(getInboundMessage(header), sender.ref());

        if (forwardedToConcierge >= 0) {
            for (int i = 0; i < forwardedToConcierge; i++) {
                final ModifyThing modifyThing = concierge.expectMsgClass(ModifyThing.class);
                assertThat((CharSequence) modifyThing.getThingEntityId()).isEqualTo(TestConstants.Things.THING_ID);
                verifySignal.accept(modifyThing);
            }
        } else {
            concierge.expectNoMessage(ONE_SECOND);
        }

        if (respondedToCaller >= 0) {
            for (int i = 0; i < respondedToCaller; i++) {
                final PublishMappedMessage publishMappedMessage =
                        clientActor.expectMsgClass(PublishMappedMessage.class);
                verifyResponse.accept(publishMappedMessage.getOutboundSignal());
            }
        } else {
            clientActor.expectNoMessage(ONE_SECOND);
        }
    }};
}
 
Example 11
Source File: PolicyPersistenceActorTest.java    From ditto with Eclipse Public License 2.0 4 votes vote down vote up
@Test
public void checkForActivityOfNonexistentPolicy() {
    new TestKit(actorSystem) {
        {
            // GIVEN: a PolicyPersistenceActor is created in a parent that forwards all messages to us
            final PolicyId policyId = PolicyId.of("test.ns", "nonexistent.policy");
            final Props persistentActorProps =
                    PolicyPersistenceActor.props(policyId, new PolicyMongoSnapshotAdapter(), pubSubMediator);

            final TestProbe errorsProbe = TestProbe.apply(actorSystem);

            final Props parentProps = Props.create(Actor.class, () -> new AbstractActor() {

                @Override
                public void preStart() {
                    getContext().actorOf(persistentActorProps);
                }

                @Override
                public SupervisorStrategy supervisorStrategy() {
                    return new OneForOneStrategy(true,
                            DeciderBuilder.matchAny(throwable -> {
                                errorsProbe.ref().tell(throwable, getSelf());
                                return SupervisorStrategy.restart();
                            }).build());
                }

                @Override
                public Receive createReceive() {
                    return ReceiveBuilder.create()
                            .matchAny(message -> {
                                if (getTestActor().equals(getSender())) {
                                    getContext().actorSelection(getSelf().path().child("*"))
                                            .forward(message, getContext());
                                } else {
                                    getTestActor().forward(message, getContext());
                                }
                            })
                            .build();
                }
            });

            // WHEN: CheckForActivity is sent to a persistence actor of nonexistent policy after startup
            final ActorRef underTest = actorSystem.actorOf(parentProps);

            final Object checkForActivity = AbstractShardedPersistenceActor.checkForActivity(1L);
            underTest.tell(checkForActivity, getRef());
            underTest.tell(checkForActivity, getRef());
            underTest.tell(checkForActivity, getRef());

            // THEN: persistence actor requests shutdown
            expectMsg(PolicySupervisorActor.Control.PASSIVATE);

            // THEN: persistence actor should not throw anything.
            errorsProbe.expectNoMessage(scala.concurrent.duration.Duration.create(3, TimeUnit.SECONDS));
        }
    };
}