Skip to content

Commit 8b9ecff

Browse files
committed
Make a better effort at getting all thread names (just in case)
1 parent 3f15673 commit 8b9ecff

File tree

1 file changed

+14
-6
lines changed
  • kotlinx-coroutines-core/src/test/kotlin/guide/test

1 file changed

+14
-6
lines changed

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

+14-6
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,20 @@ private val ignoreLostThreads = mutableSetOf<String>()
3232
fun ignoreLostThreads(vararg s: String) { ignoreLostThreads += s }
3333

3434
fun threadNames(): Set<String> {
35-
val arrayOfThreads = Array<Thread?>(Thread.activeCount()) { null }
36-
val n = Thread.enumerate(arrayOfThreads)
37-
val names = hashSetOf<String>()
38-
for (i in 0 until n)
39-
names.add(arrayOfThreads[i]!!.name)
40-
return names
35+
var estimate = 0
36+
while (true) {
37+
estimate = estimate.coerceAtLeast(Thread.activeCount() + 1)
38+
val arrayOfThreads = Array<Thread?>(estimate) { null }
39+
val n = Thread.enumerate(arrayOfThreads)
40+
if (n >= estimate) {
41+
estimate = n + 1
42+
continue // retry with a better size estimate
43+
}
44+
val names = hashSetOf<String>()
45+
for (i in 0 until n)
46+
names.add(arrayOfThreads[i]!!.name)
47+
return names
48+
}
4149
}
4250

4351
fun checkTestThreads(threadNamesBefore: Set<String>) {

0 commit comments

Comments
 (0)