Skip to content

Commit 33bc0d0

Browse files
committed
[UT DataReaderTest] Trying to fix the error in 'DataReaderTest' when running coverage.
Signed-off-by: Tatiana Leon <tatiana.leon@digi.com>
1 parent 4dcd010 commit 33bc0d0

File tree

1 file changed

+60
-38
lines changed

1 file changed

+60
-38
lines changed

library/src/test/java/com/digi/xbee/api/connection/DataReaderTest.java

Lines changed: 60 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818
import java.io.IOException;
1919
import java.io.InputStream;
2020
import java.io.OutputStream;
21+
import java.lang.Thread.State;
22+
import java.lang.management.ManagementFactory;
23+
import java.lang.management.ThreadInfo;
24+
import java.lang.management.ThreadMXBean;
2125
import java.util.ArrayList;
2226
import java.util.HashMap;
2327

@@ -243,6 +247,24 @@ public Object answer(InvocationOnMock invocation) throws Throwable {
243247
@After
244248
public void tearDown() throws Exception {
245249
}
250+
251+
private void waitForInitialization(long threadID) throws InterruptedException {
252+
ThreadMXBean mbean = ManagementFactory.getThreadMXBean();
253+
boolean isWaiting = true;
254+
255+
while (isWaiting) {
256+
ThreadInfo[] infos = mbean.dumpAllThreads(true, true);
257+
for (ThreadInfo threadInfo : infos) {
258+
if (threadInfo.getThreadId() == threadID) {
259+
if (threadInfo.getThreadState() == State.WAITING)
260+
isWaiting = false;
261+
else
262+
Thread.sleep(50);
263+
break;
264+
}
265+
}
266+
}
267+
}
246268

247269
/**
248270
* Test method for {@link com.digi.xbee.api.connection.DataReader#DataReader(IConnectionInterface, com.digi.xbee.api.models.OperatingMode, com.digi.xbee.api.XBeeDevice)}.
@@ -935,9 +957,9 @@ public Integer answer(InvocationOnMock invocation) throws Throwable {
935957
// Call the method under test.
936958
dataReader.start();
937959

938-
Thread.sleep(150);
960+
waitForInitialization(dataReader.getId());
939961
testCI.notifyData();
940-
while (!testCI.alreadyRead)
962+
while (dataReader.isRunning())
941963
Thread.sleep(30);
942964

943965
// Verify the result.
@@ -970,9 +992,9 @@ public XBeePacket answer(InvocationOnMock invocation) throws Throwable {
970992
// Call the method under test.
971993
dataReader.start();
972994

973-
Thread.sleep(150);
995+
waitForInitialization(dataReader.getId());
974996
testCI.notifyData();
975-
while (!testCI.alreadyRead)
997+
while (dataReader.isRunning())
976998
Thread.sleep(30);
977999

9781000
// Verify the result.
@@ -1008,9 +1030,9 @@ public final void testDataReaderReceivePacketNotContemplatedPacket() throws Exce
10081030
// Call the method under test.
10091031
dataReader.start();
10101032

1011-
Thread.sleep(150);
1033+
waitForInitialization(dataReader.getId());
10121034
testCI.notifyData();
1013-
while (!testCI.alreadyRead)
1035+
while (dataReader.isRunning())
10141036
Thread.sleep(30);
10151037

10161038
// Verify the result.
@@ -1050,9 +1072,9 @@ public final void testDataReaderReceivePacketListentoFrameIDNotReceived() throws
10501072
// Call the method under test.
10511073
dataReader.start();
10521074

1053-
Thread.sleep(150);
1075+
waitForInitialization(dataReader.getId());
10541076
testCI.notifyData();
1055-
while (!testCI.alreadyRead)
1077+
while (dataReader.isRunning())
10561078
Thread.sleep(30);
10571079

10581080
// Verify the result.
@@ -1092,9 +1114,9 @@ public final void testDataReaderReceivePacketListentoFrameIDReceived() throws Ex
10921114
// Call the method under test.
10931115
dataReader.start();
10941116

1095-
Thread.sleep(150);
1117+
waitForInitialization(dataReader.getId());
10961118
testCI.notifyData();
1097-
while (!testCI.alreadyRead)
1119+
while (dataReader.isRunning())
10981120
Thread.sleep(30);
10991121

11001122
// Verify the result.
@@ -1134,9 +1156,9 @@ public final void testDataReaderReceivePacketATResponse() throws Exception {
11341156
// Call the method under test.
11351157
dataReader.start();
11361158

1137-
Thread.sleep(150);
1159+
waitForInitialization(dataReader.getId());
11381160
testCI.notifyData();
1139-
while (!testCI.alreadyRead)
1161+
while (dataReader.isRunning())
11401162
Thread.sleep(30);
11411163

11421164
// Verify the result.
@@ -1176,9 +1198,9 @@ public final void testDataReaderReceivePacketRemoteATResponse() throws Exception
11761198
// Call the method under test.
11771199
dataReader.start();
11781200

1179-
Thread.sleep(150);
1201+
waitForInitialization(dataReader.getId());
11801202
testCI.notifyData();
1181-
while (!testCI.alreadyRead)
1203+
while (dataReader.isRunning())
11821204
Thread.sleep(30);
11831205

11841206
// Verify the result.
@@ -1218,9 +1240,9 @@ public final void testDataReaderReceivePacketRX64DataPacket() throws Exception {
12181240
// Call the method under test.
12191241
dataReader.start();
12201242

1221-
Thread.sleep(150);
1243+
waitForInitialization(dataReader.getId());
12221244
testCI.notifyData();
1223-
while (!testCI.alreadyRead)
1245+
while (dataReader.isRunning())
12241246
Thread.sleep(30);
12251247

12261248
// Verify the result.
@@ -1260,9 +1282,9 @@ public final void testDataReaderReceivePacketRX16DataPacket() throws Exception {
12601282
// Call the method under test.
12611283
dataReader.start();
12621284

1263-
Thread.sleep(150);
1285+
waitForInitialization(dataReader.getId());
12641286
testCI.notifyData();
1265-
while (!testCI.alreadyRead)
1287+
while (dataReader.isRunning())
12661288
Thread.sleep(30);
12671289

12681290
// Verify the result.
@@ -1302,9 +1324,9 @@ public final void testDataReaderReceivePacketRXDataPacket() throws Exception {
13021324
// Call the method under test.
13031325
dataReader.start();
13041326

1305-
Thread.sleep(150);
1327+
waitForInitialization(dataReader.getId());
13061328
testCI.notifyData();
1307-
while (!testCI.alreadyRead)
1329+
while (dataReader.isRunning())
13081330
Thread.sleep(30);
13091331

13101332
// Verify the result.
@@ -1344,9 +1366,9 @@ public final void testDataReaderReceivePacketRXIO64DataPacket() throws Exception
13441366
// Call the method under test.
13451367
dataReader.start();
13461368

1347-
Thread.sleep(150);
1369+
waitForInitialization(dataReader.getId());
13481370
testCI.notifyData();
1349-
while (!testCI.alreadyRead)
1371+
while (dataReader.isRunning())
13501372
Thread.sleep(30);
13511373

13521374
// Verify the result.
@@ -1386,9 +1408,9 @@ public final void testDataReaderReceivePacketRXIO16DataPacket() throws Exception
13861408
// Call the method under test.
13871409
dataReader.start();
13881410

1389-
Thread.sleep(150);
1411+
waitForInitialization(dataReader.getId());
13901412
testCI.notifyData();
1391-
while (!testCI.alreadyRead)
1413+
while (dataReader.isRunning())
13921414
Thread.sleep(30);
13931415

13941416
// Verify the result.
@@ -1428,9 +1450,9 @@ public final void testDataReaderReceivePacketRXIOPacket() throws Exception {
14281450
// Call the method under test.
14291451
dataReader.start();
14301452

1431-
Thread.sleep(150);
1453+
waitForInitialization(dataReader.getId());
14321454
testCI.notifyData();
1433-
while (!testCI.alreadyRead)
1455+
while (dataReader.isRunning())
14341456
Thread.sleep(30);
14351457

14361458
// Verify the result.
@@ -1470,9 +1492,9 @@ public final void testDataReaderReceivePacketModemStatusPacket() throws Exceptio
14701492
// Call the method under test.
14711493
dataReader.start();
14721494

1473-
Thread.sleep(150);
1495+
waitForInitialization(dataReader.getId());
14741496
testCI.notifyData();
1475-
while (!testCI.alreadyRead)
1497+
while (dataReader.isRunning())
14761498
Thread.sleep(30);
14771499

14781500
// Verify the result.
@@ -1512,9 +1534,9 @@ public final void testDataReaderReceivePacketExplicitIndicatorDigiPacket() throw
15121534
// Call the method under test.
15131535
dataReader.start();
15141536

1515-
Thread.sleep(150);
1537+
waitForInitialization(dataReader.getId());
15161538
testCI.notifyData();
1517-
while (!testCI.alreadyRead)
1539+
while (dataReader.isRunning())
15181540
Thread.sleep(30);
15191541

15201542
// Verify the result.
@@ -1554,9 +1576,9 @@ public final void testDataReaderReceivePacketExplicitIndicatorPacket() throws Ex
15541576
// Call the method under test.
15551577
dataReader.start();
15561578

1557-
Thread.sleep(150);
1579+
waitForInitialization(dataReader.getId());
15581580
testCI.notifyData();
1559-
while (!testCI.alreadyRead)
1581+
while (dataReader.isRunning())
15601582
Thread.sleep(30);
15611583

15621584
// Verify the result.
@@ -1593,9 +1615,9 @@ public XBeePacket answer(InvocationOnMock invocation) throws Throwable {
15931615
// Call the method under test.
15941616
dataReader.start();
15951617

1596-
Thread.sleep(150);
1618+
waitForInitialization(dataReader.getId());
15971619
testCI.notifyData();
1598-
while (!testCI.alreadyRead)
1620+
while (dataReader.isRunning())
15991621
Thread.sleep(30);
16001622

16011623
// Verify the result.
@@ -1626,9 +1648,9 @@ public InputStream getInputStream() {
16261648
// Call the method under test.
16271649
dataReader.start();
16281650

1629-
Thread.sleep(150);
1651+
waitForInitialization(dataReader.getId());
16301652
testCI.notifyData();
1631-
while (!testCI.alreadyRead)
1653+
while (dataReader.isRunning())
16321654
Thread.sleep(30);
16331655

16341656
// Verify the result.
@@ -1657,9 +1679,9 @@ public InputStream getInputStream() {
16571679
// Call the method under test.
16581680
dataReader.start();
16591681

1660-
Thread.sleep(150);
1682+
waitForInitialization(dataReader.getId());
16611683
testCI.notifyData();
1662-
while (!testCI.alreadyRead)
1684+
while (dataReader.isRunning())
16631685
Thread.sleep(30);
16641686

16651687
// Verify the result.

0 commit comments

Comments
 (0)