|
74 | 74 | import java.util.function.Supplier;
|
75 | 75 | import java.util.regex.Matcher;
|
76 | 76 | import java.util.regex.Pattern;
|
77 |
| -import java.util.stream.Collectors; |
78 | 77 |
|
79 | 78 | import static java.util.Arrays.asList;
|
80 | 79 | import static org.junit.jupiter.api.Assertions.assertEquals;
|
@@ -161,22 +160,21 @@ public static MetadataSnapshot metadataSnapshotWith(final int nodes, final Map<S
|
161 | 160 | * Asserts that there are no leaked threads with a specified name prefix and daemon status.
|
162 | 161 | * This method checks all threads in the JVM, filters them by the provided thread name prefix
|
163 | 162 | * and daemon status, and verifies that no matching threads are alive.
|
164 |
| - * If any matching threads are found, the test will fail. |
| 163 | + * Use the {@link #waitForCondition(TestCondition, String) waitForCondition} to retry the check at a regular interval |
| 164 | + * until either no matching threads are found or the timeout is exceeded. |
| 165 | + * If any matching, alive threads are found after the timeout has elapsed, the assertion will fail. |
165 | 166 | *
|
166 | 167 | * @param threadName The prefix of the thread names to check. Only threads whose names
|
167 | 168 | * start with this prefix will be considered.
|
168 | 169 | * @param isDaemon The daemon status to check. Only threads with the specified
|
169 | 170 | * daemon status (either true for daemon threads or false for non-daemon threads)
|
170 | 171 | * will be considered.
|
171 | 172 | *
|
172 |
| - * @throws AssertionError If any thread with the specified name prefix and daemon status is found and is alive. |
| 173 | + * @throws AssertionError If any thread with the specified name prefix and daemon status are found after the timeout. |
173 | 174 | */
|
174 |
| - public static void assertNoLeakedThreadsWithNameAndDaemonStatus(String threadName, boolean isDaemon) { |
175 |
| - List<Thread> threads = Thread.getAllStackTraces().keySet().stream() |
176 |
| - .filter(t -> t.isDaemon() == isDaemon && t.isAlive() && t.getName().startsWith(threadName)) |
177 |
| - .collect(Collectors.toList()); |
178 |
| - int threadCount = threads.size(); |
179 |
| - assertEquals(0, threadCount); |
| 175 | + public static void assertNoLeakedThreadsWithNameAndDaemonStatus(String threadName, boolean isDaemon) throws InterruptedException { |
| 176 | + waitForCondition(() -> Thread.getAllStackTraces().keySet().stream() |
| 177 | + .noneMatch(t -> t.isDaemon() == isDaemon && t.isAlive() && t.getName().startsWith(threadName)), String.format("Thread leak detected: %s", threadName)); |
180 | 178 | }
|
181 | 179 |
|
182 | 180 | /**
|
|
0 commit comments