forked from Kotlin/kotlinx.coroutines
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Dump pool threads on BroadcastChannelMultiReceiveStressTest failure;
Move thread test utils to different file
- Loading branch information
Showing
17 changed files
with
67 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
core/kotlinx-coroutines-core/src/test/kotlin/kotlinx/coroutines/experimental/Threads.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package kotlinx.coroutines.experimental | ||
|
||
private const val WAIT_LOST_THREADS = 10_000L // 10s | ||
private val ignoreLostThreads = mutableSetOf<String>() | ||
|
||
fun ignoreLostThreads(vararg s: String) { ignoreLostThreads += s } | ||
|
||
fun currentThreads(): Set<Thread> { | ||
var estimate = 0 | ||
while (true) { | ||
estimate = estimate.coerceAtLeast(Thread.activeCount() + 1) | ||
val arrayOfThreads = Array<Thread?>(estimate) { null } | ||
val n = Thread.enumerate(arrayOfThreads) | ||
if (n >= estimate) { | ||
estimate = n + 1 | ||
continue // retry with a better size estimate | ||
} | ||
val threads = hashSetOf<Thread>() | ||
for (i in 0 until n) | ||
threads.add(arrayOfThreads[i]!!) | ||
return threads | ||
} | ||
} | ||
|
||
fun List<Thread>.dumpThreads(header: String) { | ||
println("=== $header") | ||
forEach { thread -> | ||
println("Thread \"${thread.name}\" ${thread.state}") | ||
val trace = thread.stackTrace | ||
for (t in trace) println("\tat ${t.className}.${t.methodName}(${t.fileName}:${t.lineNumber})") | ||
println() | ||
} | ||
println("===") | ||
} | ||
|
||
fun ThreadPoolDispatcher.dumpThreads(header: String) = | ||
currentThreads().filter { it is PoolThread && it.dispatcher == this@dumpThreads }.dumpThreads(header) | ||
|
||
fun checkTestThreads(threadsBefore: Set<Thread>) { | ||
// give threads some time to shutdown | ||
val waitTill = System.currentTimeMillis() + WAIT_LOST_THREADS | ||
var diff: List<Thread> | ||
do { | ||
val threadsAfter = currentThreads() | ||
diff = (threadsAfter - threadsBefore).filter { thread -> | ||
ignoreLostThreads.none { prefix -> thread.name.startsWith(prefix) } | ||
} | ||
if (diff.isEmpty()) break | ||
} while (System.currentTimeMillis() <= waitTill) | ||
ignoreLostThreads.clear() | ||
if (diff.isEmpty()) return | ||
val message = "Lost threads ${diff.map { it.name }}" | ||
println("!!! $message") | ||
diff.dumpThreads("Dumping lost thread stack traces") | ||
error(message) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters