use of org.eclipse.smarthome.io.transport.mqtt.MqttServiceObserver in project smarthome by eclipse.
the class EmbeddedBrokerTools method waitForConnection.
/**
* Request the embedded broker connection from the {@link MqttService} and wait for a connection to be established.
*
* @throws InterruptedException
*/
public MqttBrokerConnection waitForConnection(MqttService mqttService) throws InterruptedException {
embeddedConnection = mqttService.getBrokerConnection(Constants.CLIENTID);
if (embeddedConnection == null) {
Semaphore semaphore = new Semaphore(1);
semaphore.acquire();
MqttServiceObserver observer = new MqttServiceObserver() {
@Override
public void brokerAdded(@NonNull String brokerID, @NonNull MqttBrokerConnection broker) {
if (brokerID.equals(Constants.CLIENTID)) {
embeddedConnection = broker;
semaphore.release();
}
}
@Override
public void brokerRemoved(@NonNull String brokerID, @NonNull MqttBrokerConnection broker) {
}
};
mqttService.addBrokersListener(observer);
assertTrue("Wait for embedded connection client failed", semaphore.tryAcquire(700, TimeUnit.MILLISECONDS));
}
MqttBrokerConnection embeddedConnection = this.embeddedConnection;
if (embeddedConnection == null) {
throw new IllegalStateException();
}
logger.warn("waitForConnection {}", embeddedConnection.connectionState());
Semaphore semaphore = new Semaphore(1);
semaphore.acquire();
MqttConnectionObserver mqttConnectionObserver = (state, error) -> {
if (state == MqttConnectionState.CONNECTED) {
semaphore.release();
}
};
embeddedConnection.addConnectionObserver(mqttConnectionObserver);
if (embeddedConnection.connectionState() == MqttConnectionState.CONNECTED) {
semaphore.release();
}
assertTrue("Connection " + embeddedConnection.getClientId() + " failed. State: " + embeddedConnection.connectionState(), semaphore.tryAcquire(500, TimeUnit.MILLISECONDS));
return embeddedConnection;
}
use of org.eclipse.smarthome.io.transport.mqtt.MqttServiceObserver in project smarthome by eclipse.
the class MqttServiceTests method brokerConnectionListenerTests.
// Tests addBrokersListener/removeBrokersListener
@Test
public void brokerConnectionListenerTests() throws ConfigurationException {
MqttService service = new MqttServiceImpl();
assertFalse(service.hasBrokerObservers());
MqttServiceObserver observer = mock(MqttServiceObserver.class);
service.addBrokersListener(observer);
assertTrue(service.hasBrokerObservers());
MqttBrokerConnectionEx connection = new MqttBrokerConnectionEx("tcp://123.123.123.123", null, false, "brokerConnectionListenerTests");
assertTrue(service.addBrokerConnection("name", connection));
ArgumentCaptor<MqttBrokerConnection> argumentCaptorConn = ArgumentCaptor.forClass(MqttBrokerConnection.class);
ArgumentCaptor<String> argumentCaptorConnName = ArgumentCaptor.forClass(String.class);
verify(observer).brokerAdded(argumentCaptorConnName.capture(), argumentCaptorConn.capture());
assertThat(argumentCaptorConnName.getValue(), equalTo("name"));
assertThat(argumentCaptorConn.getValue(), equalTo(connection));
service.removeBrokerConnection("name");
verify(observer).brokerRemoved(argumentCaptorConnName.capture(), argumentCaptorConn.capture());
assertThat(argumentCaptorConnName.getValue(), equalTo("name"));
assertThat(argumentCaptorConn.getValue(), equalTo(connection));
service.removeBrokersListener(observer);
assertFalse(service.hasBrokerObservers());
}
Aggregations