blob: eeb62b4a85c31660b9c30dcd411fca1fe72f382c [file] [log] [blame]
Roman Elizarovfadf8c82017-03-02 23:36:06 +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 Elizarovfadf8c82017-03-02 23:36:06 +03003 */
4
Roman Elizarovaa461cf2018-04-11 13:20:29 +03005@file:Suppress("NAMED_ARGUMENTS_NOT_ALLOWED") // KT-21913
6
Roman Elizarov1216e912017-02-22 09:57:06 +03007package kotlinx.coroutines.experimental.selects
8
9import kotlinx.coroutines.experimental.*
Roman Elizarov9fe5f462018-02-21 19:05:52 +030010import kotlin.coroutines.experimental.*
Roman Elizarovaa461cf2018-04-11 13:20:29 +030011import kotlin.test.*
Roman Elizarov1216e912017-02-22 09:57:06 +030012
13class SelectDeferredTest : TestBase() {
14 @Test
Roman Elizarovaa461cf2018-04-11 13:20:29 +030015 fun testSimpleReturnsImmediately() = runTest {
Roman Elizarov1216e912017-02-22 09:57:06 +030016 expect(1)
Roman Elizarovaa461cf2018-04-11 13:20:29 +030017 val d1 = async(coroutineContext) {
Roman Elizarov1216e912017-02-22 09:57:06 +030018 expect(3)
19 42
20 }
21 expect(2)
22 val res = select<String> {
23 d1.onAwait { v ->
24 expect(4)
25 assertEquals(42, v)
26 "OK"
27 }
28 }
29 expect(5)
30 assertEquals("OK", res)
31 finish(6)
32 }
33
34 @Test
Roman Elizarovaa461cf2018-04-11 13:20:29 +030035 fun testSimpleWithYield() = runTest {
Roman Elizarov1216e912017-02-22 09:57:06 +030036 expect(1)
Roman Elizarovaa461cf2018-04-11 13:20:29 +030037 val d1 = async(coroutineContext) {
Roman Elizarov1216e912017-02-22 09:57:06 +030038 expect(3)
39 42
40 }
Roman Elizarov43e3af72017-07-21 16:01:31 +030041 launch(coroutineContext) {
Roman Elizarovd84dbc22017-02-22 14:56:58 +030042 expect(4)
Roman Elizarov1216e912017-02-22 09:57:06 +030043 yield() // back to main
Roman Elizarovd84dbc22017-02-22 14:56:58 +030044 expect(6)
Roman Elizarov1216e912017-02-22 09:57:06 +030045 }
46 expect(2)
47 val res = select<String> {
48 d1.onAwait { v ->
Roman Elizarovd84dbc22017-02-22 14:56:58 +030049 expect(5)
Roman Elizarov1216e912017-02-22 09:57:06 +030050 assertEquals(42, v)
51 yield() // to launch
Roman Elizarovd84dbc22017-02-22 14:56:58 +030052 expect(7)
Roman Elizarov1216e912017-02-22 09:57:06 +030053 "OK"
54 }
55 }
Roman Elizarovd84dbc22017-02-22 14:56:58 +030056 finish(8)
Roman Elizarov1216e912017-02-22 09:57:06 +030057 assertEquals("OK", res)
Roman Elizarovd84dbc22017-02-22 14:56:58 +030058 }
59
60 @Test
Roman Elizarovaa461cf2018-04-11 13:20:29 +030061 fun testSelectIncompleteLazy() = runTest {
Roman Elizarovd84dbc22017-02-22 14:56:58 +030062 expect(1)
Roman Elizarov43e3af72017-07-21 16:01:31 +030063 val d1 = async(coroutineContext, CoroutineStart.LAZY) {
Roman Elizarovd84dbc22017-02-22 14:56:58 +030064 expect(5)
65 42
66 }
Roman Elizarov43e3af72017-07-21 16:01:31 +030067 launch(coroutineContext) {
Roman Elizarovd84dbc22017-02-22 14:56:58 +030068 expect(3)
69 val res = select<String> {
70 d1.onAwait { v ->
71 expect(7)
72 assertEquals(42, v)
73 "OK"
74 }
75 }
76 expect(8)
77 assertEquals("OK", res)
78 }
79 expect(2)
80 yield() // to launch
81 expect(4)
82 yield() // to started async
83 expect(6)
84 yield() // to triggered select
85 finish(9)
Roman Elizarov1216e912017-02-22 09:57:06 +030086 }
87
88 @Test
Roman Elizarovaa461cf2018-04-11 13:20:29 +030089 fun testSelectTwo() = runTest {
Roman Elizarov1216e912017-02-22 09:57:06 +030090 expect(1)
Roman Elizarovaa461cf2018-04-11 13:20:29 +030091 val d1 = async(coroutineContext) {
Roman Elizarov1216e912017-02-22 09:57:06 +030092 expect(3)
93 yield() // to the other deffered
Roman Elizarovd84dbc22017-02-22 14:56:58 +030094 expect(5)
95 yield() // to fired select
96 expect(7)
Roman Elizarov1216e912017-02-22 09:57:06 +030097 "d1"
98 }
Roman Elizarovaa461cf2018-04-11 13:20:29 +030099 val d2 = async(coroutineContext) {
Roman Elizarov1216e912017-02-22 09:57:06 +0300100 expect(4)
101 "d2" // returns result
102 }
103 expect(2)
104 val res = select<String> {
Roman Elizarov7adb8762017-03-17 17:54:13 +0300105 d1.onAwait {
Roman Elizarov1216e912017-02-22 09:57:06 +0300106 expectUnreached()
107 "FAIL"
108 }
109 d2.onAwait { v2 ->
Roman Elizarovd84dbc22017-02-22 14:56:58 +0300110 expect(6)
Roman Elizarov1216e912017-02-22 09:57:06 +0300111 assertEquals("d2", v2)
112 yield() // to first deferred
Roman Elizarovd84dbc22017-02-22 14:56:58 +0300113 expect(8)
Roman Elizarov1216e912017-02-22 09:57:06 +0300114 "OK"
115 }
116 }
117 assertEquals("OK", res)
Roman Elizarovd84dbc22017-02-22 14:56:58 +0300118 finish(9)
Roman Elizarov1216e912017-02-22 09:57:06 +0300119 }
120
Roman Elizarov8b38fa22017-09-27 17:44:31 +0300121 @Test
122 fun testSelectCancel() = runTest(
123 expected = { it is JobCancellationException }
124 ) {
125 expect(1)
126 val d = CompletableDeferred<String>()
127 launch (coroutineContext) {
128 finish(3)
129 d.cancel() // will cancel after select starts
130 }
131 expect(2)
132 select<Unit> {
133 d.onAwait {
134 expectUnreached() // will not select
135 }
136 }
137 expectUnreached()
138 }
139
Roman Elizarov1216e912017-02-22 09:57:06 +0300140}