use of org.eclipse.paho.client.mqttv3.IMqttClient in project spring-integration by spring-projects.
the class MqttAdapterTests method testStopActionNever.
@Test
public void testStopActionNever() throws Exception {
final IMqttClient client = mock(IMqttClient.class);
MqttPahoMessageDrivenChannelAdapter adapter = buildAdapter(client, null, ConsumerStopAction.UNSUBSCRIBE_NEVER);
adapter.start();
adapter.stop();
verifyNotUnsubscribe(client);
}
use of org.eclipse.paho.client.mqttv3.IMqttClient in project spring-integration by spring-projects.
the class MqttAdapterTests method testReconnect.
@Test
public void testReconnect() throws Exception {
final IMqttClient client = mock(IMqttClient.class);
MqttPahoMessageDrivenChannelAdapter adapter = buildAdapter(client, null, ConsumerStopAction.UNSUBSCRIBE_NEVER);
adapter.setRecoveryInterval(10);
Log logger = spy(TestUtils.getPropertyValue(adapter, "logger", Log.class));
new DirectFieldAccessor(adapter).setPropertyValue("logger", logger);
given(logger.isDebugEnabled()).willReturn(true);
final AtomicInteger attemptingReconnectCount = new AtomicInteger();
willAnswer(i -> {
if (attemptingReconnectCount.getAndIncrement() == 0) {
adapter.connectionLost(new RuntimeException("while schedule running"));
}
i.callRealMethod();
return null;
}).given(logger).debug("Attempting reconnect");
ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
taskScheduler.initialize();
adapter.setTaskScheduler(taskScheduler);
adapter.start();
adapter.connectionLost(new RuntimeException("initial"));
Thread.sleep(1000);
// the following assertion should be equalTo, but leq to protect against a slow CI server
assertThat(attemptingReconnectCount.get(), lessThanOrEqualTo(2));
adapter.stop();
taskScheduler.destroy();
}
use of org.eclipse.paho.client.mqttv3.IMqttClient in project spring-integration by spring-projects.
the class BrokerRunning method apply.
@Override
public Statement apply(Statement base, Description description) {
assumeTrue(brokerOnline.get(port));
String url = "tcp://localhost:" + port;
IMqttClient client = null;
try {
client = new DefaultMqttPahoClientFactory().getClientInstance(url, "junit-" + System.currentTimeMillis());
client.connect();
} catch (MqttException e) {
logger.warn("Tests not running because no broker on " + url + ":", e);
assumeNoException(e);
} finally {
if (client != null) {
try {
client.disconnect();
client.close();
} catch (MqttException e) {
}
}
}
return super.apply(base, description);
}
Aggregations