Roman Elizarov | 174c696 | 2017-02-28 17:36:51 +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 | |
Roman Elizarov | 9fe5f46 | 2018-02-21 19:05:52 +0300 | [diff] [blame] | 19 | import kotlinx.coroutines.experimental.* |
| 20 | import kotlinx.coroutines.experimental.sync.* |
Roman Elizarov | 9fe5f46 | 2018-02-21 19:05:52 +0300 | [diff] [blame] | 21 | import kotlin.coroutines.experimental.* |
Roman Elizarov | aa461cf | 2018-04-11 13:20:29 +0300 | [diff] [blame] | 22 | import kotlin.test.* |
Roman Elizarov | 174c696 | 2017-02-28 17:36:51 +0300 | [diff] [blame] | 23 | |
| 24 | class SelectMutexTest : TestBase() { |
| 25 | @Test |
Roman Elizarov | 8b38fa2 | 2017-09-27 17:44:31 +0300 | [diff] [blame] | 26 | fun testSelectLock() = runTest { |
Roman Elizarov | 174c696 | 2017-02-28 17:36:51 +0300 | [diff] [blame] | 27 | val mutex = Mutex() |
| 28 | expect(1) |
Roman Elizarov | 43e3af7 | 2017-07-21 16:01:31 +0300 | [diff] [blame] | 29 | launch(coroutineContext) { // ensure that it is not scheduled earlier than needed |
Roman Elizarov | 8b38fa2 | 2017-09-27 17:44:31 +0300 | [diff] [blame] | 30 | finish(4) // after main exits |
Roman Elizarov | 174c696 | 2017-02-28 17:36:51 +0300 | [diff] [blame] | 31 | } |
| 32 | val res = select<String> { |
| 33 | mutex.onLock { |
| 34 | assertTrue(mutex.isLocked) |
| 35 | expect(2) |
| 36 | "OK" |
| 37 | } |
| 38 | } |
| 39 | assertEquals("OK", res) |
Roman Elizarov | 8b38fa2 | 2017-09-27 17:44:31 +0300 | [diff] [blame] | 40 | expect(3) |
| 41 | // will wait for the first coroutine |
Roman Elizarov | 174c696 | 2017-02-28 17:36:51 +0300 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | @Test |
Roman Elizarov | 8b38fa2 | 2017-09-27 17:44:31 +0300 | [diff] [blame] | 45 | fun testSelectLockWait() = runTest { |
Roman Elizarov | 174c696 | 2017-02-28 17:36:51 +0300 | [diff] [blame] | 46 | val mutex = Mutex(true) // locked |
| 47 | expect(1) |
Roman Elizarov | 43e3af7 | 2017-07-21 16:01:31 +0300 | [diff] [blame] | 48 | launch(coroutineContext) { |
Roman Elizarov | 174c696 | 2017-02-28 17:36:51 +0300 | [diff] [blame] | 49 | expect(3) |
Roman Elizarov | aa461cf | 2018-04-11 13:20:29 +0300 | [diff] [blame] | 50 | val res = select<String> { |
| 51 | // will suspended |
Roman Elizarov | 174c696 | 2017-02-28 17:36:51 +0300 | [diff] [blame] | 52 | mutex.onLock { |
| 53 | assertTrue(mutex.isLocked) |
| 54 | expect(6) |
| 55 | "OK" |
| 56 | } |
| 57 | } |
| 58 | assertEquals("OK", res) |
| 59 | expect(7) |
| 60 | } |
| 61 | expect(2) |
| 62 | yield() // to launched coroutine |
| 63 | expect(4) |
| 64 | mutex.unlock() |
| 65 | expect(5) |
| 66 | yield() // to resumed select |
| 67 | finish(8) |
| 68 | } |
Roman Elizarov | 174c696 | 2017-02-28 17:36:51 +0300 | [diff] [blame] | 69 | } |