blob: 4af0944a87d7fbe2afaf85b558757890862918ac [file] [log] [blame]
Roman Elizarov3aed4ee2017-03-06 12:21:05 +03001/*
Roman Elizarov1f74a2d2018-06-29 19:19:45 +03002 * Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
Roman Elizarov3aed4ee2017-03-06 12:21:05 +03003 */
4
5package kotlinx.coroutines.experimental.channels
6
Roman Elizarov9fe5f462018-02-21 19:05:52 +03007import kotlinx.coroutines.experimental.*
Roman Elizarov9fe5f462018-02-21 19:05:52 +03008import kotlin.coroutines.experimental.*
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +03009import kotlin.test.*
Roman Elizarov3aed4ee2017-03-06 12:21:05 +030010
11class ConflatedChannelTest : TestBase() {
12 @Test
13 fun testBasicConflationOfferPoll() {
14 val q = ConflatedChannel<Int>()
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +030015 assertNull(q.poll())
16 assertTrue(q.offer(1))
17 assertTrue(q.offer(2))
18 assertTrue(q.offer(3))
19 assertEquals(3, q.poll())
20 assertNull(q.poll())
Roman Elizarov3aed4ee2017-03-06 12:21:05 +030021 }
22
23 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +030024 fun testConflatedSend() = runTest {
Roman Elizarove6e8ce82017-06-05 17:04:39 +030025 val q = ConflatedChannel<Int>()
26 q.send(1)
27 q.send(2) // shall conflated previously sent
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +030028 assertEquals(2, q.receiveOrNull())
Roman Elizarove6e8ce82017-06-05 17:04:39 +030029 }
30
31 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +030032 fun testConflatedClose() = runTest {
Roman Elizarove6e8ce82017-06-05 17:04:39 +030033 val q = ConflatedChannel<Int>()
34 q.send(1)
35 q.close() // shall conflate sent item and become closed
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +030036 assertNull(q.receiveOrNull())
Roman Elizarove6e8ce82017-06-05 17:04:39 +030037 }
38
39 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +030040 fun testConflationSendReceive() = runTest {
Roman Elizarov3aed4ee2017-03-06 12:21:05 +030041 val q = ConflatedChannel<Int>()
42 expect(1)
Roman Elizarov43e3af72017-07-21 16:01:31 +030043 launch(coroutineContext) { // receiver coroutine
Roman Elizarov3aed4ee2017-03-06 12:21:05 +030044 expect(4)
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +030045 assertEquals(2, q.receive())
Roman Elizarov3aed4ee2017-03-06 12:21:05 +030046 expect(5)
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +030047 assertEquals(3, q.receive()) // this receive suspends
Roman Elizarov3aed4ee2017-03-06 12:21:05 +030048 expect(8)
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +030049 assertEquals(6, q.receive()) // last conflated value
Roman Elizarov3aed4ee2017-03-06 12:21:05 +030050 expect(9)
51 }
52 expect(2)
53 q.send(1)
54 q.send(2) // shall conflate
55 expect(3)
56 yield() // to receiver
57 expect(6)
58 q.send(3) // send to the waiting receiver
59 q.send(4) // buffer
60 q.send(5) // conflate
61 q.send(6) // conflate again
62 expect(7)
63 yield() // to receiver
64 finish(10)
65 }
Roman Elizarovb555d912017-08-17 21:01:33 +030066
67 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +030068 fun testConsumeAll() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +030069 val q = ConflatedChannel<Int>()
70 expect(1)
71 for (i in 1..10) {
72 q.send(i) // stores as last
73 }
74 q.cancel()
75 check(q.isClosedForSend)
76 check(q.isClosedForReceive)
77 check(q.receiveOrNull() == null)
78 finish(2)
79 }
Vsevolod Tolstopyatov4b9a5592018-04-11 13:17:14 +030080
Roman Elizarovf2bdf602018-04-26 11:29:47 +030081 @Test
82 fun testCancelWithCause() = runTest({ it is TestException }) {
Vsevolod Tolstopyatov4b9a5592018-04-11 13:17:14 +030083 val channel = ConflatedChannel<Int>()
Roman Elizarovf2bdf602018-04-26 11:29:47 +030084 channel.cancel(TestException())
Vsevolod Tolstopyatov4b9a5592018-04-11 13:17:14 +030085 channel.receiveOrNull()
86 }
Roman Elizarovf2bdf602018-04-26 11:29:47 +030087
88 private class TestException : Exception()
Vsevolod Tolstopyatov4b9a5592018-04-11 13:17:14 +030089}