Roman Elizarov | d84dbc2 | 2017-02-22 14:56:58 +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.selects |
| 18 | |
| 19 | import kotlinx.coroutines.experimental.* |
Roman Elizarov | 9fe5f46 | 2018-02-21 19:05:52 +0300 | [diff] [blame] | 20 | import kotlin.coroutines.experimental.* |
Roman Elizarov | aa461cf | 2018-04-11 13:20:29 +0300 | [diff] [blame] | 21 | import kotlin.test.* |
Roman Elizarov | d84dbc2 | 2017-02-22 14:56:58 +0300 | [diff] [blame] | 22 | |
| 23 | class SelectJobTest : TestBase() { |
| 24 | @Test |
Roman Elizarov | 8b38fa2 | 2017-09-27 17:44:31 +0300 | [diff] [blame] | 25 | fun testSelectCompleted() = runTest { |
Roman Elizarov | d84dbc2 | 2017-02-22 14:56:58 +0300 | [diff] [blame] | 26 | expect(1) |
Roman Elizarov | 43e3af7 | 2017-07-21 16:01:31 +0300 | [diff] [blame] | 27 | launch(coroutineContext) { // makes sure we don't yield to it earlier |
Roman Elizarov | 8b38fa2 | 2017-09-27 17:44:31 +0300 | [diff] [blame] | 28 | finish(4) // after main exits |
Roman Elizarov | d84dbc2 | 2017-02-22 14:56:58 +0300 | [diff] [blame] | 29 | } |
| 30 | val job = Job() |
| 31 | job.cancel() |
| 32 | select<Unit> { |
| 33 | job.onJoin { |
| 34 | expect(2) |
| 35 | } |
| 36 | } |
Roman Elizarov | 8b38fa2 | 2017-09-27 17:44:31 +0300 | [diff] [blame] | 37 | expect(3) |
| 38 | // will wait for the first coroutine |
Roman Elizarov | d84dbc2 | 2017-02-22 14:56:58 +0300 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | @Test |
Roman Elizarov | 8b38fa2 | 2017-09-27 17:44:31 +0300 | [diff] [blame] | 42 | fun testSelectIncomplete() = runTest { |
Roman Elizarov | d84dbc2 | 2017-02-22 14:56:58 +0300 | [diff] [blame] | 43 | expect(1) |
| 44 | val job = Job() |
Roman Elizarov | 43e3af7 | 2017-07-21 16:01:31 +0300 | [diff] [blame] | 45 | launch(coroutineContext) { // makes sure we don't yield to it earlier |
Roman Elizarov | d84dbc2 | 2017-02-22 14:56:58 +0300 | [diff] [blame] | 46 | 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 Elizarov | 8b38fa2 | 2017-09-27 17:44:31 +0300 | [diff] [blame] | 66 | fun testSelectLazy() = runTest { |
Roman Elizarov | d84dbc2 | 2017-02-22 14:56:58 +0300 | [diff] [blame] | 67 | expect(1) |
Roman Elizarov | 43e3af7 | 2017-07-21 16:01:31 +0300 | [diff] [blame] | 68 | val job = launch(coroutineContext, CoroutineStart.LAZY) { |
Roman Elizarov | d84dbc2 | 2017-02-22 14:56:58 +0300 | [diff] [blame] | 69 | 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 | } |