From 34a9c5482a9284ea62362e430013de6d546377ef Mon Sep 17 00:00:00 2001 From: Fabian Wolter Date: Wed, 9 Dec 2020 13:21:58 +0100 Subject: [PATCH] [mqtt] Try to make tests more stable (#9297) * [mqtt] Try to make tests more stable * Increase timeout from 10ms to 10sec Signed-off-by: Fabian Wolter --- .../mqtt/handler/BrokerHandlerTest.java | 24 +++++++++---------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/bundles/org.openhab.binding.mqtt/src/test/java/org/openhab/binding/mqtt/handler/BrokerHandlerTest.java b/bundles/org.openhab.binding.mqtt/src/test/java/org/openhab/binding/mqtt/handler/BrokerHandlerTest.java index 6498ccf54189c..3412e1beec785 100644 --- a/bundles/org.openhab.binding.mqtt/src/test/java/org/openhab/binding/mqtt/handler/BrokerHandlerTest.java +++ b/bundles/org.openhab.binding.mqtt/src/test/java/org/openhab/binding/mqtt/handler/BrokerHandlerTest.java @@ -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; @@ -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; @@ -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(); @@ -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()); @@ -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 statusInfoCaptor = ArgumentCaptor.forClass(ThingStatusInfo.class); @@ -116,8 +115,7 @@ 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(); @@ -125,13 +123,13 @@ boolean initializeHandlerWaitForTimeout() 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; } }