blob: 897801e378caad60c43242b0c15c9dba6d25bf4c [file] [log] [blame]
Roman Elizarov98b7a6e2017-06-07 08:43:43 +03001/*
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
17package kotlinx.coroutines.experimental.channels
18
Roman Elizarov98b7a6e2017-06-07 08:43:43 +030019import kotlinx.coroutines.experimental.TestBase
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +030020import kotlin.test.*
Roman Elizarov98b7a6e2017-06-07 08:43:43 +030021
22class LinkedListChannelTest : TestBase() {
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +030023
Roman Elizarov98b7a6e2017-06-07 08:43:43 +030024 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +030025 fun testBasic() = runTest {
Roman Elizarov98b7a6e2017-06-07 08:43:43 +030026 val c = LinkedListChannel<Int>()
27 c.send(1)
Roman Elizaroveab2cff2017-07-21 19:07:17 +030028 check(c.offer(2))
Roman Elizarov98b7a6e2017-06-07 08:43:43 +030029 c.send(3)
Roman Elizaroveab2cff2017-07-21 19:07:17 +030030 check(c.close())
31 check(!c.close())
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +030032 assertEquals(1, c.receive())
33 assertEquals(2, c.poll())
34 assertEquals(3, c.receiveOrNull())
35 assertNull(c.receiveOrNull())
Roman Elizarov98b7a6e2017-06-07 08:43:43 +030036 }
Roman Elizarovb555d912017-08-17 21:01:33 +030037
38 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +030039 fun testConsumeAll() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +030040 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 Tolstopyatov96191342018-04-20 18:13:33 +030049}