-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
BlockHoundTest.kt
114 lines (104 loc) · 2.76 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package kotlinx.coroutines.debug
import kotlinx.coroutines.*
import kotlinx.coroutines.channels.*
import org.junit.*
import reactor.blockhound.*
@Suppress("UnusedEquals", "DeferredResultUnused", "BlockingMethodInNonBlockingContext")
class BlockHoundTest : TestBase() {
@Before
fun init() {
BlockHound.install()
}
@Test(expected = BlockingOperationError::class)
fun testShouldDetectBlockingInDefault() = runTest {
withContext(Dispatchers.Default) {
Thread.sleep(1)
}
}
@Test
fun testShouldNotDetectBlockingInIO() = runTest {
withContext(Dispatchers.IO) {
Thread.sleep(1)
}
}
@Test
fun testShouldNotDetectNonblocking() = 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
fun testChannelNotBeingConsideredBlocking() = runTest {
withContext(Dispatchers.Default) {
// Copy of kotlinx.coroutines.channels.ArrayChannelTest.testSimple
val q = Channel<Int>(1)
check(q.isEmpty)
check(!q.isClosedForReceive)
check(!q.isClosedForSend)
val sender = launch {
q.send(1)
q.send(2)
}
val receiver = launch {
q.receive() == 1
q.receive() == 2
}
sender.join()
receiver.join()
}
}
@Test
fun testConflatedChannelsNotBeingConsideredBlocking() = runTest {
withContext(Dispatchers.Default) {
val q = Channel<Int>(Channel.CONFLATED)
check(q.isEmpty)
check(!q.isClosedForReceive)
check(!q.isClosedForSend)
val sender = launch {
q.send(1)
}
val receiver = launch {
q.receive() == 1
}
sender.join()
receiver.join()
}
}
@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)
}
}
}
}