blob: 3fb344a3da1df51cabb64b650dd988af55f6ebd6 [file] [log] [blame]
Roman Elizarov8b38fa22017-09-27 17:44:31 +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 Elizarov8b38fa22017-09-27 17:44:31 +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 Elizarov8b38fa22017-09-27 17:44:31 +030010
11class ProduceTest : TestBase() {
12 @Test
Roman Elizarov6aed8782017-10-20 14:00:50 +080013 fun testBasic() = runTest {
Roman Elizarov8b38fa22017-09-27 17:44:31 +030014 val c = produce(coroutineContext) {
Roman Elizarov6aed8782017-10-20 14:00:50 +080015 expect(2)
Roman Elizarov8b38fa22017-09-27 17:44:31 +030016 send(1)
Roman Elizarov6aed8782017-10-20 14:00:50 +080017 expect(3)
Roman Elizarov8b38fa22017-09-27 17:44:31 +030018 send(2)
Roman Elizarov6aed8782017-10-20 14:00:50 +080019 expect(6)
Roman Elizarov8b38fa22017-09-27 17:44:31 +030020 }
Roman Elizarov6aed8782017-10-20 14:00:50 +080021 expect(1)
Roman Elizarov8b38fa22017-09-27 17:44:31 +030022 check(c.receive() == 1)
Roman Elizarov6aed8782017-10-20 14:00:50 +080023 expect(4)
Roman Elizarov8b38fa22017-09-27 17:44:31 +030024 check(c.receive() == 2)
Roman Elizarov6aed8782017-10-20 14:00:50 +080025 expect(5)
Roman Elizarov8b38fa22017-09-27 17:44:31 +030026 check(c.receiveOrNull() == null)
Roman Elizarov6aed8782017-10-20 14:00:50 +080027 finish(7)
Roman Elizarov8b38fa22017-09-27 17:44:31 +030028 }
29
30 @Test
Vsevolod Tolstopyatov4b9a5592018-04-11 13:17:14 +030031 fun testCancelWithoutCause() = runTest {
Roman Elizarov8b38fa22017-09-27 17:44:31 +030032 val c = produce(coroutineContext) {
Roman Elizarov6aed8782017-10-20 14:00:50 +080033 expect(2)
Roman Elizarov8b38fa22017-09-27 17:44:31 +030034 send(1)
Roman Elizarov6aed8782017-10-20 14:00:50 +080035 expect(3)
36 try {
37 send(2) // will get cancelled
38 } catch (e: Throwable) {
Roman Elizarovb555d912017-08-17 21:01:33 +030039 finish(7)
Roman Elizarov6aed8782017-10-20 14:00:50 +080040 check(e is JobCancellationException && e.job == coroutineContext[Job])
41 throw e
42 }
Roman Elizarov8b38fa22017-09-27 17:44:31 +030043 expectUnreached()
44 }
Roman Elizarov6aed8782017-10-20 14:00:50 +080045 expect(1)
Roman Elizarov8b38fa22017-09-27 17:44:31 +030046 check(c.receive() == 1)
Roman Elizarov6aed8782017-10-20 14:00:50 +080047 expect(4)
Roman Elizarov8b38fa22017-09-27 17:44:31 +030048 c.cancel()
Roman Elizarov6aed8782017-10-20 14:00:50 +080049 expect(5)
Vsevolod Tolstopyatov4b9a5592018-04-11 13:17:14 +030050 assertNull(c.receiveOrNull())
Roman Elizarovb555d912017-08-17 21:01:33 +030051 expect(6)
Roman Elizarov8b38fa22017-09-27 17:44:31 +030052 }
Vsevolod Tolstopyatov4b9a5592018-04-11 13:17:14 +030053
54 @Test
55 fun testCancelWithCause() = runTest {
56 val c = produce(coroutineContext) {
57 expect(2)
58 send(1)
59 expect(3)
60 try {
61 send(2) // will get cancelled
62 } catch (e: Exception) {
63 finish(6)
64 check(e is JobCancellationException && e.job == coroutineContext[Job])
Roman Elizarovf2bdf602018-04-26 11:29:47 +030065 check(e.cause is TestException)
Vsevolod Tolstopyatov4b9a5592018-04-11 13:17:14 +030066 throw e
67 }
68 expectUnreached()
69 }
70
71 expect(1)
72 check(c.receive() == 1)
73 expect(4)
Roman Elizarovf2bdf602018-04-26 11:29:47 +030074 c.cancel(TestException())
Vsevolod Tolstopyatov4b9a5592018-04-11 13:17:14 +030075
76 try {
77 assertNull(c.receiveOrNull())
78 expectUnreached()
Roman Elizarovf2bdf602018-04-26 11:29:47 +030079 } catch (e: TestException) {
Vsevolod Tolstopyatov4b9a5592018-04-11 13:17:14 +030080 expect(5)
81 }
82 }
Roman Elizarovf2bdf602018-04-26 11:29:47 +030083
84 private class TestException : Exception()
Vsevolod Tolstopyatov4b9a5592018-04-11 13:17:14 +030085}