Roman Elizarov | 3aed4ee | 2017-03-06 12:21:05 +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 | |
| 17 | package kotlinx.coroutines.experimental.channels |
| 18 | |
Roman Elizarov | 9fe5f46 | 2018-02-21 19:05:52 +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 | 3aed4ee | 2017-03-06 12:21:05 +0300 | [diff] [blame] | 22 | |
| 23 | class ConflatedChannelTest : TestBase() { |
| 24 | @Test |
| 25 | fun testBasicConflationOfferPoll() { |
| 26 | val q = ConflatedChannel<Int>() |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 27 | assertNull(q.poll()) |
| 28 | assertTrue(q.offer(1)) |
| 29 | assertTrue(q.offer(2)) |
| 30 | assertTrue(q.offer(3)) |
| 31 | assertEquals(3, q.poll()) |
| 32 | assertNull(q.poll()) |
Roman Elizarov | 3aed4ee | 2017-03-06 12:21:05 +0300 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | @Test |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 36 | fun testConflatedSend() = runTest { |
Roman Elizarov | e6e8ce8 | 2017-06-05 17:04:39 +0300 | [diff] [blame] | 37 | val q = ConflatedChannel<Int>() |
| 38 | q.send(1) |
| 39 | q.send(2) // shall conflated previously sent |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 40 | assertEquals(2, q.receiveOrNull()) |
Roman Elizarov | e6e8ce8 | 2017-06-05 17:04:39 +0300 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | @Test |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 44 | fun testConflatedClose() = runTest { |
Roman Elizarov | e6e8ce8 | 2017-06-05 17:04:39 +0300 | [diff] [blame] | 45 | val q = ConflatedChannel<Int>() |
| 46 | q.send(1) |
| 47 | q.close() // shall conflate sent item and become closed |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 48 | assertNull(q.receiveOrNull()) |
Roman Elizarov | e6e8ce8 | 2017-06-05 17:04:39 +0300 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | @Test |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 52 | fun testConflationSendReceive() = runTest { |
Roman Elizarov | 3aed4ee | 2017-03-06 12:21:05 +0300 | [diff] [blame] | 53 | val q = ConflatedChannel<Int>() |
| 54 | expect(1) |
Roman Elizarov | 43e3af7 | 2017-07-21 16:01:31 +0300 | [diff] [blame] | 55 | launch(coroutineContext) { // receiver coroutine |
Roman Elizarov | 3aed4ee | 2017-03-06 12:21:05 +0300 | [diff] [blame] | 56 | expect(4) |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 57 | assertEquals(2, q.receive()) |
Roman Elizarov | 3aed4ee | 2017-03-06 12:21:05 +0300 | [diff] [blame] | 58 | expect(5) |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 59 | assertEquals(3, q.receive()) // this receive suspends |
Roman Elizarov | 3aed4ee | 2017-03-06 12:21:05 +0300 | [diff] [blame] | 60 | expect(8) |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 61 | assertEquals(6, q.receive()) // last conflated value |
Roman Elizarov | 3aed4ee | 2017-03-06 12:21:05 +0300 | [diff] [blame] | 62 | expect(9) |
| 63 | } |
| 64 | expect(2) |
| 65 | q.send(1) |
| 66 | q.send(2) // shall conflate |
| 67 | expect(3) |
| 68 | yield() // to receiver |
| 69 | expect(6) |
| 70 | q.send(3) // send to the waiting receiver |
| 71 | q.send(4) // buffer |
| 72 | q.send(5) // conflate |
| 73 | q.send(6) // conflate again |
| 74 | expect(7) |
| 75 | yield() // to receiver |
| 76 | finish(10) |
| 77 | } |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 78 | |
| 79 | @Test |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 80 | fun testConsumeAll() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 81 | val q = ConflatedChannel<Int>() |
| 82 | expect(1) |
| 83 | for (i in 1..10) { |
| 84 | q.send(i) // stores as last |
| 85 | } |
| 86 | q.cancel() |
| 87 | check(q.isClosedForSend) |
| 88 | check(q.isClosedForReceive) |
| 89 | check(q.receiveOrNull() == null) |
| 90 | finish(2) |
| 91 | } |
Vsevolod Tolstopyatov | 4b9a559 | 2018-04-11 13:17:14 +0300 | [diff] [blame] | 92 | |
Roman Elizarov | f2bdf60 | 2018-04-26 11:29:47 +0300 | [diff] [blame] | 93 | @Test |
| 94 | fun testCancelWithCause() = runTest({ it is TestException }) { |
Vsevolod Tolstopyatov | 4b9a559 | 2018-04-11 13:17:14 +0300 | [diff] [blame] | 95 | val channel = ConflatedChannel<Int>() |
Roman Elizarov | f2bdf60 | 2018-04-26 11:29:47 +0300 | [diff] [blame] | 96 | channel.cancel(TestException()) |
Vsevolod Tolstopyatov | 4b9a559 | 2018-04-11 13:17:14 +0300 | [diff] [blame] | 97 | channel.receiveOrNull() |
| 98 | } |
Roman Elizarov | f2bdf60 | 2018-04-26 11:29:47 +0300 | [diff] [blame] | 99 | |
| 100 | private class TestException : Exception() |
Vsevolod Tolstopyatov | 4b9a559 | 2018-04-11 13:17:14 +0300 | [diff] [blame] | 101 | } |