1818import static org .mockito .ArgumentMatchers .*;
1919import static org .mockito .Mockito .*;
2020
21+ import java .nio .charset .StandardCharsets ;
2122import java .util .ArrayList ;
2223import java .util .Collections ;
2324import java .util .List ;
3031import org .eclipse .jdt .annotation .NonNullByDefault ;
3132import org .junit .jupiter .api .AfterEach ;
3233import org .junit .jupiter .api .BeforeEach ;
33- import org .junit .jupiter .api .Disabled ;
3434import org .junit .jupiter .api .Test ;
3535import org .junit .jupiter .api .extension .ExtendWith ;
3636import org .mockito .Mock ;
5555import org .openhab .core .io .transport .mqtt .MqttBrokerConnection ;
5656import org .openhab .core .io .transport .mqtt .MqttConnectionObserver ;
5757import org .openhab .core .io .transport .mqtt .MqttConnectionState ;
58+ import org .openhab .core .io .transport .mqtt .MqttMessageSubscriber ;
5859import org .openhab .core .library .types .OnOffType ;
5960import org .openhab .core .library .types .QuantityType ;
6061import org .openhab .core .library .unit .SIUnits ;
@@ -72,6 +73,7 @@ public class HomieImplementationTest extends MqttOSGiTest {
7273 private static final String BASE_TOPIC = "homie" ;
7374 private static final String DEVICE_ID = ThingChannelConstants .TEST_HOMIE_THING .getId ();
7475 private static final String DEVICE_TOPIC = BASE_TOPIC + "/" + DEVICE_ID ;
76+ private static final int MQTT_TIMEOUT = 10000 ;
7577
7678 private @ NonNullByDefault ({}) MqttBrokerConnection homieConnection ;
7779 private int registeredTopics = 100 ;
@@ -165,15 +167,13 @@ public void retrieveAllTopics() throws Exception {
165167 "Connection " + homieConnection .getClientId () + " not retrieving all topics " );
166168 }
167169
168- @ Disabled ("https://github.com/openhab/openhab-addons/issues/12667" )
169170 @ Test
170171 public void retrieveOneAttribute () throws Exception {
171172 WaitForTopicValue watcher = new WaitForTopicValue (homieConnection , DEVICE_TOPIC + "/$homie" );
172- assertThat (watcher .waitForTopicValue (1000 ), is ("3.0" ));
173+ assertThat (watcher .waitForTopicValue (MQTT_TIMEOUT ), is ("3.0" ));
173174 }
174175
175176 @ SuppressWarnings ("null" )
176- @ Disabled ("Temporarily disabled: unstable" )
177177 @ Test
178178 public void retrieveAttributes () throws Exception {
179179 assertThat (homieConnection .hasSubscribers (), is (false ));
@@ -183,9 +183,6 @@ public void retrieveAttributes() throws Exception {
183183 Property property = spy (
184184 new Property (DEVICE_TOPIC + "/testnode" , node , "temperature" , callback , new PropertyAttributes ()));
185185
186- // Create a scheduler
187- ScheduledExecutorService scheduler = new ScheduledThreadPoolExecutor (4 );
188-
189186 property .subscribe (homieConnection , scheduler , 500 ).get ();
190187
191188 assertThat (property .attributes .settable , is (true ));
@@ -202,8 +199,8 @@ public void retrieveAttributes() throws Exception {
202199
203200 property .startChannel (homieConnection , scheduler , 500 ).get ();
204201 verify (channelState ).start (any (), any (), anyInt ());
205- verify (channelState , timeout (500 )).processMessage (any (), any ());
206- verify (callback ). updateChannelState (any (), any ());
202+ verify (callback , timeout (MQTT_TIMEOUT )).updateChannelState (any (), any ());
203+ verify (channelState ). processMessage (any (), any ());
207204
208205 assertThat (property .getChannelState ().getCache ().getChannelState (),
209206 is (new QuantityType <>(10 , SIUnits .CELSIUS )));
@@ -231,7 +228,6 @@ public Node createSpyNode(InvocationOnMock invocation) {
231228 }
232229
233230 @ SuppressWarnings ("null" )
234- @ Disabled ("Temporarily disabled: unstable" )
235231 @ Test
236232 public void parseHomieTree () throws Exception {
237233 // Create a Homie Device object. Because spied Nodes are required for call verification,
@@ -297,14 +293,24 @@ public void parseHomieTree() throws Exception {
297293 device .startChannels (homieConnection , scheduler , 50 , handler ).get ();
298294 assertThat (propertyBell .getChannelState ().isStateful (), is (false ));
299295 assertThat (propertyBell .getChannelState ().getCache ().getChannelState (), is (UnDefType .UNDEF ));
300- assertThat (property .getChannelState ().getCache ().getChannelState (),
301- is (new QuantityType <>(10 , SIUnits .CELSIUS )));
296+ Property temperatureProperty = property ;
297+ waitForAssert (() -> assertThat (temperatureProperty .getChannelState ().getCache ().getChannelState (),
298+ is (new QuantityType <>(10 , SIUnits .CELSIUS ))));
302299
303300 property = node .properties .get ("testRetain" );
304- WaitForTopicValue watcher = new WaitForTopicValue (brokerConnection , propertyTestTopic + "/set" );
301+ String commandTopic = propertyTestTopic + "/set" ;
302+ CompletableFuture <String > receivedValue = new CompletableFuture <>();
303+ MqttMessageSubscriber subscriber = (topic , payload ) -> receivedValue
304+ .complete (new String (payload , StandardCharsets .UTF_8 ));
305+ assertTrue (brokerConnection .subscribe (commandTopic , subscriber ).get (5 , TimeUnit .SECONDS ));
306+
305307 // Watch the topic. Publish a retain=false value to MQTT
306- property .getChannelState ().publishValue (OnOffType .OFF ).get ();
307- assertThat (watcher .waitForTopicValue (10000 ), is ("false" ));
308+ try {
309+ property .getChannelState ().publishValue (OnOffType .OFF ).get ();
310+ assertThat (receivedValue .get (MQTT_TIMEOUT , TimeUnit .MILLISECONDS ), is ("false" ));
311+ } finally {
312+ brokerConnection .unsubscribe (commandTopic , subscriber ).get (5 , TimeUnit .SECONDS );
313+ }
308314
309315 // Publish a retain=false value to MQTT.
310316 property .getChannelState ().publishValue (OnOffType .ON ).get ();
0 commit comments