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