Roman Elizarov | f16fd27 | 2017-02-07 11:26:00 +0300 | [diff] [blame] | 1 | /* |
Roman Elizarov | 1f74a2d | 2018-06-29 19:19:45 +0300 | [diff] [blame^] | 2 | * Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. |
Roman Elizarov | f16fd27 | 2017-02-07 11:26:00 +0300 | [diff] [blame] | 3 | */ |
| 4 | |
Roman Elizarov | 7b2d8b0 | 2017-02-02 20:09:14 +0300 | [diff] [blame] | 5 | package kotlinx.coroutines.experimental.channels |
| 6 | |
| 7 | import kotlinx.coroutines.experimental.* |
Roman Elizarov | 9fe5f46 | 2018-02-21 19:05:52 +0300 | [diff] [blame] | 8 | import kotlin.coroutines.experimental.* |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 9 | import kotlin.test.* |
Roman Elizarov | 7b2d8b0 | 2017-02-02 20:09:14 +0300 | [diff] [blame] | 10 | |
| 11 | class ArrayChannelTest : TestBase() { |
| 12 | @Test |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 13 | fun testSimple() = runTest { |
Roman Elizarov | 7b2d8b0 | 2017-02-02 20:09:14 +0300 | [diff] [blame] | 14 | val q = ArrayChannel<Int>(1) |
| 15 | check(q.isEmpty && !q.isFull) |
| 16 | expect(1) |
Roman Elizarov | 43e3af7 | 2017-07-21 16:01:31 +0300 | [diff] [blame] | 17 | val sender = launch(coroutineContext) { |
Roman Elizarov | 7b2d8b0 | 2017-02-02 20:09:14 +0300 | [diff] [blame] | 18 | expect(4) |
| 19 | q.send(1) // success -- buffered |
| 20 | check(!q.isEmpty && q.isFull) |
| 21 | expect(5) |
| 22 | q.send(2) // suspends (buffer full) |
| 23 | expect(9) |
| 24 | } |
| 25 | expect(2) |
Roman Elizarov | 43e3af7 | 2017-07-21 16:01:31 +0300 | [diff] [blame] | 26 | val receiver = launch(coroutineContext) { |
Roman Elizarov | 7b2d8b0 | 2017-02-02 20:09:14 +0300 | [diff] [blame] | 27 | expect(6) |
| 28 | check(q.receive() == 1) // does not suspend -- took from buffer |
| 29 | check(!q.isEmpty && q.isFull) // waiting sender's element moved to buffer |
| 30 | expect(7) |
| 31 | check(q.receive() == 2) // does not suspend (takes from sender) |
| 32 | expect(8) |
| 33 | } |
| 34 | expect(3) |
| 35 | sender.join() |
| 36 | receiver.join() |
| 37 | check(q.isEmpty && !q.isFull) |
| 38 | finish(10) |
| 39 | } |
| 40 | |
| 41 | @Test |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 42 | fun testClosedBufferedReceiveOrNull() = runTest { |
Roman Elizarov | 7b2d8b0 | 2017-02-02 20:09:14 +0300 | [diff] [blame] | 43 | val q = ArrayChannel<Int>(1) |
| 44 | check(q.isEmpty && !q.isFull && !q.isClosedForSend && !q.isClosedForReceive) |
| 45 | expect(1) |
Roman Elizarov | 43e3af7 | 2017-07-21 16:01:31 +0300 | [diff] [blame] | 46 | launch(coroutineContext) { |
Roman Elizarov | 7b2d8b0 | 2017-02-02 20:09:14 +0300 | [diff] [blame] | 47 | expect(5) |
| 48 | check(!q.isEmpty && !q.isFull && q.isClosedForSend && !q.isClosedForReceive) |
| 49 | assertEquals(42, q.receiveOrNull()) |
| 50 | expect(6) |
| 51 | check(!q.isEmpty && !q.isFull && q.isClosedForSend && q.isClosedForReceive) |
| 52 | assertEquals(null, q.receiveOrNull()) |
| 53 | expect(7) |
| 54 | } |
| 55 | expect(2) |
| 56 | q.send(42) // buffers |
| 57 | expect(3) |
| 58 | q.close() // goes on |
| 59 | expect(4) |
| 60 | check(!q.isEmpty && !q.isFull && q.isClosedForSend && !q.isClosedForReceive) |
| 61 | yield() |
| 62 | check(!q.isEmpty && !q.isFull && q.isClosedForSend && q.isClosedForReceive) |
| 63 | finish(8) |
| 64 | } |
| 65 | |
| 66 | @Test |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 67 | fun testClosedExceptions() = runTest { |
Roman Elizarov | 7b2d8b0 | 2017-02-02 20:09:14 +0300 | [diff] [blame] | 68 | val q = ArrayChannel<Int>(1) |
| 69 | expect(1) |
Roman Elizarov | 43e3af7 | 2017-07-21 16:01:31 +0300 | [diff] [blame] | 70 | launch(coroutineContext) { |
Roman Elizarov | 7b2d8b0 | 2017-02-02 20:09:14 +0300 | [diff] [blame] | 71 | expect(4) |
| 72 | try { q.receive() } |
| 73 | catch (e: ClosedReceiveChannelException) { |
| 74 | expect(5) |
| 75 | } |
| 76 | } |
| 77 | expect(2) |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 78 | |
| 79 | require(q.close()) |
Roman Elizarov | 7b2d8b0 | 2017-02-02 20:09:14 +0300 | [diff] [blame] | 80 | expect(3) |
| 81 | yield() |
| 82 | expect(6) |
| 83 | try { q.send(42) } |
| 84 | catch (e: ClosedSendChannelException) { |
| 85 | finish(7) |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | @Test |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 90 | fun testOfferAndPool() = runTest { |
Roman Elizarov | 7b2d8b0 | 2017-02-02 20:09:14 +0300 | [diff] [blame] | 91 | val q = ArrayChannel<Int>(1) |
| 92 | assertTrue(q.offer(1)) |
| 93 | expect(1) |
Roman Elizarov | 43e3af7 | 2017-07-21 16:01:31 +0300 | [diff] [blame] | 94 | launch(coroutineContext) { |
Roman Elizarov | 7b2d8b0 | 2017-02-02 20:09:14 +0300 | [diff] [blame] | 95 | expect(3) |
| 96 | assertEquals(1, q.poll()) |
| 97 | expect(4) |
| 98 | assertEquals(null, q.poll()) |
| 99 | expect(5) |
| 100 | assertEquals(2, q.receive()) // suspends |
| 101 | expect(9) |
| 102 | assertEquals(3, q.poll()) |
| 103 | expect(10) |
| 104 | assertEquals(null, q.poll()) |
| 105 | expect(11) |
| 106 | } |
| 107 | expect(2) |
| 108 | yield() |
| 109 | expect(6) |
| 110 | assertTrue(q.offer(2)) |
| 111 | expect(7) |
| 112 | assertTrue(q.offer(3)) |
| 113 | expect(8) |
| 114 | assertFalse(q.offer(4)) |
| 115 | yield() |
| 116 | finish(12) |
| 117 | } |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 118 | |
| 119 | @Test |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 120 | fun testConsumeAll() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 121 | val q = ArrayChannel<Int>(5) |
| 122 | for (i in 1..10) { |
| 123 | if (i <= 5) { |
| 124 | expect(i) |
| 125 | q.send(i) // shall buffer |
| 126 | } else { |
| 127 | launch(coroutineContext, CoroutineStart.UNDISPATCHED) { |
| 128 | expect(i) |
| 129 | q.send(i) // suspends |
| 130 | expectUnreached() // will get cancelled by cancel |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | expect(11) |
| 135 | q.cancel() |
| 136 | check(q.isClosedForSend) |
| 137 | check(q.isClosedForReceive) |
| 138 | check(q.receiveOrNull() == null) |
| 139 | finish(12) |
| 140 | } |
Vsevolod Tolstopyatov | 4b9a559 | 2018-04-11 13:17:14 +0300 | [diff] [blame] | 141 | |
Roman Elizarov | f2bdf60 | 2018-04-26 11:29:47 +0300 | [diff] [blame] | 142 | @Test |
| 143 | fun testCancelWithCause() = runTest({ it is TestException }) { |
Vsevolod Tolstopyatov | 4b9a559 | 2018-04-11 13:17:14 +0300 | [diff] [blame] | 144 | val channel = ArrayChannel<Int>(5) |
Roman Elizarov | f2bdf60 | 2018-04-26 11:29:47 +0300 | [diff] [blame] | 145 | channel.cancel(TestException()) |
Vsevolod Tolstopyatov | 4b9a559 | 2018-04-11 13:17:14 +0300 | [diff] [blame] | 146 | channel.receiveOrNull() |
| 147 | } |
Roman Elizarov | f2bdf60 | 2018-04-26 11:29:47 +0300 | [diff] [blame] | 148 | |
| 149 | private class TestException : Exception() |
Vsevolod Tolstopyatov | 4b9a559 | 2018-04-11 13:17:14 +0300 | [diff] [blame] | 150 | } |