Skip to content

Commit d929278

Browse files
author
Ravi Nadahar
committed
Adjust tests to accommodate openhab/openhab-core#5032
openhab/openhab-core#5032 made discovery result registration asynchronous, which led to some tests failing because the discovery results aren't immediately available one the call to thingDiscovered() returns - because the registration itself is done in a different thread. This has been addressed by pausing slightly before testing the results. Signed-off-by: Ravi Nadahar <[email protected]>
1 parent fba8b0f commit d929278

File tree

3 files changed

+26
-13
lines changed

3 files changed

+26
-13
lines changed

bundles/org.openhab.binding.boschshc/src/test/java/org/openhab/binding/boschshc/internal/discovery/ThingDiscoveryServiceTest.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import static org.mockito.ArgumentMatchers.any;
2121
import static org.mockito.Mockito.mock;
2222
import static org.mockito.Mockito.never;
23+
import static org.mockito.Mockito.timeout;
2324
import static org.mockito.Mockito.times;
2425
import static org.mockito.Mockito.verify;
2526
import static org.mockito.Mockito.when;
@@ -159,7 +160,7 @@ void testAddDevices() {
159160
fixture.addDevices(devices, emptyRooms);
160161

161162
// two calls for the two devices expected
162-
verify(discoveryListener, times(2)).thingDiscovered(any(), any());
163+
verify(discoveryListener, timeout(1000L).times(2)).thingDiscovered(any(), any());
163164
}
164165

165166
@Test
@@ -185,7 +186,8 @@ void testAddDevice() {
185186
device.name = "Test Name";
186187
fixture.addDevice(device, "TestRoom");
187188

188-
verify(discoveryListener).thingDiscovered(discoveryServiceCaptor.capture(), discoveryResultCaptor.capture());
189+
verify(discoveryListener, timeout(1000L)).thingDiscovered(discoveryServiceCaptor.capture(),
190+
discoveryResultCaptor.capture());
189191

190192
assertThat(discoveryServiceCaptor.getValue().getClass(), is(ThingDiscoveryService.class));
191193
DiscoveryResult result = discoveryResultCaptor.getValue();
@@ -226,7 +228,8 @@ private void assertDeviceNiceName(String deviceName, String roomName, String exp
226228
device.id = "testDevice:ID";
227229
device.name = deviceName;
228230
fixture.addDevice(device, roomName);
229-
verify(discoveryListener).thingDiscovered(discoveryServiceCaptor.capture(), discoveryResultCaptor.capture());
231+
verify(discoveryListener, timeout(1000L)).thingDiscovered(discoveryServiceCaptor.capture(),
232+
discoveryResultCaptor.capture());
230233
assertThat(discoveryServiceCaptor.getValue().getClass(), is(ThingDiscoveryService.class));
231234
DiscoveryResult result = discoveryResultCaptor.getValue();
232235
assertThat(result.getLabel(), is(expectedNiceName));

bundles/org.openhab.binding.mspa/src/test/java/org/openhab/binding/mspa/TestMessages.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ void testToken() {
8787
}
8888

8989
@Test
90-
void testDiscovery() {
90+
void testDiscovery() throws InterruptedException {
9191
Bridge thing = new BridgeImpl(THING_TYPE_OWNER_ACCOUNT, new ThingUID("mspa", "account"));
9292
Map<String, Object> configMap = new HashMap<>();
9393
MSpaDiscoveryService discovery = new MSpaDiscoveryService();
@@ -102,6 +102,7 @@ void testDiscovery() {
102102
try {
103103
String content = new String(Files.readAllBytes(Paths.get(fileName)));
104104
account.decodeDevices(content);
105+
Thread.sleep(500L);
105106
List<DiscoveryResult> results = discoveryListener.getResults();
106107
assertEquals(1, results.size(), "Number of discovery results");
107108
DiscoveryResult result = results.get(0);

bundles/org.openhab.binding.tradfri/src/test/java/org/openhab/binding/tradfri/internal/discovery/TradfriDiscoveryServiceTest.java

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -108,12 +108,13 @@ public void correctSupportedTypes() {
108108
}
109109

110110
@Test
111-
public void validDiscoveryResultWhiteLightW() {
111+
public void validDiscoveryResultWhiteLightW() throws InterruptedException {
112112
String json = "{\"9001\":\"TRADFRI bulb E27 W opal 1000lm\",\"9002\":1492856270,\"9020\":1507194357,\"9003\":65537,\"3311\":[{\"5850\":1,\"5851\":254,\"9003\":0}],\"9054\":0,\"5750\":2,\"9019\":1,\"3\":{\"0\":\"IKEA of Sweden\",\"1\":\"TRADFRI bulb E27 W opal 1000lm\",\"2\":\"\",\"3\":\"1.2.214\",\"6\":1}}";
113113
JsonObject data = JsonParser.parseString(json).getAsJsonObject();
114114

115115
discovery.onUpdate("65537", data);
116116

117+
Thread.sleep(500L);
117118
assertNotNull(discoveryResult);
118119
assertThat(discoveryResult.getFlag(), is(DiscoveryResultFlag.NEW));
119120
assertThat(discoveryResult.getThingUID(), is(new ThingUID("tradfri:0100:1:65537")));
@@ -124,12 +125,13 @@ public void validDiscoveryResultWhiteLightW() {
124125
}
125126

126127
@Test
127-
public void validDiscoveryResultWhiteLightWS() {
128+
public void validDiscoveryResultWhiteLightWS() throws InterruptedException {
128129
String json = "{\"9001\":\"TRADFRI bulb E27 WS opal 980lm\",\"9002\":1492955148,\"9020\":1507200447,\"9003\":65537,\"3311\":[{\"5710\":26909,\"5850\":1,\"5851\":203,\"5707\":0,\"5708\":0,\"5709\":30140,\"5711\":370,\"5706\":\"f1e0b5\",\"9003\":0}],\"9054\":0,\"5750\":2,\"9019\":1,\"3\":{\"0\":\"IKEA of Sweden\",\"1\":\"TRADFRI bulb E27 WS opal 980lm\",\"2\":\"\",\"3\":\"1.2.217\",\"6\":1}}";
129130
JsonObject data = JsonParser.parseString(json).getAsJsonObject();
130131

131132
discovery.onUpdate("65537", data);
132133

134+
Thread.sleep(500L);
133135
assertNotNull(discoveryResult);
134136
assertThat(discoveryResult.getFlag(), is(DiscoveryResultFlag.NEW));
135137
assertThat(discoveryResult.getThingUID(), is(new ThingUID("tradfri:0220:1:65537")));
@@ -140,14 +142,15 @@ public void validDiscoveryResultWhiteLightWS() {
140142
}
141143

142144
@Test
143-
public void validDiscoveryResultWhiteLightWSWithIncompleteJson() {
145+
public void validDiscoveryResultWhiteLightWSWithIncompleteJson() throws InterruptedException {
144146
// We do not always receive a COLOR = "5706" attribute, even the light supports it - but the gateway does not
145147
// seem to have this information, if the bulb is unreachable.
146148
String json = "{\"9001\":\"TRADFRI bulb E27 WS opal 980lm\",\"9002\":1492955148,\"9020\":1506968670,\"9003\":65537,\"3311\":[{\"9003\":0}],\"9054\":0,\"5750\":2,\"9019\":0,\"3\":{\"0\":\"IKEA of Sweden\",\"1\":\"TRADFRI bulb E27 WS opal 980lm\",\"2\":\"\",\"3\":\"1.2.217\",\"6\":1}}";
147149
JsonObject data = JsonParser.parseString(json).getAsJsonObject();
148150

149151
discovery.onUpdate("65537", data);
150152

153+
Thread.sleep(500L);
151154
assertNotNull(discoveryResult);
152155
assertThat(discoveryResult.getFlag(), is(DiscoveryResultFlag.NEW));
153156
assertThat(discoveryResult.getThingUID(), is(new ThingUID("tradfri:0220:1:65537")));
@@ -158,12 +161,13 @@ public void validDiscoveryResultWhiteLightWSWithIncompleteJson() {
158161
}
159162

160163
@Test
161-
public void validDiscoveryResultColorLightCWS() {
164+
public void validDiscoveryResultColorLightCWS() throws InterruptedException {
162165
String json = "{\"9001\":\"TRADFRI bulb E27 CWS opal 600lm\",\"9002\":1505151864,\"9020\":1505433527,\"9003\":65550,\"9019\":1,\"9054\":0,\"5750\":2,\"3\":{\"0\":\"IKEA of Sweden\",\"1\":\"TRADFRI bulb E27 CWS opal 600lm\",\"2\":\"\",\"3\":\"1.3.002\",\"6\":1},\"3311\":[{\"5850\":1,\"5708\":0,\"5851\":254,\"5707\":0,\"5709\":33137,\"5710\":27211,\"5711\":0,\"5706\":\"efd275\",\"9003\":0}]}";
163166
JsonObject data = JsonParser.parseString(json).getAsJsonObject();
164167

165168
discovery.onUpdate("65550", data);
166169

170+
Thread.sleep(500L);
167171
assertNotNull(discoveryResult);
168172
assertThat(discoveryResult.getFlag(), is(DiscoveryResultFlag.NEW));
169173
assertThat(discoveryResult.getThingUID(), is(new ThingUID("tradfri:0210:1:65550")));
@@ -174,12 +178,13 @@ public void validDiscoveryResultColorLightCWS() {
174178
}
175179

176180
@Test
177-
public void validDiscoveryResultAlternativeColorLightCWS() {
181+
public void validDiscoveryResultAlternativeColorLightCWS() throws InterruptedException {
178182
String json = "{\"3311\":[{\"5850\":1,\"5709\":32886,\"5851\":216,\"5707\":5309,\"5708\":52400,\"5710\":27217,\"5706\":\"efd275\",\"9003\":0}],\"9001\":\"Mushroom lamp\",\"9002\":1571036916,\"9020\":1571588312,\"9003\":65539,\"9054\":0,\"9019\":1,\"3\":{\"0\":\"IKEA of Sweden\",\"1\":\"TRADFRI bulb E27 C\\/WS opal 600\",\"2\":\"\",\"3\":\"1.3.009\",\"6\":1},\"5750\":2}";
179183
JsonObject data = JsonParser.parseString(json).getAsJsonObject();
180184

181185
discovery.onUpdate("65539", data);
182186

187+
Thread.sleep(500L);
183188
assertNotNull(discoveryResult);
184189
assertThat(discoveryResult.getFlag(), is(DiscoveryResultFlag.NEW));
185190
assertThat(discoveryResult.getThingUID(), is(new ThingUID("tradfri:0210:1:65539")));
@@ -190,12 +195,13 @@ public void validDiscoveryResultAlternativeColorLightCWS() {
190195
}
191196

192197
@Test
193-
public void validDiscoveryResultRemoteControl() {
198+
public void validDiscoveryResultRemoteControl() throws InterruptedException {
194199
String json = "{\"9001\":\"TRADFRI remote control\",\"9002\":1492843083,\"9020\":1506977986,\"9003\":65536,\"9054\":0,\"5750\":0,\"9019\":1,\"3\":{\"0\":\"IKEA of Sweden\",\"1\":\"TRADFRI remote control\",\"2\":\"\",\"3\":\"1.2.214\",\"6\":3,\"9\":47},\"15009\":[{\"9003\":0}]}";
195200
JsonObject data = JsonParser.parseString(json).getAsJsonObject();
196201

197202
discovery.onUpdate("65536", data);
198203

204+
Thread.sleep(500L);
199205
assertNotNull(discoveryResult);
200206
assertThat(discoveryResult.getFlag(), is(DiscoveryResultFlag.NEW));
201207
assertThat(discoveryResult.getThingUID(), is(new ThingUID("tradfri:0830:1:65536")));
@@ -206,12 +212,13 @@ public void validDiscoveryResultRemoteControl() {
206212
}
207213

208214
@Test
209-
public void validDiscoveryResultWirelessDimmer() {
215+
public void validDiscoveryResultWirelessDimmer() throws InterruptedException {
210216
String json = "{\"9001\":\"TRADFRI wireless dimmer\",\"9002\":1492843083,\"9020\":1506977986,\"9003\":65536,\"9054\":0,\"5750\":0,\"9019\":1,\"3\":{\"0\":\"IKEA of Sweden\",\"1\":\"TRADFRI wireless dimmer\",\"2\":\"\",\"3\":\"1.2.214\",\"6\":3,\"9\":47},\"15009\":[{\"9003\":0}]}";
211217
JsonObject data = JsonParser.parseString(json).getAsJsonObject();
212218

213219
discovery.onUpdate("65536", data);
214220

221+
Thread.sleep(500L);
215222
assertNotNull(discoveryResult);
216223
assertThat(discoveryResult.getFlag(), is(DiscoveryResultFlag.NEW));
217224
assertThat(discoveryResult.getThingUID(), is(new ThingUID("tradfri:0820:1:65536")));
@@ -222,12 +229,13 @@ public void validDiscoveryResultWirelessDimmer() {
222229
}
223230

224231
@Test
225-
public void validDiscoveryResultMotionSensor() {
232+
public void validDiscoveryResultMotionSensor() throws InterruptedException {
226233
String json = "{\"9001\":\"TRADFRI motion sensor\",\"9002\":1492955083,\"9020\":1507120083,\"9003\":65538,\"9054\":0,\"5750\":4,\"9019\":1,\"3\":{\"0\":\"IKEA of Sweden\",\"1\":\"TRADFRI motion sensor\",\"2\":\"\",\"3\":\"1.2.214\",\"6\":3,\"9\":60},\"3300\":[{\"9003\":0}]}";
227234
JsonObject data = JsonParser.parseString(json).getAsJsonObject();
228235

229236
discovery.onUpdate("65538", data);
230237

238+
Thread.sleep(500L);
231239
assertNotNull(discoveryResult);
232240
assertThat(discoveryResult.getFlag(), is(DiscoveryResultFlag.NEW));
233241
assertThat(discoveryResult.getThingUID(), is(new ThingUID("tradfri:0107:1:65538")));
@@ -238,12 +246,13 @@ public void validDiscoveryResultMotionSensor() {
238246
}
239247

240248
@Test
241-
public void validDiscoveryResultAirPurifier() {
249+
public void validDiscoveryResultAirPurifier() throws InterruptedException {
242250
String json = "{\"3\":{\"0\":\"IKEAofSweden\",\"1\":\"STARKVINDAirpurifier\",\"2\":\"\",\"3\":\"1.0.033\",\"6\":1,\"7\":4364},\"5750\":10,\"9001\":\"Luftreiniger\",\"9002\":1633096623,\"9003\":65548,\"9019\":1,\"9020\":1633096633,\"9054\":0,\"15025\":[{\"5900\":1,\"5902\":2,\"5903\":0,\"5904\":259200,\"5905\":0,\"5906\":0,\"5907\":5,\"5908\":10,\"5909\":2,\"5910\":259198,\"9003\":0}]}";
243251
JsonObject data = JsonParser.parseString(json).getAsJsonObject();
244252

245253
discovery.onUpdate("65548", data);
246254

255+
Thread.sleep(500L);
247256
assertNotNull(discoveryResult);
248257
assertThat(discoveryResult.getFlag(), is(DiscoveryResultFlag.NEW));
249258
assertThat(discoveryResult.getThingUID(), is(new ThingUID("tradfri:0007:1:65548")));

0 commit comments

Comments
 (0)