forked from kotest/kotest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BlockHoundTest.kt
74 lines (62 loc) · 2.43 KB
/
BlockHoundTest.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package io.kotest.extensions.blockhound
import io.kotest.assertions.throwables.shouldNotThrow
import io.kotest.assertions.throwables.shouldThrow
import io.kotest.core.spec.style.FunSpec
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import reactor.blockhound.BlockingOperationError
private suspend fun blockInNonBlockingContext() {
// Provokes a blocking situation which will be detected if
// a) BlockHound has been successfully activated, and
// b) the required 'kotlinx-coroutines-debug' dependency is present.
withContext(Dispatchers.Default) { // Use a non-blocking dispatcher
@Suppress("BlockingMethodInNonBlockingContext")
Thread.sleep(2)
}
}
class BlockHoundCaseTest : FunSpec({
test("detects for test case").config(extensions = listOf(BlockHound())) {
shouldThrow<BlockingOperationError> { blockInNonBlockingContext() }
}
test("individually disabled").config(extensions = listOf(BlockHound())) {
shouldThrow<BlockingOperationError> { blockInNonBlockingContext() }
withBlockHoundMode(BlockHoundMode.DISABLED) {
shouldNotThrow<BlockingOperationError> { blockInNonBlockingContext() }
}
}
test("prints for test case").config(extensions = listOf(BlockHound(BlockHoundMode.PRINT))) {
shouldNotThrow<BlockingOperationError> { blockInNonBlockingContext() }
}
test("not enabled for test case") {
shouldNotThrow<BlockingOperationError> { blockInNonBlockingContext() }
}
})
class BlockHoundSpecTest : FunSpec({
extension(BlockHound())
test("detects for spec") {
shouldThrow<BlockingOperationError> { blockInNonBlockingContext() }
}
test("does not complain for I/O threads") {
shouldNotThrow<BlockingOperationError> {
withContext(Dispatchers.IO) { // Use a blocking dispatcher
Thread.sleep(2)
}
}
}
context("nested test") {
test("child test") {
shouldThrow<BlockingOperationError> { blockInNonBlockingContext() }
}
}
test("nested configuration").config(extensions = listOf(BlockHound(BlockHoundMode.DISABLED))) {
shouldNotThrow<BlockingOperationError> { blockInNonBlockingContext() }
}
test("parallelism").config(invocations = 2, threads = 2) {
shouldThrow<BlockingOperationError> {
withContext(Dispatchers.Default) {
@Suppress("BlockingMethodInNonBlockingContext")
Thread.sleep(2)
}
}
}
})