blob: 3ca3e07f8991316c645e2a3a4681b66ce72f61e3 [file] [log] [blame]
Roman Elizarovd84dbc22017-02-22 14:56:58 +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.selects
18
19import kotlinx.coroutines.experimental.*
Roman Elizarov9fe5f462018-02-21 19:05:52 +030020import kotlin.coroutines.experimental.*
Roman Elizarovaa461cf2018-04-11 13:20:29 +030021import kotlin.test.*
Roman Elizarovd84dbc22017-02-22 14:56:58 +030022
23class SelectJobTest : TestBase() {
24 @Test
Roman Elizarov8b38fa22017-09-27 17:44:31 +030025 fun testSelectCompleted() = runTest {
Roman Elizarovd84dbc22017-02-22 14:56:58 +030026 expect(1)
Roman Elizarov43e3af72017-07-21 16:01:31 +030027 launch(coroutineContext) { // makes sure we don't yield to it earlier
Roman Elizarov8b38fa22017-09-27 17:44:31 +030028 finish(4) // after main exits
Roman Elizarovd84dbc22017-02-22 14:56:58 +030029 }
30 val job = Job()
31 job.cancel()
32 select<Unit> {
33 job.onJoin {
34 expect(2)
35 }
36 }
Roman Elizarov8b38fa22017-09-27 17:44:31 +030037 expect(3)
38 // will wait for the first coroutine
Roman Elizarovd84dbc22017-02-22 14:56:58 +030039 }
40
41 @Test
Roman Elizarov8b38fa22017-09-27 17:44:31 +030042 fun testSelectIncomplete() = runTest {
Roman Elizarovd84dbc22017-02-22 14:56:58 +030043 expect(1)
44 val job = Job()
Roman Elizarov43e3af72017-07-21 16:01:31 +030045 launch(coroutineContext) { // makes sure we don't yield to it earlier
Roman Elizarovd84dbc22017-02-22 14:56:58 +030046 expect(3)
47 val res = select<String> {
48 job.onJoin {
49 expect(6)
50 "OK"
51 }
52 }
53 expect(7)
54 assertEquals("OK", res)
55 }
56 expect(2)
57 yield()
58 expect(4)
59 job.cancel()
60 expect(5)
61 yield()
62 finish(8)
63 }
64
65 @Test
Roman Elizarov8b38fa22017-09-27 17:44:31 +030066 fun testSelectLazy() = runTest {
Roman Elizarovd84dbc22017-02-22 14:56:58 +030067 expect(1)
Roman Elizarov43e3af72017-07-21 16:01:31 +030068 val job = launch(coroutineContext, CoroutineStart.LAZY) {
Roman Elizarovd84dbc22017-02-22 14:56:58 +030069 expect(2)
70 }
71 val res = select<String> {
72 job.onJoin {
73 expect(3)
74 "OK"
75 }
76 }
77 finish(4)
78 assertEquals("OK", res)
79 }
80}