Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a25bf36
commit edc388c
Showing
5 changed files
with
130 additions
and
0 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
43 changes: 43 additions & 0 deletions
43
kotlinx-coroutines-debug/src/internal/BlockHoundIntegration.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,43 @@ | ||
package kotlinx.coroutines.debug.internal | ||
|
||
import reactor.blockhound.BlockHound | ||
import kotlin.reflect.KClass | ||
import kotlin.reflect.full.* | ||
|
||
internal object BlockHoundIntegration { | ||
|
||
init { | ||
val cls = Class.forName("kotlinx.coroutines.scheduling.CoroutineScheduler\$Worker").kotlin | ||
initializerHelper(cls) | ||
} | ||
|
||
private fun <T : Any> initializerHelper(cls: KClass<T>) { | ||
val field = cls.declaredMemberProperties.find { it.name == "state" }!! | ||
BlockHound.builder() | ||
.addDynamicThreadPredicate(cls::isInstance) | ||
.nonBlockingThreadPredicate { p -> | ||
p.or { thread -> | ||
val castThread = cls.safeCast(thread) | ||
if (!enabled || castThread == null) { | ||
false | ||
} else { | ||
val state = field(castThread) as Enum<*> | ||
state.name == "CPU_ACQUIRED" | ||
} | ||
} | ||
} | ||
.install() | ||
} | ||
|
||
@Volatile | ||
private var enabled = false | ||
|
||
fun install() { | ||
enabled = true | ||
} | ||
|
||
fun uninstall() { | ||
enabled = false | ||
} | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package kotlinx.coroutines.debug | ||
import kotlinx.coroutines.* | ||
import kotlinx.coroutines.debug.internal.BlockHoundIntegration | ||
import org.junit.* | ||
import reactor.blockhound.BlockingOperationError | ||
|
||
class BlockHoundTest : TestBase() { | ||
|
||
@Before | ||
fun init() { | ||
BlockHoundIntegration.install() | ||
} | ||
|
||
@After | ||
fun deinit() { | ||
BlockHoundIntegration.uninstall() | ||
} | ||
|
||
@Test(expected = BlockingOperationError::class) | ||
fun shouldDetectBlockingInDefault() = runTest { | ||
withContext(Dispatchers.Default) { | ||
Thread.sleep(1) | ||
} | ||
} | ||
|
||
@Test | ||
fun shouldNotDetectBlockingInIO() = runTest { | ||
withContext(Dispatchers.IO) { | ||
Thread.sleep(1) | ||
} | ||
} | ||
|
||
@Test | ||
fun shouldNotDetectNonblocking() = runTest { | ||
withContext(Dispatchers.Default) { | ||
val a = 1 | ||
val b = 2 | ||
assert(a + b == 3) | ||
} | ||
} | ||
|
||
@Test | ||
fun testReusingThreads() = runTest { | ||
val n = 100 | ||
repeat(n) { | ||
async(Dispatchers.IO) { | ||
Thread.sleep(1) | ||
} | ||
} | ||
repeat(n) { | ||
async(Dispatchers.Default) { | ||
} | ||
} | ||
repeat(n) { | ||
async(Dispatchers.IO) { | ||
Thread.sleep(1) | ||
} | ||
} | ||
} | ||
|
||
@Test(expected = BlockingOperationError::class) | ||
fun testReusingThreadsFailure() = runTest { | ||
val n = 100 | ||
repeat(n) { | ||
async(Dispatchers.IO) { | ||
Thread.sleep(1) | ||
} | ||
} | ||
async(Dispatchers.Default) { | ||
Thread.sleep(1) | ||
} | ||
repeat(n) { | ||
async(Dispatchers.IO) { | ||
Thread.sleep(1) | ||
} | ||
} | ||
} | ||
|
||
} |