Search in sources :

Example 1 with JoynrMqttClient

use of io.joynr.messaging.mqtt.JoynrMqttClient in project joynr by bmwcarit.

the class MqttPahoClientTest method mqttClientTestShutdownIfDisconnectFromMQTT.

@Test
public void mqttClientTestShutdownIfDisconnectFromMQTT() throws Exception {
    properties.put(MqttModule.PROPERTY_KEY_MQTT_BROKER_URI, "tcp://localhost:1111");
    properties.put(MqttModule.PROPERTY_KEY_MQTT_RECONNECT_SLEEP_MS, "100");
    // create and start client
    final JoynrMqttClient client = createMqttClientInternal(mock(MqttStatusReceiver.class));
    final Semaphore semaphoreBeforeStartMethod = new Semaphore(0);
    final Semaphore semaphoreAfterStartMethod = new Semaphore(0);
    final int timeout = 500;
    Runnable myRunnable = new Runnable() {

        @Override
        public void run() {
            semaphoreBeforeStartMethod.release();
            client.start();
            semaphoreAfterStartMethod.release();
        }
    };
    new Thread(myRunnable).start();
    assertTrue(semaphoreBeforeStartMethod.tryAcquire(timeout, TimeUnit.MILLISECONDS));
    // sleep in order to increase the probability of the runnable
    // to be in the sleep part of the start method
    Thread.sleep(timeout);
    // At this point the semaphoreAfterStartMethod is supposed to be not released
    // because we expect to be still in start()
    assertFalse(semaphoreAfterStartMethod.tryAcquire());
    client.shutdown();
    assertTrue(semaphoreAfterStartMethod.tryAcquire(timeout, TimeUnit.MILLISECONDS));
}
Also used : JoynrMqttClient(io.joynr.messaging.mqtt.JoynrMqttClient) MqttStatusReceiver(io.joynr.messaging.mqtt.statusmetrics.MqttStatusReceiver) Semaphore(java.util.concurrent.Semaphore) Test(org.junit.Test)

Example 2 with JoynrMqttClient

use of io.joynr.messaging.mqtt.JoynrMqttClient in project joynr by bmwcarit.

the class MqttPahoClientTest method testClientNotifiesStatusReceiverAboutShutdownDisconnect.

@Test
public void testClientNotifiesStatusReceiverAboutShutdownDisconnect() throws Exception {
    final MqttStatusReceiver mqttStatusReceiver = mock(MqttStatusReceiver.class);
    final JoynrMqttClient mqttClient = createMqttClientWithoutSubscription(false, mqttStatusReceiver);
    verify(mqttStatusReceiver).notifyConnectionStatusChanged(MqttStatusReceiver.ConnectionStatus.CONNECTED);
    mqttClient.shutdown();
    verify(mqttStatusReceiver).notifyConnectionStatusChanged(MqttStatusReceiver.ConnectionStatus.NOT_CONNECTED);
}
Also used : JoynrMqttClient(io.joynr.messaging.mqtt.JoynrMqttClient) MqttStatusReceiver(io.joynr.messaging.mqtt.statusmetrics.MqttStatusReceiver) Test(org.junit.Test)

Example 3 with JoynrMqttClient

use of io.joynr.messaging.mqtt.JoynrMqttClient in project joynr by bmwcarit.

the class MqttPahoClientTest method testClientNotifiesStatusReceiverAboutBrokerDisconnect.

// This test was disabled, because it runs perfectly on a local machine but not in the CI.
// Further investigations are required to stabilize this test.
@Test
@Ignore
public void testClientNotifiesStatusReceiverAboutBrokerDisconnect() throws Exception {
    final MqttStatusReceiver mqttStatusReceiver = mock(MqttStatusReceiver.class);
    @SuppressWarnings("unused") final JoynrMqttClient mqttClient = createMqttClientWithoutSubscription(false, mqttStatusReceiver);
    verify(mqttStatusReceiver).notifyConnectionStatusChanged(MqttStatusReceiver.ConnectionStatus.CONNECTED);
    stopBroker();
    Thread.sleep(1000);
    verify(mqttStatusReceiver).notifyConnectionStatusChanged(MqttStatusReceiver.ConnectionStatus.NOT_CONNECTED);
    startBroker();
    Thread.sleep(2000);
    verify(mqttStatusReceiver, times(2)).notifyConnectionStatusChanged(MqttStatusReceiver.ConnectionStatus.CONNECTED);
}
Also used : JoynrMqttClient(io.joynr.messaging.mqtt.JoynrMqttClient) MqttStatusReceiver(io.joynr.messaging.mqtt.statusmetrics.MqttStatusReceiver) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 4 with JoynrMqttClient

use of io.joynr.messaging.mqtt.JoynrMqttClient in project joynr by bmwcarit.

the class MqttPahoClientTest method createMqttClientWithoutSubscription.

private JoynrMqttClient createMqttClientWithoutSubscription(boolean isSecureConnection, final MqttStatusReceiver mqttStatusReceiver) {
    if (isSecureConnection) {
        properties.put(MqttModule.PROPERTY_KEY_MQTT_BROKER_URI, "ssl://localhost:8883");
    } else {
        properties.put(MqttModule.PROPERTY_KEY_MQTT_BROKER_URI, "tcp://localhost:1883");
    }
    JoynrMqttClient client = createMqttClientInternal(mqttStatusReceiver);
    client.start();
    return client;
}
Also used : JoynrMqttClient(io.joynr.messaging.mqtt.JoynrMqttClient)

Example 5 with JoynrMqttClient

use of io.joynr.messaging.mqtt.JoynrMqttClient in project joynr by bmwcarit.

the class MqttPahoClientTest method mqttClientTestResubscriptionWithCleanRestartEnabled.

@Test
public void mqttClientTestResubscriptionWithCleanRestartEnabled() throws Exception {
    properties.put(MqttModule.PROPERTY_KEY_MQTT_BROKER_URI, "tcp://localhost:1883");
    injector = Guice.createInjector(new MqttPahoModule(), new JoynrPropertiesModule(properties), new AbstractModule() {

        @Override
        protected void configure() {
            bind(MessageRouter.class).toInstance(mockMessageRouter);
            bind(ScheduledExecutorService.class).annotatedWith(Names.named(MessageRouter.SCHEDULEDTHREADPOOL)).toInstance(Executors.newScheduledThreadPool(10));
            bind(RawMessagingPreprocessor.class).to(NoOpRawMessagingPreprocessor.class);
            Multibinder.newSetBinder(binder(), new TypeLiteral<JoynrMessageProcessor>() {
            });
        }
    });
    ownTopic = injector.getInstance((Key.get(MqttAddress.class, Names.named(MqttModule.PROPERTY_MQTT_GLOBAL_ADDRESS))));
    ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(10);
    MqttClientIdProvider mqttClientIdProvider = injector.getInstance(MqttClientIdProvider.class);
    String clientId = mqttClientIdProvider.getClientId();
    String brokerUri = "tcp://localhost:1883";
    int reconnectSleepMs = 100;
    int keepAliveTimerSec = 60;
    int connectionTimeoutSec = 60;
    int timeToWaitMs = -1;
    int maxMsgsInflight = 100;
    int maxMsgSizeBytes = 0;
    boolean cleanSession = true;
    MqttClient mqttClient = new MqttClient(brokerUri, clientId, new MemoryPersistence(), scheduledExecutorService);
    joynrMqttClient = new MqttPahoClient(mqttClient, reconnectSleepMs, keepAliveTimerSec, connectionTimeoutSec, timeToWaitMs, maxMsgsInflight, maxMsgSizeBytes, cleanSession, "", "", "", "", mock(MqttStatusReceiver.class));
    joynrMqttClient.start();
    joynrMqttClient.setMessageListener(mockReceiver);
    joynrMqttClient.subscribe(ownTopic.getTopic());
    // manually call disconnect and connectionLost
    mqttClient.disconnect(500);
    MqttException exeption = new MqttException(MqttException.REASON_CODE_CLIENT_TIMEOUT);
    MqttPahoClient mqttPahoClient = (MqttPahoClient) joynrMqttClient;
    mqttPahoClient.connectionLost(exeption);
    joynrMqttClientPublishAndVerifyReceivedMessage(serializedMessage);
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) MemoryPersistence(org.eclipse.paho.client.mqttv3.persist.MemoryPersistence) MessageRouter(io.joynr.messaging.routing.MessageRouter) MqttClientIdProvider(io.joynr.messaging.mqtt.MqttClientIdProvider) JoynrMessageProcessor(io.joynr.messaging.JoynrMessageProcessor) NoOpRawMessagingPreprocessor(io.joynr.messaging.NoOpRawMessagingPreprocessor) RawMessagingPreprocessor(io.joynr.messaging.RawMessagingPreprocessor) AbstractModule(com.google.inject.AbstractModule) JoynrMqttClient(io.joynr.messaging.mqtt.JoynrMqttClient) MqttClient(org.eclipse.paho.client.mqttv3.MqttClient) JoynrPropertiesModule(io.joynr.common.JoynrPropertiesModule) MqttException(org.eclipse.paho.client.mqttv3.MqttException) MqttAddress(joynr.system.RoutingTypes.MqttAddress) Test(org.junit.Test)

Aggregations

JoynrMqttClient (io.joynr.messaging.mqtt.JoynrMqttClient)7 Test (org.junit.Test)4 MqttStatusReceiver (io.joynr.messaging.mqtt.statusmetrics.MqttStatusReceiver)3 AbstractModule (com.google.inject.AbstractModule)2 JoynrPropertiesModule (io.joynr.common.JoynrPropertiesModule)2 JoynrMessageProcessor (io.joynr.messaging.JoynrMessageProcessor)2 NoOpRawMessagingPreprocessor (io.joynr.messaging.NoOpRawMessagingPreprocessor)2 RawMessagingPreprocessor (io.joynr.messaging.RawMessagingPreprocessor)2 MessageRouter (io.joynr.messaging.routing.MessageRouter)2 MqttClient (org.eclipse.paho.client.mqttv3.MqttClient)2 MqttException (org.eclipse.paho.client.mqttv3.MqttException)2 MemoryPersistence (org.eclipse.paho.client.mqttv3.persist.MemoryPersistence)2 MqttClientFactory (io.joynr.messaging.mqtt.MqttClientFactory)1 MqttClientIdProvider (io.joynr.messaging.mqtt.MqttClientIdProvider)1 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)1 Semaphore (java.util.concurrent.Semaphore)1 MqttAddress (joynr.system.RoutingTypes.MqttAddress)1 Ignore (org.junit.Ignore)1