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