Roman Elizarov | 98b7a6e | 2017-06-07 08:43:43 +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 | 98b7a6e | 2017-06-07 08:43:43 +0300 | [diff] [blame] | 3 | */ |
| 4 | |
| 5 | package kotlinx.coroutines.experimental.channels |
| 6 | |
Roman Elizarov | f2bdf60 | 2018-04-26 11:29:47 +0300 | [diff] [blame] | 7 | import kotlinx.coroutines.experimental.* |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 8 | import kotlin.test.* |
Roman Elizarov | 98b7a6e | 2017-06-07 08:43:43 +0300 | [diff] [blame] | 9 | |
| 10 | class LinkedListChannelTest : TestBase() { |
| 11 | @Test |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 12 | fun testBasic() = runTest { |
Roman Elizarov | 98b7a6e | 2017-06-07 08:43:43 +0300 | [diff] [blame] | 13 | val c = LinkedListChannel<Int>() |
| 14 | c.send(1) |
Roman Elizarov | eab2cff | 2017-07-21 19:07:17 +0300 | [diff] [blame] | 15 | check(c.offer(2)) |
Roman Elizarov | 98b7a6e | 2017-06-07 08:43:43 +0300 | [diff] [blame] | 16 | c.send(3) |
Roman Elizarov | eab2cff | 2017-07-21 19:07:17 +0300 | [diff] [blame] | 17 | check(c.close()) |
| 18 | check(!c.close()) |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 19 | assertEquals(1, c.receive()) |
| 20 | assertEquals(2, c.poll()) |
| 21 | assertEquals(3, c.receiveOrNull()) |
| 22 | assertNull(c.receiveOrNull()) |
Roman Elizarov | 98b7a6e | 2017-06-07 08:43:43 +0300 | [diff] [blame] | 23 | } |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 24 | |
| 25 | @Test |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 26 | fun testConsumeAll() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 27 | val q = LinkedListChannel<Int>() |
| 28 | for (i in 1..10) { |
| 29 | q.send(i) // buffers |
| 30 | } |
| 31 | q.cancel() |
| 32 | check(q.isClosedForSend) |
| 33 | check(q.isClosedForReceive) |
| 34 | check(q.receiveOrNull() == null) |
| 35 | } |
Vsevolod Tolstopyatov | 4b9a559 | 2018-04-11 13:17:14 +0300 | [diff] [blame] | 36 | |
Roman Elizarov | f2bdf60 | 2018-04-26 11:29:47 +0300 | [diff] [blame] | 37 | @Test |
| 38 | fun testCancelWithCause() = runTest({ it is TestException }) { |
Vsevolod Tolstopyatov | 4b9a559 | 2018-04-11 13:17:14 +0300 | [diff] [blame] | 39 | val channel = LinkedListChannel<Int>() |
Roman Elizarov | f2bdf60 | 2018-04-26 11:29:47 +0300 | [diff] [blame] | 40 | channel.cancel(TestException()) |
Vsevolod Tolstopyatov | 4b9a559 | 2018-04-11 13:17:14 +0300 | [diff] [blame] | 41 | channel.receiveOrNull() |
| 42 | } |
Roman Elizarov | f2bdf60 | 2018-04-26 11:29:47 +0300 | [diff] [blame] | 43 | |
| 44 | private class TestException : Exception() |
Vsevolod Tolstopyatov | 4b9a559 | 2018-04-11 13:17:14 +0300 | [diff] [blame] | 45 | } |