Roman Elizarov | 8b38fa2 | 2017-09-27 17:44:31 +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 | 8b38fa2 | 2017-09-27 17:44:31 +0300 | [diff] [blame] | 3 | */ |
4 | |||||
5 | package kotlinx.coroutines.experimental.channels | ||||
6 | |||||
Roman Elizarov | 9fe5f46 | 2018-02-21 19:05:52 +0300 | [diff] [blame] | 7 | import kotlinx.coroutines.experimental.* |
Roman Elizarov | 9fe5f46 | 2018-02-21 19:05:52 +0300 | [diff] [blame] | 8 | import kotlin.coroutines.experimental.* |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 9 | import kotlin.test.* |
Roman Elizarov | 8b38fa2 | 2017-09-27 17:44:31 +0300 | [diff] [blame] | 10 | |
11 | class ProduceTest : TestBase() { | ||||
12 | @Test | ||||
Roman Elizarov | 6aed878 | 2017-10-20 14:00:50 +0800 | [diff] [blame] | 13 | fun testBasic() = runTest { |
Roman Elizarov | 8b38fa2 | 2017-09-27 17:44:31 +0300 | [diff] [blame] | 14 | val c = produce(coroutineContext) { |
Roman Elizarov | 6aed878 | 2017-10-20 14:00:50 +0800 | [diff] [blame] | 15 | expect(2) |
Roman Elizarov | 8b38fa2 | 2017-09-27 17:44:31 +0300 | [diff] [blame] | 16 | send(1) |
Roman Elizarov | 6aed878 | 2017-10-20 14:00:50 +0800 | [diff] [blame] | 17 | expect(3) |
Roman Elizarov | 8b38fa2 | 2017-09-27 17:44:31 +0300 | [diff] [blame] | 18 | send(2) |
Roman Elizarov | 6aed878 | 2017-10-20 14:00:50 +0800 | [diff] [blame] | 19 | expect(6) |
Roman Elizarov | 8b38fa2 | 2017-09-27 17:44:31 +0300 | [diff] [blame] | 20 | } |
Roman Elizarov | 6aed878 | 2017-10-20 14:00:50 +0800 | [diff] [blame] | 21 | expect(1) |
Roman Elizarov | 8b38fa2 | 2017-09-27 17:44:31 +0300 | [diff] [blame] | 22 | check(c.receive() == 1) |
Roman Elizarov | 6aed878 | 2017-10-20 14:00:50 +0800 | [diff] [blame] | 23 | expect(4) |
Roman Elizarov | 8b38fa2 | 2017-09-27 17:44:31 +0300 | [diff] [blame] | 24 | check(c.receive() == 2) |
Roman Elizarov | 6aed878 | 2017-10-20 14:00:50 +0800 | [diff] [blame] | 25 | expect(5) |
Roman Elizarov | 8b38fa2 | 2017-09-27 17:44:31 +0300 | [diff] [blame] | 26 | check(c.receiveOrNull() == null) |
Roman Elizarov | 6aed878 | 2017-10-20 14:00:50 +0800 | [diff] [blame] | 27 | finish(7) |
Roman Elizarov | 8b38fa2 | 2017-09-27 17:44:31 +0300 | [diff] [blame] | 28 | } |
29 | |||||
30 | @Test | ||||
Vsevolod Tolstopyatov | 4b9a559 | 2018-04-11 13:17:14 +0300 | [diff] [blame] | 31 | fun testCancelWithoutCause() = runTest { |
Roman Elizarov | 8b38fa2 | 2017-09-27 17:44:31 +0300 | [diff] [blame] | 32 | val c = produce(coroutineContext) { |
Roman Elizarov | 6aed878 | 2017-10-20 14:00:50 +0800 | [diff] [blame] | 33 | expect(2) |
Roman Elizarov | 8b38fa2 | 2017-09-27 17:44:31 +0300 | [diff] [blame] | 34 | send(1) |
Roman Elizarov | 6aed878 | 2017-10-20 14:00:50 +0800 | [diff] [blame] | 35 | expect(3) |
36 | try { | ||||
37 | send(2) // will get cancelled | ||||
38 | } catch (e: Throwable) { | ||||
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 39 | finish(7) |
Roman Elizarov | 6aed878 | 2017-10-20 14:00:50 +0800 | [diff] [blame] | 40 | check(e is JobCancellationException && e.job == coroutineContext[Job]) |
41 | throw e | ||||
42 | } | ||||
Roman Elizarov | 8b38fa2 | 2017-09-27 17:44:31 +0300 | [diff] [blame] | 43 | expectUnreached() |
44 | } | ||||
Roman Elizarov | 6aed878 | 2017-10-20 14:00:50 +0800 | [diff] [blame] | 45 | expect(1) |
Roman Elizarov | 8b38fa2 | 2017-09-27 17:44:31 +0300 | [diff] [blame] | 46 | check(c.receive() == 1) |
Roman Elizarov | 6aed878 | 2017-10-20 14:00:50 +0800 | [diff] [blame] | 47 | expect(4) |
Roman Elizarov | 8b38fa2 | 2017-09-27 17:44:31 +0300 | [diff] [blame] | 48 | c.cancel() |
Roman Elizarov | 6aed878 | 2017-10-20 14:00:50 +0800 | [diff] [blame] | 49 | expect(5) |
Vsevolod Tolstopyatov | 4b9a559 | 2018-04-11 13:17:14 +0300 | [diff] [blame] | 50 | assertNull(c.receiveOrNull()) |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 51 | expect(6) |
Roman Elizarov | 8b38fa2 | 2017-09-27 17:44:31 +0300 | [diff] [blame] | 52 | } |
Vsevolod Tolstopyatov | 4b9a559 | 2018-04-11 13:17:14 +0300 | [diff] [blame] | 53 | |
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 Elizarov | f2bdf60 | 2018-04-26 11:29:47 +0300 | [diff] [blame] | 65 | check(e.cause is TestException) |
Vsevolod Tolstopyatov | 4b9a559 | 2018-04-11 13:17:14 +0300 | [diff] [blame] | 66 | throw e |
67 | } | ||||
68 | expectUnreached() | ||||
69 | } | ||||
70 | |||||
71 | expect(1) | ||||
72 | check(c.receive() == 1) | ||||
73 | expect(4) | ||||
Roman Elizarov | f2bdf60 | 2018-04-26 11:29:47 +0300 | [diff] [blame] | 74 | c.cancel(TestException()) |
Vsevolod Tolstopyatov | 4b9a559 | 2018-04-11 13:17:14 +0300 | [diff] [blame] | 75 | |
76 | try { | ||||
77 | assertNull(c.receiveOrNull()) | ||||
78 | expectUnreached() | ||||
Roman Elizarov | f2bdf60 | 2018-04-26 11:29:47 +0300 | [diff] [blame] | 79 | } catch (e: TestException) { |
Vsevolod Tolstopyatov | 4b9a559 | 2018-04-11 13:17:14 +0300 | [diff] [blame] | 80 | expect(5) |
81 | } | ||||
82 | } | ||||
Roman Elizarov | f2bdf60 | 2018-04-26 11:29:47 +0300 | [diff] [blame] | 83 | |
84 | private class TestException : Exception() | ||||
Vsevolod Tolstopyatov | 4b9a559 | 2018-04-11 13:17:14 +0300 | [diff] [blame] | 85 | } |