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 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
|
/*
* Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/
package kotlinx.coroutines.channels
import kotlinx.coroutines.*
import kotlin.test.*
class RendezvousChannelTest : TestBase() {
@Test
fun testSimple() = runTest {
val q = Channel<Int>(Channel.RENDEZVOUS)
check(q.isEmpty && q.isFull)
expect(1)
val sender = launch {
expect(4)
q.send(1) // suspend -- the first to come to rendezvous
expect(7)
q.send(2) // does not suspend -- receiver is there
expect(8)
}
expect(2)
val receiver = launch {
expect(5)
check(q.receive() == 1) // does not suspend -- sender was there
expect(6)
check(q.receive() == 2) // suspends
expect(9)
}
expect(3)
sender.join()
receiver.join()
check(q.isEmpty && q.isFull)
finish(10)
}
@Test
fun testClosedReceiveOrNull() = runTest {
val q = Channel<Int>(Channel.RENDEZVOUS)
check(q.isEmpty && q.isFull && !q.isClosedForSend && !q.isClosedForReceive)
expect(1)
launch {
expect(3)
assertEquals(42, q.receiveOrNull())
expect(4)
assertEquals(null, q.receiveOrNull())
expect(6)
}
expect(2)
q.send(42)
expect(5)
q.close()
check(!q.isEmpty && !q.isFull && q.isClosedForSend && q.isClosedForReceive)
yield()
check(!q.isEmpty && !q.isFull && q.isClosedForSend && q.isClosedForReceive)
finish(7)
}
@Test
fun testClosedExceptions() = runTest {
val q = Channel<Int>(Channel.RENDEZVOUS)
expect(1)
launch {
expect(4)
try { q.receive() }
catch (e: ClosedReceiveChannelException) {
expect(5)
}
}
expect(2)
q.close()
expect(3)
yield()
expect(6)
try { q.send(42) }
catch (e: ClosedSendChannelException) {
finish(7)
}
}
@Test
fun testOfferAndPool() = runTest {
val q = Channel<Int>(Channel.RENDEZVOUS)
assertFalse(q.offer(1))
expect(1)
launch {
expect(3)
assertEquals(null, q.poll())
expect(4)
assertEquals(2, q.receive())
expect(7)
assertEquals(null, q.poll())
yield()
expect(9)
assertEquals(3, q.poll())
expect(10)
}
expect(2)
yield()
expect(5)
assertTrue(q.offer(2))
expect(6)
yield()
expect(8)
q.send(3)
finish(11)
}
@Test
fun testIteratorClosed() = runTest {
val q = Channel<Int>(Channel.RENDEZVOUS)
expect(1)
launch {
expect(3)
q.close()
expect(4)
}
expect(2)
for (x in q) {
expectUnreached()
}
finish(5)
}
@Test
fun testIteratorOne() = runTest {
val q = Channel<Int>(Channel.RENDEZVOUS)
expect(1)
launch {
expect(3)
q.send(1)
expect(4)
q.close()
expect(5)
}
expect(2)
for (x in q) {
expect(6)
assertEquals(1, x)
}
finish(7)
}
@Test
fun testIteratorOneWithYield() = runTest {
val q = Channel<Int>(Channel.RENDEZVOUS)
expect(1)
launch {
expect(3)
q.send(1) // will suspend
expect(6)
q.close()
expect(7)
}
expect(2)
yield() // yield to sender coroutine right before starting for loop
expect(4)
for (x in q) {
expect(5)
assertEquals(1, x)
}
finish(8)
}
@Test
fun testIteratorTwo() = runTest {
val q = Channel<Int>(Channel.RENDEZVOUS)
expect(1)
launch {
expect(3)
q.send(1)
expect(4)
q.send(2)
expect(7)
q.close()
expect(8)
}
expect(2)
for (x in q) {
when (x) {
1 -> expect(5)
2 -> expect(6)
else -> expectUnreached()
}
}
finish(9)
}
@Test
fun testIteratorTwoWithYield() = runTest {
val q = Channel<Int>(Channel.RENDEZVOUS)
expect(1)
launch {
expect(3)
q.send(1) // will suspend
expect(6)
q.send(2)
expect(7)
q.close()
expect(8)
}
expect(2)
yield() // yield to sender coroutine right before starting for loop
expect(4)
for (x in q) {
when (x) {
1 -> expect(5)
2 -> expect(9)
else -> expectUnreached()
}
}
finish(10)
}
@Test
fun testSuspendSendOnClosedChannel() = runTest {
val q = Channel<Int>(Channel.RENDEZVOUS)
expect(1)
launch {
expect(4)
q.send(42) // suspend
expect(11)
}
expect(2)
launch {
expect(5)
q.close()
expect(6)
}
expect(3)
yield() // to sender
expect(7)
yield() // try to resume sender (it will not resume despite the close!)
expect(8)
assertEquals(42, q.receiveOrNull())
expect(9)
assertNull(q.receiveOrNull())
expect(10)
yield() // to sender, it was resumed!
finish(12)
}
class BadClass {
override fun equals(other: Any?): Boolean = error("equals")
override fun hashCode(): Int = error("hashCode")
override fun toString(): String = error("toString")
}
@Test
fun testProduceBadClass() = runTest {
val bad = BadClass()
val c = produce {
expect(1)
send(bad)
}
assertTrue(c.receive() === bad)
finish(2)
}
@Test
fun testConsumeAll() = runTest {
val q = Channel<Int>(Channel.RENDEZVOUS)
for (i in 1..10) {
launch(start = CoroutineStart.UNDISPATCHED) {
expect(i)
q.send(i) // suspends
expectUnreached() // will get cancelled by cancel
}
}
expect(11)
q.cancel()
check(q.isClosedForSend)
check(q.isClosedForReceive)
check(q.receiveOrNull() == null)
finish(12)
}
@Test
fun testCancelWithCause() = runTest({ it is TestException }) {
val channel = Channel<Int>(Channel.RENDEZVOUS)
channel.cancel(TestException())
channel.receiveOrNull()
}
}
|