diff --git a/core/src/test/scala/unit/kafka/utils/TestUtils.scala b/core/src/test/scala/unit/kafka/utils/TestUtils.scala index 9616f89c467..46b8df0c6c1 100755 --- a/core/src/test/scala/unit/kafka/utils/TestUtils.scala +++ b/core/src/test/scala/unit/kafka/utils/TestUtils.scala @@ -1341,10 +1341,12 @@ object TestUtils extends Logging { // Note: Call this method in the test itself, rather than the @AfterEach method. // Because of the assert, if assertNoNonDaemonThreads fails, nothing after would be executed. def assertNoNonDaemonThreads(threadNamePrefix: String): Unit = { - val threadCount = Thread.getAllStackTraces.keySet.asScala.count { t => + val nonDaemonThreads = Thread.getAllStackTraces.keySet.asScala.filter { t => !t.isDaemon && t.isAlive && t.getName.startsWith(threadNamePrefix) } - assertEquals(0, threadCount) + + val threadCount = nonDaemonThreads.size + assertEquals(0, threadCount, s"Found unexpected $threadCount NonDaemon threads=${nonDaemonThreads.map(t => t.getName).mkString(", ")}") } def allThreadStackTraces(): String = {