Skip to content

Instantly share code, notes, and snippets.

View mreichelt's full-sized avatar
👨‍💻
Coding is awesome 🎉

Marc Reichelt mreichelt

👨‍💻
Coding is awesome 🎉
View GitHub Profile
@mreichelt
mreichelt / RunBlockingTest.kt
Created March 31, 2022 11:58
Kotlin Coroutines 1.6: run blocking test, but cancel scope at exit without caring about endless-running jobs
fun TestCoroutineScope.runBlockingTest(
testBody: suspend TestCoroutineScope.() -> Unit,
) {
val regularEndOfTest = CancellationException()
try {
this.runBlockingTestKotlin { // import kotlinx.coroutines.test.runBlockingTest as runBlockingTestKotlin
testBody()
cancel(regularEndOfTest)
}
} catch (e: CancellationException) {