blob: 2d8c0ebc758e1161f839f67d72d69b12c47fccf0 (
plain)
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
|
/*
* Copyright 2016-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/
package kotlinx.coroutines.exceptions
import kotlinx.coroutines.*
import kotlinx.coroutines.channels.*
import org.junit.*
import org.junit.rules.*
import kotlin.coroutines.*
class StackTraceRecoveryChannelsTest : TestBase() {
@get:Rule
val name = TestName()
@Test
fun testReceiveFromChannel() = runTest {
val channel = Channel<Int>()
val job = launch {
expect(2)
channel.close(RecoverableTestException())
}
expect(1)
channelReceive(channel)
expect(3)
job.join()
finish(4)
}
@Test
fun testReceiveFromClosedChannel() = runTest {
val channel = Channel<Int>()
channel.close(RecoverableTestException())
channelReceive(channel)
}
@Test
fun testSendToClosedChannel() = runTest {
val channel = Channel<Int>()
channel.close(RecoverableTestException())
channelSend(channel)
}
@Test
fun testSendToChannel() = runTest {
val channel = Channel<Int>()
val job = launch {
expect(2)
channel.cancel()
}
expect(1)
channelSend(channel)
expect(3)
job.join()
finish(4)
}
private suspend fun channelReceive(channel: Channel<Int>) = channelOp { channel.receive() }
private suspend inline fun channelOp(block: () -> Unit) {
try {
yield()
block()
expectUnreached()
} catch (e: RecoverableTestException) {
verifyStackTrace("channels/${name.methodName}", e)
}
}
private suspend fun channelSend(channel: Channel<Int>) {
try {
yield()
channel.send(1)
expectUnreached()
} catch (e: Exception) {
verifyStackTrace("channels/${name.methodName}", e)
}
}
@Test
fun testOfferWithCurrentContext() = runTest {
val channel = Channel<Int>()
channel.close(RecoverableTestException())
try {
channel.sendWithContext(coroutineContext)
} catch (e: RecoverableTestException) {
verifyStackTrace("channels/${name.methodName}", e)
}
}
@Test
fun testOfferWithContextWrapped() = runTest {
val channel = Channel<Int>()
channel.close(RecoverableTestException())
try {
channel.sendWithContext(wrapperDispatcher(coroutineContext))
} catch (e: Exception) {
verifyStackTrace("channels/${name.methodName}", e)
}
}
@Test
fun testOfferFromScope() = runTest {
val channel = Channel<Int>()
channel.close(RecoverableTestException())
try {
channel.sendFromScope()
} catch (e: Exception) {
verifyStackTrace("channels/${name.methodName}", e)
}
}
// Slow path via suspending send
@Test
fun testSendFromScope() = runTest {
val channel = Channel<Int>()
val deferred = async {
try {
expect(1)
channel.sendFromScope()
} catch (e: Exception) {
verifyStackTrace("channels/${name.methodName}", e)
}
}
yield()
expect(2)
// Cancel is an analogue of `produce` failure, just a shorthand
channel.cancel(RecoverableTestCancellationException())
finish(3)
deferred.await()
}
private suspend fun Channel<Int>.sendWithContext(ctx: CoroutineContext) = withContext(ctx) {
sendInChannel()
yield() // TCE
}
private suspend fun Channel<Int>.sendInChannel() {
send(42)
yield() // TCE
}
private suspend fun Channel<Int>.sendFromScope() = coroutineScope {
sendWithContext(wrapperDispatcher(coroutineContext))
}
}
|