blob: 41ba67851d93d51d8e5ef3ccc1fa60e1e2901ba1 [file] [log] [blame]
Roman Elizarov8b38fa22017-09-27 17:44:31 +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 Elizarov9fe5f462018-02-21 19:05:52 +030019import kotlinx.coroutines.experimental.*
Roman Elizarov9fe5f462018-02-21 19:05:52 +030020import kotlin.coroutines.experimental.*
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +030021import kotlin.test.*
Roman Elizarov8b38fa22017-09-27 17:44:31 +030022
23class ProduceTest : TestBase() {
24 @Test
Roman Elizarov6aed8782017-10-20 14:00:50 +080025 fun testBasic() = runTest {
Roman Elizarov8b38fa22017-09-27 17:44:31 +030026 val c = produce(coroutineContext) {
Roman Elizarov6aed8782017-10-20 14:00:50 +080027 expect(2)
Roman Elizarov8b38fa22017-09-27 17:44:31 +030028 send(1)
Roman Elizarov6aed8782017-10-20 14:00:50 +080029 expect(3)
Roman Elizarov8b38fa22017-09-27 17:44:31 +030030 send(2)
Roman Elizarov6aed8782017-10-20 14:00:50 +080031 expect(6)
Roman Elizarov8b38fa22017-09-27 17:44:31 +030032 }
Roman Elizarov6aed8782017-10-20 14:00:50 +080033 expect(1)
Roman Elizarov8b38fa22017-09-27 17:44:31 +030034 check(c.receive() == 1)
Roman Elizarov6aed8782017-10-20 14:00:50 +080035 expect(4)
Roman Elizarov8b38fa22017-09-27 17:44:31 +030036 check(c.receive() == 2)
Roman Elizarov6aed8782017-10-20 14:00:50 +080037 expect(5)
Roman Elizarov8b38fa22017-09-27 17:44:31 +030038 check(c.receiveOrNull() == null)
Roman Elizarov6aed8782017-10-20 14:00:50 +080039 finish(7)
Roman Elizarov8b38fa22017-09-27 17:44:31 +030040 }
41
42 @Test
Vsevolod Tolstopyatov4b9a5592018-04-11 13:17:14 +030043 fun testCancelWithoutCause() = runTest {
Roman Elizarov8b38fa22017-09-27 17:44:31 +030044 val c = produce(coroutineContext) {
Roman Elizarov6aed8782017-10-20 14:00:50 +080045 expect(2)
Roman Elizarov8b38fa22017-09-27 17:44:31 +030046 send(1)
Roman Elizarov6aed8782017-10-20 14:00:50 +080047 expect(3)
48 try {
49 send(2) // will get cancelled
50 } catch (e: Throwable) {
Roman Elizarovb555d912017-08-17 21:01:33 +030051 finish(7)
Roman Elizarov6aed8782017-10-20 14:00:50 +080052 check(e is JobCancellationException && e.job == coroutineContext[Job])
53 throw e
54 }
Roman Elizarov8b38fa22017-09-27 17:44:31 +030055 expectUnreached()
56 }
Roman Elizarov6aed8782017-10-20 14:00:50 +080057 expect(1)
Roman Elizarov8b38fa22017-09-27 17:44:31 +030058 check(c.receive() == 1)
Roman Elizarov6aed8782017-10-20 14:00:50 +080059 expect(4)
Roman Elizarov8b38fa22017-09-27 17:44:31 +030060 c.cancel()
Roman Elizarov6aed8782017-10-20 14:00:50 +080061 expect(5)
Vsevolod Tolstopyatov4b9a5592018-04-11 13:17:14 +030062 assertNull(c.receiveOrNull())
Roman Elizarovb555d912017-08-17 21:01:33 +030063 expect(6)
Roman Elizarov8b38fa22017-09-27 17:44:31 +030064 }
Vsevolod Tolstopyatov4b9a5592018-04-11 13:17:14 +030065
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 Elizarovf2bdf602018-04-26 11:29:47 +030077 check(e.cause is TestException)
Vsevolod Tolstopyatov4b9a5592018-04-11 13:17:14 +030078 throw e
79 }
80 expectUnreached()
81 }
82
83 expect(1)
84 check(c.receive() == 1)
85 expect(4)
Roman Elizarovf2bdf602018-04-26 11:29:47 +030086 c.cancel(TestException())
Vsevolod Tolstopyatov4b9a5592018-04-11 13:17:14 +030087
88 try {
89 assertNull(c.receiveOrNull())
90 expectUnreached()
Roman Elizarovf2bdf602018-04-26 11:29:47 +030091 } catch (e: TestException) {
Vsevolod Tolstopyatov4b9a5592018-04-11 13:17:14 +030092 expect(5)
93 }
94 }
Roman Elizarovf2bdf602018-04-26 11:29:47 +030095
96 private class TestException : Exception()
Vsevolod Tolstopyatov4b9a5592018-04-11 13:17:14 +030097}