Skip to content

Commit

Permalink
[mqtt] Try to make tests more stable (#9297)
Browse files Browse the repository at this point in the history
* [mqtt] Try to make tests more stable
* Increase timeout from 10ms to 10sec

Signed-off-by: Fabian Wolter <[email protected]>
  • Loading branch information
fwolter authored Dec 9, 2020
1 parent f152a58 commit 34a9c54
Showing 1 changed file with 11 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.openhab.core.io.transport.mqtt.MqttConnectionState;
import org.openhab.core.io.transport.mqtt.MqttException;
import org.openhab.core.io.transport.mqtt.MqttService;
import org.openhab.core.test.java.JavaTest;
import org.openhab.core.thing.Bridge;
import org.openhab.core.thing.ThingStatus;
import org.openhab.core.thing.ThingStatusInfo;
Expand All @@ -48,7 +49,7 @@
*/
@ExtendWith(MockitoExtension.class)
@MockitoSettings(strictness = Strictness.WARN)
public class BrokerHandlerTest {
public class BrokerHandlerTest extends JavaTest {
private ScheduledExecutorService scheduler;

private @Mock ThingHandlerCallback callback;
Expand All @@ -60,10 +61,10 @@ public class BrokerHandlerTest {
private BrokerHandler handler;

@BeforeEach
public void setUp() throws ConfigurationException, MqttException {
public void setUp() {
scheduler = new ScheduledThreadPoolExecutor(1);
connection = spy(new MqttBrokerConnectionEx("10.10.0.10", 80, false, "BrokerHandlerTest"));
connection.setTimeoutExecutor(scheduler, 10);
connection.setTimeoutExecutor(scheduler, 10000);
connection.setConnectionCallback(connection);

Configuration config = new Configuration();
Expand All @@ -79,8 +80,7 @@ public void tearDown() {
}

@Test
public void handlerInitWithoutUrl()
throws InterruptedException, IllegalArgumentException, MqttException, ConfigurationException {
public void handlerInitWithoutUrl() throws IllegalArgumentException {
// Assume it is a real handler and not a mock as defined above
handler = new BrokerHandler(thing);
assertThrows(IllegalArgumentException.class, () -> initializeHandlerWaitForTimeout());
Expand All @@ -97,8 +97,7 @@ public void createBrokerConnection() {
}

@Test
public void handlerInit()
throws InterruptedException, IllegalArgumentException, MqttException, ConfigurationException {
public void handlerInit() throws InterruptedException, IllegalArgumentException {
assertThat(initializeHandlerWaitForTimeout(), is(true));

ArgumentCaptor<ThingStatusInfo> statusInfoCaptor = ArgumentCaptor.forClass(ThingStatusInfo.class);
Expand All @@ -116,22 +115,21 @@ public void handlerInit()
* @throws MqttException
* @throws ConfigurationException
*/
boolean initializeHandlerWaitForTimeout()
throws InterruptedException, IllegalArgumentException, MqttException, ConfigurationException {
boolean initializeHandlerWaitForTimeout() throws InterruptedException, IllegalArgumentException {
MqttBrokerConnection c = connection;

MqttConnectionObserverEx o = new MqttConnectionObserverEx();
c.addConnectionObserver(o);

assertThat(connection.connectionState(), is(MqttConnectionState.DISCONNECTED));
handler.initialize();
verify(connection, times(2)).addConnectionObserver(any());
verify(connection, times(1)).start();
waitForAssert(() -> verify(connection, times(2)).addConnectionObserver(any()));
waitForAssert(() -> verify(connection, times(1)).start());
// First we expect a CONNECTING state and then a CONNECTED unique state change
assertThat(o.counter, is(2));
waitForAssert(() -> assertThat(o.counter, is(2)));
// First we expect a CONNECTING state and then a CONNECTED state change
// (and other CONNECTED after the future completes)
verify(handler, times(3)).connectionStateChanged(any(), any());
waitForAssert(() -> verify(handler, times(3)).connectionStateChanged(any(), any()));
return true;
}
}

0 comments on commit 34a9c54

Please sign in to comment.