From 7fca982a71897c1ef8c0ab6f7748f5377fd9a225 Mon Sep 17 00:00:00 2001 From: Kirill Tkalenko Date: Fri, 3 Apr 2026 10:48:56 +0300 Subject: [PATCH] IGNITE-28449 wip --- .../IgniteThreadGroupNodeRestartTest.java | 74 +++++++++++++++++-- 1 file changed, 68 insertions(+), 6 deletions(-) diff --git a/modules/core/src/test/java/org/apache/ignite/internal/IgniteThreadGroupNodeRestartTest.java b/modules/core/src/test/java/org/apache/ignite/internal/IgniteThreadGroupNodeRestartTest.java index a828b9a49b194..e2ad00f655671 100644 --- a/modules/core/src/test/java/org/apache/ignite/internal/IgniteThreadGroupNodeRestartTest.java +++ b/modules/core/src/test/java/org/apache/ignite/internal/IgniteThreadGroupNodeRestartTest.java @@ -17,14 +17,33 @@ package org.apache.ignite.internal; +import java.util.Arrays; +import java.util.Objects; import java.util.concurrent.atomic.AtomicReference; +import org.apache.ignite.testframework.GridTestUtils; import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest; import org.junit.Test; +import static java.util.stream.Collectors.joining; + /** * */ public class IgniteThreadGroupNodeRestartTest extends GridCommonAbstractTest { + /** {@inheritDoc} */ + @Override protected void beforeTest() throws Exception { + super.beforeTest(); + + stopAllGrids(); + } + + /** {@inheritDoc} */ + @Override protected void afterTest() throws Exception { + super.afterTest(); + + stopAllGrids(); + } + /** * @throws Exception if failed. */ @@ -36,9 +55,7 @@ public void testNodeRestartInsideThreadGroup() throws Exception { Thread t = new Thread(tg, () -> { try { - startGrid(0); - - stopGrid(0); + startStopGrid(0); } catch (Exception e) { err.set(e); @@ -47,13 +64,58 @@ public void testNodeRestartInsideThreadGroup() throws Exception { t.start(); t.join(getTestTimeout()); + assertFalse(t.isAlive()); if (err.get() != null) throw err.get(); - tg.destroy(); + destroyWithWaitAllThreadIsComplete(tg, getTestTimeout()); + + startStopGrid(0); + } + + /** */ + private void startStopGrid(int idx) throws Exception { + startGrid(idx); + stopGrid(idx); + } + + /** */ + private static void destroyWithWaitAllThreadIsComplete(ThreadGroup tg, long millis) throws Exception { + if (GridTestUtils.waitForCondition(() -> tg.activeCount() == 0, millis, 10) || tg.activeCount() == 0) { + tg.destroy(); + + return; + } + + Thread[] threads = new Thread[tg.activeCount() + 256]; + int copied = tg.enumerate(threads, true); + + if (copied == 0) { + tg.destroy(); + + return; + } + + fail(String.format( + "Thread group still has active threads: [count=%s, threads=%s]", + copied, threadInfos(threads) + )); + } + + /** */ + private static String threadInfos(Thread... threads) { + return Arrays.stream(threads) + .filter(Objects::nonNull) + .map(IgniteThreadGroupNodeRestartTest::threadInfo) + .collect(joining(", ", "[", "]")); + } - startGrid(0); - stopGrid(0); + /** */ + private static String threadInfo(Thread t) { + return String.format( + "[id=%s, name=%s, alive=%s, deamon=%s, state=%s]", + t.getId(), t.getName(), t.isAlive(), t.isDaemon(), t.getState() + ); } }