|
18 | 18 | import java.io.IOException; |
19 | 19 | import java.io.InputStream; |
20 | 20 | 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; |
21 | 25 | import java.util.ArrayList; |
22 | 26 | import java.util.HashMap; |
23 | 27 |
|
@@ -243,6 +247,24 @@ public Object answer(InvocationOnMock invocation) throws Throwable { |
243 | 247 | @After |
244 | 248 | public void tearDown() throws Exception { |
245 | 249 | } |
| 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 | + } |
246 | 268 |
|
247 | 269 | /** |
248 | 270 | * 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 { |
935 | 957 | // Call the method under test. |
936 | 958 | dataReader.start(); |
937 | 959 |
|
938 | | - Thread.sleep(150); |
| 960 | + waitForInitialization(dataReader.getId()); |
939 | 961 | testCI.notifyData(); |
940 | | - while (!testCI.alreadyRead) |
| 962 | + while (dataReader.isRunning()) |
941 | 963 | Thread.sleep(30); |
942 | 964 |
|
943 | 965 | // Verify the result. |
@@ -970,9 +992,9 @@ public XBeePacket answer(InvocationOnMock invocation) throws Throwable { |
970 | 992 | // Call the method under test. |
971 | 993 | dataReader.start(); |
972 | 994 |
|
973 | | - Thread.sleep(150); |
| 995 | + waitForInitialization(dataReader.getId()); |
974 | 996 | testCI.notifyData(); |
975 | | - while (!testCI.alreadyRead) |
| 997 | + while (dataReader.isRunning()) |
976 | 998 | Thread.sleep(30); |
977 | 999 |
|
978 | 1000 | // Verify the result. |
@@ -1008,9 +1030,9 @@ public final void testDataReaderReceivePacketNotContemplatedPacket() throws Exce |
1008 | 1030 | // Call the method under test. |
1009 | 1031 | dataReader.start(); |
1010 | 1032 |
|
1011 | | - Thread.sleep(150); |
| 1033 | + waitForInitialization(dataReader.getId()); |
1012 | 1034 | testCI.notifyData(); |
1013 | | - while (!testCI.alreadyRead) |
| 1035 | + while (dataReader.isRunning()) |
1014 | 1036 | Thread.sleep(30); |
1015 | 1037 |
|
1016 | 1038 | // Verify the result. |
@@ -1050,9 +1072,9 @@ public final void testDataReaderReceivePacketListentoFrameIDNotReceived() throws |
1050 | 1072 | // Call the method under test. |
1051 | 1073 | dataReader.start(); |
1052 | 1074 |
|
1053 | | - Thread.sleep(150); |
| 1075 | + waitForInitialization(dataReader.getId()); |
1054 | 1076 | testCI.notifyData(); |
1055 | | - while (!testCI.alreadyRead) |
| 1077 | + while (dataReader.isRunning()) |
1056 | 1078 | Thread.sleep(30); |
1057 | 1079 |
|
1058 | 1080 | // Verify the result. |
@@ -1092,9 +1114,9 @@ public final void testDataReaderReceivePacketListentoFrameIDReceived() throws Ex |
1092 | 1114 | // Call the method under test. |
1093 | 1115 | dataReader.start(); |
1094 | 1116 |
|
1095 | | - Thread.sleep(150); |
| 1117 | + waitForInitialization(dataReader.getId()); |
1096 | 1118 | testCI.notifyData(); |
1097 | | - while (!testCI.alreadyRead) |
| 1119 | + while (dataReader.isRunning()) |
1098 | 1120 | Thread.sleep(30); |
1099 | 1121 |
|
1100 | 1122 | // Verify the result. |
@@ -1134,9 +1156,9 @@ public final void testDataReaderReceivePacketATResponse() throws Exception { |
1134 | 1156 | // Call the method under test. |
1135 | 1157 | dataReader.start(); |
1136 | 1158 |
|
1137 | | - Thread.sleep(150); |
| 1159 | + waitForInitialization(dataReader.getId()); |
1138 | 1160 | testCI.notifyData(); |
1139 | | - while (!testCI.alreadyRead) |
| 1161 | + while (dataReader.isRunning()) |
1140 | 1162 | Thread.sleep(30); |
1141 | 1163 |
|
1142 | 1164 | // Verify the result. |
@@ -1176,9 +1198,9 @@ public final void testDataReaderReceivePacketRemoteATResponse() throws Exception |
1176 | 1198 | // Call the method under test. |
1177 | 1199 | dataReader.start(); |
1178 | 1200 |
|
1179 | | - Thread.sleep(150); |
| 1201 | + waitForInitialization(dataReader.getId()); |
1180 | 1202 | testCI.notifyData(); |
1181 | | - while (!testCI.alreadyRead) |
| 1203 | + while (dataReader.isRunning()) |
1182 | 1204 | Thread.sleep(30); |
1183 | 1205 |
|
1184 | 1206 | // Verify the result. |
@@ -1218,9 +1240,9 @@ public final void testDataReaderReceivePacketRX64DataPacket() throws Exception { |
1218 | 1240 | // Call the method under test. |
1219 | 1241 | dataReader.start(); |
1220 | 1242 |
|
1221 | | - Thread.sleep(150); |
| 1243 | + waitForInitialization(dataReader.getId()); |
1222 | 1244 | testCI.notifyData(); |
1223 | | - while (!testCI.alreadyRead) |
| 1245 | + while (dataReader.isRunning()) |
1224 | 1246 | Thread.sleep(30); |
1225 | 1247 |
|
1226 | 1248 | // Verify the result. |
@@ -1260,9 +1282,9 @@ public final void testDataReaderReceivePacketRX16DataPacket() throws Exception { |
1260 | 1282 | // Call the method under test. |
1261 | 1283 | dataReader.start(); |
1262 | 1284 |
|
1263 | | - Thread.sleep(150); |
| 1285 | + waitForInitialization(dataReader.getId()); |
1264 | 1286 | testCI.notifyData(); |
1265 | | - while (!testCI.alreadyRead) |
| 1287 | + while (dataReader.isRunning()) |
1266 | 1288 | Thread.sleep(30); |
1267 | 1289 |
|
1268 | 1290 | // Verify the result. |
@@ -1302,9 +1324,9 @@ public final void testDataReaderReceivePacketRXDataPacket() throws Exception { |
1302 | 1324 | // Call the method under test. |
1303 | 1325 | dataReader.start(); |
1304 | 1326 |
|
1305 | | - Thread.sleep(150); |
| 1327 | + waitForInitialization(dataReader.getId()); |
1306 | 1328 | testCI.notifyData(); |
1307 | | - while (!testCI.alreadyRead) |
| 1329 | + while (dataReader.isRunning()) |
1308 | 1330 | Thread.sleep(30); |
1309 | 1331 |
|
1310 | 1332 | // Verify the result. |
@@ -1344,9 +1366,9 @@ public final void testDataReaderReceivePacketRXIO64DataPacket() throws Exception |
1344 | 1366 | // Call the method under test. |
1345 | 1367 | dataReader.start(); |
1346 | 1368 |
|
1347 | | - Thread.sleep(150); |
| 1369 | + waitForInitialization(dataReader.getId()); |
1348 | 1370 | testCI.notifyData(); |
1349 | | - while (!testCI.alreadyRead) |
| 1371 | + while (dataReader.isRunning()) |
1350 | 1372 | Thread.sleep(30); |
1351 | 1373 |
|
1352 | 1374 | // Verify the result. |
@@ -1386,9 +1408,9 @@ public final void testDataReaderReceivePacketRXIO16DataPacket() throws Exception |
1386 | 1408 | // Call the method under test. |
1387 | 1409 | dataReader.start(); |
1388 | 1410 |
|
1389 | | - Thread.sleep(150); |
| 1411 | + waitForInitialization(dataReader.getId()); |
1390 | 1412 | testCI.notifyData(); |
1391 | | - while (!testCI.alreadyRead) |
| 1413 | + while (dataReader.isRunning()) |
1392 | 1414 | Thread.sleep(30); |
1393 | 1415 |
|
1394 | 1416 | // Verify the result. |
@@ -1428,9 +1450,9 @@ public final void testDataReaderReceivePacketRXIOPacket() throws Exception { |
1428 | 1450 | // Call the method under test. |
1429 | 1451 | dataReader.start(); |
1430 | 1452 |
|
1431 | | - Thread.sleep(150); |
| 1453 | + waitForInitialization(dataReader.getId()); |
1432 | 1454 | testCI.notifyData(); |
1433 | | - while (!testCI.alreadyRead) |
| 1455 | + while (dataReader.isRunning()) |
1434 | 1456 | Thread.sleep(30); |
1435 | 1457 |
|
1436 | 1458 | // Verify the result. |
@@ -1470,9 +1492,9 @@ public final void testDataReaderReceivePacketModemStatusPacket() throws Exceptio |
1470 | 1492 | // Call the method under test. |
1471 | 1493 | dataReader.start(); |
1472 | 1494 |
|
1473 | | - Thread.sleep(150); |
| 1495 | + waitForInitialization(dataReader.getId()); |
1474 | 1496 | testCI.notifyData(); |
1475 | | - while (!testCI.alreadyRead) |
| 1497 | + while (dataReader.isRunning()) |
1476 | 1498 | Thread.sleep(30); |
1477 | 1499 |
|
1478 | 1500 | // Verify the result. |
@@ -1512,9 +1534,9 @@ public final void testDataReaderReceivePacketExplicitIndicatorDigiPacket() throw |
1512 | 1534 | // Call the method under test. |
1513 | 1535 | dataReader.start(); |
1514 | 1536 |
|
1515 | | - Thread.sleep(150); |
| 1537 | + waitForInitialization(dataReader.getId()); |
1516 | 1538 | testCI.notifyData(); |
1517 | | - while (!testCI.alreadyRead) |
| 1539 | + while (dataReader.isRunning()) |
1518 | 1540 | Thread.sleep(30); |
1519 | 1541 |
|
1520 | 1542 | // Verify the result. |
@@ -1554,9 +1576,9 @@ public final void testDataReaderReceivePacketExplicitIndicatorPacket() throws Ex |
1554 | 1576 | // Call the method under test. |
1555 | 1577 | dataReader.start(); |
1556 | 1578 |
|
1557 | | - Thread.sleep(150); |
| 1579 | + waitForInitialization(dataReader.getId()); |
1558 | 1580 | testCI.notifyData(); |
1559 | | - while (!testCI.alreadyRead) |
| 1581 | + while (dataReader.isRunning()) |
1560 | 1582 | Thread.sleep(30); |
1561 | 1583 |
|
1562 | 1584 | // Verify the result. |
@@ -1593,9 +1615,9 @@ public XBeePacket answer(InvocationOnMock invocation) throws Throwable { |
1593 | 1615 | // Call the method under test. |
1594 | 1616 | dataReader.start(); |
1595 | 1617 |
|
1596 | | - Thread.sleep(150); |
| 1618 | + waitForInitialization(dataReader.getId()); |
1597 | 1619 | testCI.notifyData(); |
1598 | | - while (!testCI.alreadyRead) |
| 1620 | + while (dataReader.isRunning()) |
1599 | 1621 | Thread.sleep(30); |
1600 | 1622 |
|
1601 | 1623 | // Verify the result. |
@@ -1626,9 +1648,9 @@ public InputStream getInputStream() { |
1626 | 1648 | // Call the method under test. |
1627 | 1649 | dataReader.start(); |
1628 | 1650 |
|
1629 | | - Thread.sleep(150); |
| 1651 | + waitForInitialization(dataReader.getId()); |
1630 | 1652 | testCI.notifyData(); |
1631 | | - while (!testCI.alreadyRead) |
| 1653 | + while (dataReader.isRunning()) |
1632 | 1654 | Thread.sleep(30); |
1633 | 1655 |
|
1634 | 1656 | // Verify the result. |
@@ -1657,9 +1679,9 @@ public InputStream getInputStream() { |
1657 | 1679 | // Call the method under test. |
1658 | 1680 | dataReader.start(); |
1659 | 1681 |
|
1660 | | - Thread.sleep(150); |
| 1682 | + waitForInitialization(dataReader.getId()); |
1661 | 1683 | testCI.notifyData(); |
1662 | | - while (!testCI.alreadyRead) |
| 1684 | + while (dataReader.isRunning()) |
1663 | 1685 | Thread.sleep(30); |
1664 | 1686 |
|
1665 | 1687 | // Verify the result. |
|
0 commit comments