Roman Elizarov | f16fd27 | 2017-02-07 11:26:00 +0300 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-2017 JetBrains s.r.o. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Roman Elizarov | 187eace | 2017-01-31 09:39:58 +0300 | [diff] [blame] | 17 | package kotlinx.coroutines.experimental.channels |
| 18 | |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 19 | import kotlinx.coroutines.experimental.* |
Roman Elizarov | 9fe5f46 | 2018-02-21 19:05:52 +0300 | [diff] [blame] | 20 | import kotlin.coroutines.experimental.* |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 21 | import kotlin.test.* |
Roman Elizarov | 187eace | 2017-01-31 09:39:58 +0300 | [diff] [blame] | 22 | |
| 23 | class RendezvousChannelTest : TestBase() { |
| 24 | @Test |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 25 | fun testSimple() = runTest { |
Roman Elizarov | 187eace | 2017-01-31 09:39:58 +0300 | [diff] [blame] | 26 | val q = RendezvousChannel<Int>() |
| 27 | check(q.isEmpty && q.isFull) |
| 28 | expect(1) |
Roman Elizarov | 43e3af7 | 2017-07-21 16:01:31 +0300 | [diff] [blame] | 29 | val sender = launch(coroutineContext) { |
Roman Elizarov | 187eace | 2017-01-31 09:39:58 +0300 | [diff] [blame] | 30 | expect(4) |
| 31 | q.send(1) // suspend -- the first to come to rendezvous |
| 32 | expect(7) |
| 33 | q.send(2) // does not suspend -- receiver is there |
| 34 | expect(8) |
| 35 | } |
| 36 | expect(2) |
Roman Elizarov | 43e3af7 | 2017-07-21 16:01:31 +0300 | [diff] [blame] | 37 | val receiver = launch(coroutineContext) { |
Roman Elizarov | 187eace | 2017-01-31 09:39:58 +0300 | [diff] [blame] | 38 | expect(5) |
| 39 | check(q.receive() == 1) // does not suspend -- sender was there |
| 40 | expect(6) |
| 41 | check(q.receive() == 2) // suspends |
| 42 | expect(9) |
| 43 | } |
| 44 | expect(3) |
| 45 | sender.join() |
| 46 | receiver.join() |
| 47 | check(q.isEmpty && q.isFull) |
| 48 | finish(10) |
| 49 | } |
| 50 | |
| 51 | @Test |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 52 | fun testClosedReceiveOrNull() = runTest { |
Roman Elizarov | 187eace | 2017-01-31 09:39:58 +0300 | [diff] [blame] | 53 | val q = RendezvousChannel<Int>() |
| 54 | check(q.isEmpty && q.isFull && !q.isClosedForSend && !q.isClosedForReceive) |
| 55 | expect(1) |
Roman Elizarov | 43e3af7 | 2017-07-21 16:01:31 +0300 | [diff] [blame] | 56 | launch(coroutineContext) { |
Roman Elizarov | 187eace | 2017-01-31 09:39:58 +0300 | [diff] [blame] | 57 | expect(3) |
| 58 | assertEquals(42, q.receiveOrNull()) |
| 59 | expect(4) |
| 60 | assertEquals(null, q.receiveOrNull()) |
| 61 | expect(6) |
| 62 | } |
| 63 | expect(2) |
| 64 | q.send(42) |
| 65 | expect(5) |
| 66 | q.close() |
| 67 | check(!q.isEmpty && !q.isFull && q.isClosedForSend && q.isClosedForReceive) |
| 68 | yield() |
| 69 | check(!q.isEmpty && !q.isFull && q.isClosedForSend && q.isClosedForReceive) |
| 70 | finish(7) |
| 71 | } |
| 72 | |
| 73 | @Test |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 74 | fun testClosedExceptions() = runTest { |
Roman Elizarov | 187eace | 2017-01-31 09:39:58 +0300 | [diff] [blame] | 75 | val q = RendezvousChannel<Int>() |
| 76 | expect(1) |
Roman Elizarov | 43e3af7 | 2017-07-21 16:01:31 +0300 | [diff] [blame] | 77 | launch(coroutineContext) { |
Roman Elizarov | 187eace | 2017-01-31 09:39:58 +0300 | [diff] [blame] | 78 | expect(4) |
| 79 | try { q.receive() } |
| 80 | catch (e: ClosedReceiveChannelException) { |
| 81 | expect(5) |
| 82 | } |
| 83 | } |
| 84 | expect(2) |
| 85 | q.close() |
| 86 | expect(3) |
| 87 | yield() |
| 88 | expect(6) |
| 89 | try { q.send(42) } |
| 90 | catch (e: ClosedSendChannelException) { |
| 91 | finish(7) |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | @Test |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 96 | fun testOfferAndPool() = runTest { |
Roman Elizarov | 187eace | 2017-01-31 09:39:58 +0300 | [diff] [blame] | 97 | val q = RendezvousChannel<Int>() |
| 98 | assertFalse(q.offer(1)) |
| 99 | expect(1) |
Roman Elizarov | 43e3af7 | 2017-07-21 16:01:31 +0300 | [diff] [blame] | 100 | launch(coroutineContext) { |
Roman Elizarov | 187eace | 2017-01-31 09:39:58 +0300 | [diff] [blame] | 101 | expect(3) |
Roman Elizarov | 7b2d8b0 | 2017-02-02 20:09:14 +0300 | [diff] [blame] | 102 | assertEquals(null, q.poll()) |
Roman Elizarov | 187eace | 2017-01-31 09:39:58 +0300 | [diff] [blame] | 103 | expect(4) |
| 104 | assertEquals(2, q.receive()) |
| 105 | expect(7) |
Roman Elizarov | 7b2d8b0 | 2017-02-02 20:09:14 +0300 | [diff] [blame] | 106 | assertEquals(null, q.poll()) |
Roman Elizarov | 187eace | 2017-01-31 09:39:58 +0300 | [diff] [blame] | 107 | yield() |
| 108 | expect(9) |
Roman Elizarov | 7b2d8b0 | 2017-02-02 20:09:14 +0300 | [diff] [blame] | 109 | assertEquals(3, q.poll()) |
Roman Elizarov | 187eace | 2017-01-31 09:39:58 +0300 | [diff] [blame] | 110 | expect(10) |
| 111 | } |
| 112 | expect(2) |
| 113 | yield() |
| 114 | expect(5) |
| 115 | assertTrue(q.offer(2)) |
| 116 | expect(6) |
| 117 | yield() |
| 118 | expect(8) |
| 119 | q.send(3) |
| 120 | finish(11) |
| 121 | } |
Roman Elizarov | 7b2d8b0 | 2017-02-02 20:09:14 +0300 | [diff] [blame] | 122 | |
| 123 | @Test |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 124 | fun testIteratorClosed() = runTest { |
Roman Elizarov | 7b2d8b0 | 2017-02-02 20:09:14 +0300 | [diff] [blame] | 125 | val q = RendezvousChannel<Int>() |
| 126 | expect(1) |
Roman Elizarov | 43e3af7 | 2017-07-21 16:01:31 +0300 | [diff] [blame] | 127 | launch(coroutineContext) { |
Roman Elizarov | 7b2d8b0 | 2017-02-02 20:09:14 +0300 | [diff] [blame] | 128 | expect(3) |
| 129 | q.close() |
| 130 | expect(4) |
| 131 | } |
| 132 | expect(2) |
| 133 | for (x in q) { |
| 134 | expectUnreached() |
| 135 | } |
| 136 | finish(5) |
| 137 | } |
| 138 | |
| 139 | @Test |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 140 | fun testIteratorOne() = runTest { |
Roman Elizarov | 7b2d8b0 | 2017-02-02 20:09:14 +0300 | [diff] [blame] | 141 | val q = RendezvousChannel<Int>() |
| 142 | expect(1) |
Roman Elizarov | 43e3af7 | 2017-07-21 16:01:31 +0300 | [diff] [blame] | 143 | launch(coroutineContext) { |
Roman Elizarov | 7b2d8b0 | 2017-02-02 20:09:14 +0300 | [diff] [blame] | 144 | expect(3) |
| 145 | q.send(1) |
| 146 | expect(4) |
| 147 | q.close() |
| 148 | expect(5) |
| 149 | } |
| 150 | expect(2) |
| 151 | for (x in q) { |
| 152 | expect(6) |
| 153 | assertEquals(1, x) |
| 154 | } |
| 155 | finish(7) |
| 156 | } |
| 157 | |
| 158 | @Test |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 159 | fun testIteratorOneWithYield() = runTest { |
Roman Elizarov | 7b2d8b0 | 2017-02-02 20:09:14 +0300 | [diff] [blame] | 160 | val q = RendezvousChannel<Int>() |
| 161 | expect(1) |
Roman Elizarov | 43e3af7 | 2017-07-21 16:01:31 +0300 | [diff] [blame] | 162 | launch(coroutineContext) { |
Roman Elizarov | 7b2d8b0 | 2017-02-02 20:09:14 +0300 | [diff] [blame] | 163 | expect(3) |
| 164 | q.send(1) // will suspend |
| 165 | expect(6) |
| 166 | q.close() |
| 167 | expect(7) |
| 168 | } |
| 169 | expect(2) |
| 170 | yield() // yield to sender coroutine right before starting for loop |
| 171 | expect(4) |
| 172 | for (x in q) { |
| 173 | expect(5) |
| 174 | assertEquals(1, x) |
| 175 | } |
| 176 | finish(8) |
| 177 | } |
| 178 | |
| 179 | @Test |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 180 | fun testIteratorTwo() = runTest { |
Roman Elizarov | 7b2d8b0 | 2017-02-02 20:09:14 +0300 | [diff] [blame] | 181 | val q = RendezvousChannel<Int>() |
| 182 | expect(1) |
Roman Elizarov | 43e3af7 | 2017-07-21 16:01:31 +0300 | [diff] [blame] | 183 | launch(coroutineContext) { |
Roman Elizarov | 7b2d8b0 | 2017-02-02 20:09:14 +0300 | [diff] [blame] | 184 | expect(3) |
| 185 | q.send(1) |
| 186 | expect(4) |
| 187 | q.send(2) |
| 188 | expect(7) |
| 189 | q.close() |
| 190 | expect(8) |
| 191 | } |
| 192 | expect(2) |
| 193 | for (x in q) { |
| 194 | when (x) { |
| 195 | 1 -> expect(5) |
| 196 | 2 -> expect(6) |
| 197 | else -> expectUnreached() |
| 198 | } |
| 199 | } |
| 200 | finish(9) |
| 201 | } |
| 202 | |
| 203 | @Test |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 204 | fun testIteratorTwoWithYield() = runTest { |
Roman Elizarov | 7b2d8b0 | 2017-02-02 20:09:14 +0300 | [diff] [blame] | 205 | val q = RendezvousChannel<Int>() |
| 206 | expect(1) |
Roman Elizarov | 43e3af7 | 2017-07-21 16:01:31 +0300 | [diff] [blame] | 207 | launch(coroutineContext) { |
Roman Elizarov | 7b2d8b0 | 2017-02-02 20:09:14 +0300 | [diff] [blame] | 208 | expect(3) |
| 209 | q.send(1) // will suspend |
| 210 | expect(6) |
| 211 | q.send(2) |
| 212 | expect(7) |
| 213 | q.close() |
| 214 | expect(8) |
| 215 | } |
| 216 | expect(2) |
| 217 | yield() // yield to sender coroutine right before starting for loop |
| 218 | expect(4) |
| 219 | for (x in q) { |
| 220 | when (x) { |
| 221 | 1 -> expect(5) |
| 222 | 2 -> expect(9) |
| 223 | else -> expectUnreached() |
| 224 | } |
| 225 | } |
| 226 | finish(10) |
| 227 | } |
Roman Elizarov | b6b0125 | 2017-02-06 13:17:40 +0300 | [diff] [blame] | 228 | |
Roman Elizarov | 4e60132 | 2017-03-17 12:46:36 +0300 | [diff] [blame] | 229 | @Test |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 230 | fun testSuspendSendOnClosedChannel() = runTest { |
Roman Elizarov | 4e60132 | 2017-03-17 12:46:36 +0300 | [diff] [blame] | 231 | val q = RendezvousChannel<Int>() |
| 232 | expect(1) |
Roman Elizarov | 43e3af7 | 2017-07-21 16:01:31 +0300 | [diff] [blame] | 233 | launch(coroutineContext) { |
Roman Elizarov | 4e60132 | 2017-03-17 12:46:36 +0300 | [diff] [blame] | 234 | expect(4) |
| 235 | q.send(42) // suspend |
| 236 | expect(11) |
| 237 | } |
| 238 | expect(2) |
Roman Elizarov | 43e3af7 | 2017-07-21 16:01:31 +0300 | [diff] [blame] | 239 | launch(coroutineContext) { |
Roman Elizarov | 4e60132 | 2017-03-17 12:46:36 +0300 | [diff] [blame] | 240 | expect(5) |
| 241 | q.close() |
| 242 | expect(6) |
| 243 | } |
| 244 | expect(3) |
| 245 | yield() // to sender |
| 246 | expect(7) |
| 247 | yield() // try to resume sender (it will not resume despite the close!) |
| 248 | expect(8) |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 249 | assertEquals(42, q.receiveOrNull()) |
Roman Elizarov | 4e60132 | 2017-03-17 12:46:36 +0300 | [diff] [blame] | 250 | expect(9) |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 251 | assertNull(q.receiveOrNull()) |
Roman Elizarov | 4e60132 | 2017-03-17 12:46:36 +0300 | [diff] [blame] | 252 | expect(10) |
| 253 | yield() // to sender, it was resumed! |
| 254 | finish(12) |
| 255 | } |
| 256 | |
Roman Elizarov | b6b0125 | 2017-02-06 13:17:40 +0300 | [diff] [blame] | 257 | class BadClass { |
| 258 | override fun equals(other: Any?): Boolean = error("equals") |
| 259 | override fun hashCode(): Int = error("hashCode") |
| 260 | override fun toString(): String = error("toString") |
| 261 | } |
| 262 | |
| 263 | @Test |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 264 | fun testProduceBadClass() = runTest { |
Roman Elizarov | b6b0125 | 2017-02-06 13:17:40 +0300 | [diff] [blame] | 265 | val bad = BadClass() |
Roman Elizarov | 43e3af7 | 2017-07-21 16:01:31 +0300 | [diff] [blame] | 266 | val c = produce(coroutineContext) { |
Roman Elizarov | b6b0125 | 2017-02-06 13:17:40 +0300 | [diff] [blame] | 267 | expect(1) |
| 268 | send(bad) |
| 269 | } |
| 270 | assertTrue(c.receive() === bad) |
| 271 | finish(2) |
| 272 | } |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 273 | |
| 274 | @Test |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 275 | fun testConsumeAll() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 276 | val q = RendezvousChannel<Int>() |
| 277 | for (i in 1..10) { |
| 278 | launch(coroutineContext, CoroutineStart.UNDISPATCHED) { |
| 279 | expect(i) |
| 280 | q.send(i) // suspends |
| 281 | expectUnreached() // will get cancelled by cancel |
| 282 | } |
| 283 | } |
| 284 | expect(11) |
| 285 | q.cancel() |
| 286 | check(q.isClosedForSend) |
| 287 | check(q.isClosedForReceive) |
| 288 | check(q.receiveOrNull() == null) |
| 289 | finish(12) |
| 290 | } |
Vsevolod Tolstopyatov | 4b9a559 | 2018-04-11 13:17:14 +0300 | [diff] [blame] | 291 | |
Roman Elizarov | f2bdf60 | 2018-04-26 11:29:47 +0300 | [diff] [blame] | 292 | @Test |
| 293 | fun testCancelWithCause() = runTest({ it is TestException }) { |
Vsevolod Tolstopyatov | 4b9a559 | 2018-04-11 13:17:14 +0300 | [diff] [blame] | 294 | val channel = RendezvousChannel<Int>() |
Roman Elizarov | f2bdf60 | 2018-04-26 11:29:47 +0300 | [diff] [blame] | 295 | channel.cancel(TestException()) |
Vsevolod Tolstopyatov | 4b9a559 | 2018-04-11 13:17:14 +0300 | [diff] [blame] | 296 | channel.receiveOrNull() |
| 297 | } |
Roman Elizarov | f2bdf60 | 2018-04-26 11:29:47 +0300 | [diff] [blame] | 298 | |
| 299 | private class TestException : Exception() |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 300 | } |