Roman Elizarov | 174c696 | 2017-02-28 17:36:51 +0300 | [diff] [blame] | 1 | /* |
Roman Elizarov | 1f74a2d | 2018-06-29 19:19:45 +0300 | [diff] [blame^] | 2 | * Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. |
Roman Elizarov | 174c696 | 2017-02-28 17:36:51 +0300 | [diff] [blame] | 3 | */ |
| 4 | |
| 5 | package kotlinx.coroutines.experimental.selects |
| 6 | |
Roman Elizarov | 9fe5f46 | 2018-02-21 19:05:52 +0300 | [diff] [blame] | 7 | import kotlinx.coroutines.experimental.* |
| 8 | import kotlinx.coroutines.experimental.sync.* |
Roman Elizarov | 9fe5f46 | 2018-02-21 19:05:52 +0300 | [diff] [blame] | 9 | import kotlin.coroutines.experimental.* |
Roman Elizarov | aa461cf | 2018-04-11 13:20:29 +0300 | [diff] [blame] | 10 | import kotlin.test.* |
Roman Elizarov | 174c696 | 2017-02-28 17:36:51 +0300 | [diff] [blame] | 11 | |
| 12 | class SelectMutexTest : TestBase() { |
| 13 | @Test |
Roman Elizarov | 8b38fa2 | 2017-09-27 17:44:31 +0300 | [diff] [blame] | 14 | fun testSelectLock() = runTest { |
Roman Elizarov | 174c696 | 2017-02-28 17:36:51 +0300 | [diff] [blame] | 15 | val mutex = Mutex() |
| 16 | expect(1) |
Roman Elizarov | 43e3af7 | 2017-07-21 16:01:31 +0300 | [diff] [blame] | 17 | launch(coroutineContext) { // ensure that it is not scheduled earlier than needed |
Roman Elizarov | 8b38fa2 | 2017-09-27 17:44:31 +0300 | [diff] [blame] | 18 | finish(4) // after main exits |
Roman Elizarov | 174c696 | 2017-02-28 17:36:51 +0300 | [diff] [blame] | 19 | } |
| 20 | val res = select<String> { |
| 21 | mutex.onLock { |
| 22 | assertTrue(mutex.isLocked) |
| 23 | expect(2) |
| 24 | "OK" |
| 25 | } |
| 26 | } |
| 27 | assertEquals("OK", res) |
Roman Elizarov | 8b38fa2 | 2017-09-27 17:44:31 +0300 | [diff] [blame] | 28 | expect(3) |
| 29 | // will wait for the first coroutine |
Roman Elizarov | 174c696 | 2017-02-28 17:36:51 +0300 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | @Test |
Roman Elizarov | 8b38fa2 | 2017-09-27 17:44:31 +0300 | [diff] [blame] | 33 | fun testSelectLockWait() = runTest { |
Roman Elizarov | 174c696 | 2017-02-28 17:36:51 +0300 | [diff] [blame] | 34 | val mutex = Mutex(true) // locked |
| 35 | expect(1) |
Roman Elizarov | 43e3af7 | 2017-07-21 16:01:31 +0300 | [diff] [blame] | 36 | launch(coroutineContext) { |
Roman Elizarov | 174c696 | 2017-02-28 17:36:51 +0300 | [diff] [blame] | 37 | expect(3) |
Roman Elizarov | aa461cf | 2018-04-11 13:20:29 +0300 | [diff] [blame] | 38 | val res = select<String> { |
| 39 | // will suspended |
Roman Elizarov | 174c696 | 2017-02-28 17:36:51 +0300 | [diff] [blame] | 40 | mutex.onLock { |
| 41 | assertTrue(mutex.isLocked) |
| 42 | expect(6) |
| 43 | "OK" |
| 44 | } |
| 45 | } |
| 46 | assertEquals("OK", res) |
| 47 | expect(7) |
| 48 | } |
| 49 | expect(2) |
| 50 | yield() // to launched coroutine |
| 51 | expect(4) |
| 52 | mutex.unlock() |
| 53 | expect(5) |
| 54 | yield() // to resumed select |
| 55 | finish(8) |
| 56 | } |
Roman Elizarov | 174c696 | 2017-02-28 17:36:51 +0300 | [diff] [blame] | 57 | } |