Search in sources :

Example 1 with ReconnectListener

use of org.eclipse.hono.client.ReconnectListener in project hono by eclipse.

the class AmqpAdapterClientCommandConsumerTest method testReceiverIsRecreatedOnConnectionFailure.

/**
 * Verifies that the proton receiver is recreated after a reconnect.
 */
@Test
public void testReceiverIsRecreatedOnConnectionFailure() {
    final AtomicReference<ReconnectListener<HonoConnection>> reconnectListener = new AtomicReference<>();
    doAnswer(invocation -> {
        reconnectListener.set(invocation.getArgument(0));
        return null;
    }).when(connection).addReconnectListener(any());
    // GIVEN a connected command consumer
    @SuppressWarnings("unchecked") final Future<CommandConsumer> consumerFuture = AmqpAdapterClientCommandConsumer.create(connection, mock(BiConsumer.class));
    final AmqpAdapterClientCommandConsumer commandConsumer = (AmqpAdapterClientCommandConsumer) consumerFuture.result();
    // WHEN the connection is re-established
    final ProtonReceiver newReceiver = createNewProtonReceiver(connection);
    reconnectListener.get().onReconnect(null);
    // THEN the receiver is recreated
    verify(connection, times(2)).createReceiver(eq("command"), eq(ProtonQoS.AT_LEAST_ONCE), any(ProtonMessageHandler.class), VertxMockSupport.anyHandler());
    final ProtonReceiver actual = commandConsumer.getReceiver();
    assertThat(actual).isNotEqualTo(originalReceiver);
    assertThat(actual).isEqualTo(newReceiver);
}
Also used : ProtonReceiver(io.vertx.proton.ProtonReceiver) ProtonMessageHandler(io.vertx.proton.ProtonMessageHandler) CommandConsumer(org.eclipse.hono.client.command.CommandConsumer) AtomicReference(java.util.concurrent.atomic.AtomicReference) ReconnectListener(org.eclipse.hono.client.ReconnectListener) BiConsumer(java.util.function.BiConsumer) Test(org.junit.jupiter.api.Test)

Aggregations

ProtonMessageHandler (io.vertx.proton.ProtonMessageHandler)1 ProtonReceiver (io.vertx.proton.ProtonReceiver)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 BiConsumer (java.util.function.BiConsumer)1 ReconnectListener (org.eclipse.hono.client.ReconnectListener)1 CommandConsumer (org.eclipse.hono.client.command.CommandConsumer)1 Test (org.junit.jupiter.api.Test)1