Skip to content

Commit f96adc8

Browse files
authored
IGNITE-28449 Fix flaky IgniteThreadGroupNodeRestartTest#testNodeRestartInsideThreadGroup (#12977)
https://issues.apache.org/jira/browse/IGNITE-28449
1 parent 341bb1c commit f96adc8

1 file changed

Lines changed: 68 additions & 6 deletions

File tree

modules/core/src/test/java/org/apache/ignite/internal/IgniteThreadGroupNodeRestartTest.java

Lines changed: 68 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,33 @@
1717

1818
package org.apache.ignite.internal;
1919

20+
import java.util.Arrays;
21+
import java.util.Objects;
2022
import java.util.concurrent.atomic.AtomicReference;
23+
import org.apache.ignite.testframework.GridTestUtils;
2124
import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest;
2225
import org.junit.Test;
2326

27+
import static java.util.stream.Collectors.joining;
28+
2429
/**
2530
*
2631
*/
2732
public class IgniteThreadGroupNodeRestartTest extends GridCommonAbstractTest {
33+
/** {@inheritDoc} */
34+
@Override protected void beforeTest() throws Exception {
35+
super.beforeTest();
36+
37+
stopAllGrids();
38+
}
39+
40+
/** {@inheritDoc} */
41+
@Override protected void afterTest() throws Exception {
42+
super.afterTest();
43+
44+
stopAllGrids();
45+
}
46+
2847
/**
2948
* @throws Exception if failed.
3049
*/
@@ -36,9 +55,7 @@ public void testNodeRestartInsideThreadGroup() throws Exception {
3655

3756
Thread t = new Thread(tg, () -> {
3857
try {
39-
startGrid(0);
40-
41-
stopGrid(0);
58+
startStopGrid(0);
4259
}
4360
catch (Exception e) {
4461
err.set(e);
@@ -47,13 +64,58 @@ public void testNodeRestartInsideThreadGroup() throws Exception {
4764

4865
t.start();
4966
t.join(getTestTimeout());
67+
assertFalse(t.isAlive());
5068

5169
if (err.get() != null)
5270
throw err.get();
5371

54-
tg.destroy();
72+
destroyWithWaitAllThreadIsComplete(tg, getTestTimeout());
73+
74+
startStopGrid(0);
75+
}
76+
77+
/** */
78+
private void startStopGrid(int idx) throws Exception {
79+
startGrid(idx);
80+
stopGrid(idx);
81+
}
82+
83+
/** */
84+
private static void destroyWithWaitAllThreadIsComplete(ThreadGroup tg, long millis) throws Exception {
85+
if (GridTestUtils.waitForCondition(() -> tg.activeCount() == 0, millis, 10) || tg.activeCount() == 0) {
86+
tg.destroy();
87+
88+
return;
89+
}
90+
91+
Thread[] threads = new Thread[tg.activeCount() + 256];
92+
int copied = tg.enumerate(threads, true);
93+
94+
if (copied == 0) {
95+
tg.destroy();
96+
97+
return;
98+
}
99+
100+
fail(String.format(
101+
"Thread group still has active threads: [count=%s, threads=%s]",
102+
copied, threadInfos(threads)
103+
));
104+
}
105+
106+
/** */
107+
private static String threadInfos(Thread... threads) {
108+
return Arrays.stream(threads)
109+
.filter(Objects::nonNull)
110+
.map(IgniteThreadGroupNodeRestartTest::threadInfo)
111+
.collect(joining(", ", "[", "]"));
112+
}
55113

56-
startGrid(0);
57-
stopGrid(0);
114+
/** */
115+
private static String threadInfo(Thread t) {
116+
return String.format(
117+
"[id=%s, name=%s, alive=%s, deamon=%s, state=%s]",
118+
t.getId(), t.getName(), t.isAlive(), t.isDaemon(), t.getState()
119+
);
58120
}
59121
}

0 commit comments

Comments
 (0)