Skip to content

Commit 1e0ed74

Browse files
committed
Sanitize thread names in tests to avoid intra-test interference
(coroutine name is removed from thread name)
1 parent 7015ad2 commit 1e0ed74

File tree

1 file changed

+7
-1
lines changed
  • kotlinx-coroutines-core/src/test/kotlin/guide/test

1 file changed

+7
-1
lines changed

kotlinx-coroutines-core/src/test/kotlin/guide/test/TestUtil.kt

+7-1
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,17 @@ fun threadNames(): Set<String> {
4343
}
4444
val names = hashSetOf<String>()
4545
for (i in 0 until n)
46-
names.add(arrayOfThreads[i]!!.name)
46+
names.add(sanitizeThreadName(arrayOfThreads[i]!!.name))
4747
return names
4848
}
4949
}
5050

51+
// remove coroutine names from thread in case we have lost threads with coroutines running in them
52+
private fun sanitizeThreadName(name: String): String {
53+
val i = name.indexOf(" @")
54+
return if (i < 0) name else name.substring(0, i)
55+
}
56+
5157
fun checkTestThreads(threadNamesBefore: Set<String>) {
5258
// give threads some time to shutdown
5359
val waitTill = System.currentTimeMillis() + 1000L

0 commit comments

Comments
 (0)