Skip to content

Add test for catch DeadLocks #2428

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 92 additions & 0 deletions kotlinx-coroutines-core/jvm/test/BuildersKtTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/
import kotlinx.coroutines.*
import org.junit.*
import org.junit.Test
import org.junit.rules.*
import org.junit.rules.Timeout.*
import org.junit.runner.*
import org.junit.runners.*
import java.util.concurrent.*
import kotlin.coroutines.*
import kotlin.test.*

@RunWith(Parameterized::class)
class BuildersKtTest(
private val disp1: CoroutineContext,
private val disp2: CoroutineContext,
private val disp3: CoroutineContext,
private val disp4: CoroutineContext
) {
companion object {
const val STEP: Long = 100

private val allParams = listOf(
EmptyCoroutineContext,
Executors.newSingleThreadExecutor().asCoroutineDispatcher(),
Executors.newSingleThreadScheduledExecutor().asCoroutineDispatcher(),
Dispatchers.Default,
Dispatchers.IO,
// Dispatchers.Main,
)

@ExperimentalStdlibApi
@JvmStatic
@Parameterized.Parameters
fun data(): List<*> = buildList {
allParams.forEach { i1 ->
allParams.forEach { i2 ->
allParams.forEach { i3 ->
allParams.forEach { i4 ->
add(arrayOf(i1, i2, i3, i4))
}
}
}
}
}
}

// Hard deadLock detect
@Rule
@JvmField
var globalTimeout: Timeout = millis(STEP * 5 * 2)

@Test
fun testTemplate(): Unit =
runBlocking(disp1) {
runCatching {
// Soft deadLock detect
withTimeout(STEP * 5) {
delay(STEP)
withContext(disp2) {
delay(STEP)
}
runBlocking(disp3) {
// Soft deadLock detect
runCatching {
withTimeout(STEP * 3) {
delay(STEP)
withContext(disp4) {
delay(STEP)
}
}
}.onFailure {
when (it) {
is TimeoutException, is TimeoutCancellationException -> fail(
"DEADLOCK on Level2",
it
)
else -> fail("other exception on Level2", it)
}
}.getOrThrow()
}
}
}.onFailure {
when (it) {
is TimeoutException, is TimeoutCancellationException -> fail("DEADLOCK on Level1", it)
else -> fail("other exception on Level2", it)
}
}.getOrThrow()
}
}