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));
}
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);
}
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);
}
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;
}
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);
}
Aggregations