Roman Elizarov | 8bd5254 | 2017-02-14 15:51: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 | |
Roman Elizarov | f8fc478 | 2017-02-22 10:35:08 +0300 | [diff] [blame] | 17 | package kotlinx.coroutines.experimental.sync |
Roman Elizarov | 8bd5254 | 2017-02-14 15:51:58 +0300 | [diff] [blame] | 18 | |
Roman Elizarov | f8fc478 | 2017-02-22 10:35:08 +0300 | [diff] [blame] | 19 | import kotlinx.coroutines.experimental.* |
Roman Elizarov | aa461cf | 2018-04-11 13:20:29 +0300 | [diff] [blame] | 20 | import kotlin.test.* |
Roman Elizarov | 9fe5f46 | 2018-02-21 19:05:52 +0300 | [diff] [blame] | 21 | import kotlin.coroutines.experimental.* |
Roman Elizarov | 8bd5254 | 2017-02-14 15:51:58 +0300 | [diff] [blame] | 22 | |
| 23 | class MutexTest : TestBase() { |
| 24 | @Test |
Roman Elizarov | aa461cf | 2018-04-11 13:20:29 +0300 | [diff] [blame] | 25 | fun testSimple() = runTest { |
Roman Elizarov | 8bd5254 | 2017-02-14 15:51:58 +0300 | [diff] [blame] | 26 | val mutex = Mutex() |
| 27 | expect(1) |
Roman Elizarov | 43e3af7 | 2017-07-21 16:01:31 +0300 | [diff] [blame] | 28 | launch(coroutineContext) { |
Roman Elizarov | 8bd5254 | 2017-02-14 15:51:58 +0300 | [diff] [blame] | 29 | expect(4) |
| 30 | mutex.lock() // suspends |
| 31 | expect(7) // now got lock |
| 32 | mutex.unlock() |
| 33 | expect(8) |
| 34 | } |
| 35 | expect(2) |
| 36 | mutex.lock() // locked |
| 37 | expect(3) |
| 38 | yield() // yield to child |
| 39 | expect(5) |
| 40 | mutex.unlock() |
| 41 | expect(6) |
| 42 | yield() // now child has lock |
| 43 | finish(9) |
| 44 | } |
| 45 | |
| 46 | @Test |
| 47 | fun tryLockTest() { |
| 48 | val mutex = Mutex() |
Roman Elizarov | 33ecdca | 2017-02-15 10:53:56 +0300 | [diff] [blame] | 49 | assertFalse(mutex.isLocked) |
Roman Elizarov | 8bd5254 | 2017-02-14 15:51:58 +0300 | [diff] [blame] | 50 | assertTrue(mutex.tryLock()) |
Roman Elizarov | 33ecdca | 2017-02-15 10:53:56 +0300 | [diff] [blame] | 51 | assertTrue(mutex.isLocked) |
Roman Elizarov | 8bd5254 | 2017-02-14 15:51:58 +0300 | [diff] [blame] | 52 | assertFalse(mutex.tryLock()) |
Roman Elizarov | 33ecdca | 2017-02-15 10:53:56 +0300 | [diff] [blame] | 53 | assertTrue(mutex.isLocked) |
Roman Elizarov | 8bd5254 | 2017-02-14 15:51:58 +0300 | [diff] [blame] | 54 | mutex.unlock() |
Roman Elizarov | 33ecdca | 2017-02-15 10:53:56 +0300 | [diff] [blame] | 55 | assertFalse(mutex.isLocked) |
Roman Elizarov | 8bd5254 | 2017-02-14 15:51:58 +0300 | [diff] [blame] | 56 | assertTrue(mutex.tryLock()) |
Roman Elizarov | 33ecdca | 2017-02-15 10:53:56 +0300 | [diff] [blame] | 57 | assertTrue(mutex.isLocked) |
Roman Elizarov | 8bd5254 | 2017-02-14 15:51:58 +0300 | [diff] [blame] | 58 | assertFalse(mutex.tryLock()) |
Roman Elizarov | 33ecdca | 2017-02-15 10:53:56 +0300 | [diff] [blame] | 59 | assertTrue(mutex.isLocked) |
Roman Elizarov | 8bd5254 | 2017-02-14 15:51:58 +0300 | [diff] [blame] | 60 | mutex.unlock() |
Roman Elizarov | 33ecdca | 2017-02-15 10:53:56 +0300 | [diff] [blame] | 61 | assertFalse(mutex.isLocked) |
Roman Elizarov | 8bd5254 | 2017-02-14 15:51:58 +0300 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | @Test |
Roman Elizarov | aa461cf | 2018-04-11 13:20:29 +0300 | [diff] [blame] | 65 | fun withLockTest() = runTest { |
Roman Elizarov | c02ee11 | 2017-04-19 12:57:38 +0300 | [diff] [blame] | 66 | val mutex = Mutex() |
| 67 | assertFalse(mutex.isLocked) |
| 68 | mutex.withLock { |
| 69 | assertTrue(mutex.isLocked) |
| 70 | } |
| 71 | assertFalse(mutex.isLocked) |
| 72 | } |
| 73 | |
| 74 | @Test |
Roman Elizarov | 11c140a | 2017-07-21 21:12:55 +0300 | [diff] [blame] | 75 | fun testUnconfinedStackOverflow() { |
| 76 | val waiters = 10000 |
| 77 | val mutex = Mutex(true) |
| 78 | var done = 0 |
| 79 | repeat(waiters) { |
| 80 | launch(Unconfined) { // a lot of unconfined waiters |
| 81 | mutex.withLock { |
| 82 | done++ |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | mutex.unlock() // should not produce StackOverflowError |
Roman Elizarov | aa461cf | 2018-04-11 13:20:29 +0300 | [diff] [blame] | 87 | assertEquals(waiters, done) |
Roman Elizarov | 11c140a | 2017-07-21 21:12:55 +0300 | [diff] [blame] | 88 | } |
Francesco Vasco | 14328d1 | 2017-07-26 15:31:15 +0200 | [diff] [blame] | 89 | |
| 90 | @Test |
Roman Elizarov | aa461cf | 2018-04-11 13:20:29 +0300 | [diff] [blame] | 91 | fun holdLock() = runTest { |
Francesco Vasco | 14328d1 | 2017-07-26 15:31:15 +0200 | [diff] [blame] | 92 | val mutex = Mutex() |
| 93 | val firstOwner = Any() |
| 94 | val secondOwner = Any() |
| 95 | |
| 96 | // no lock |
| 97 | assertFalse(mutex.holdsLock(firstOwner)) |
| 98 | assertFalse(mutex.holdsLock(secondOwner)) |
| 99 | |
| 100 | // owner firstOwner |
| 101 | mutex.lock(firstOwner) |
Roman Elizarov | aa461cf | 2018-04-11 13:20:29 +0300 | [diff] [blame] | 102 | val secondLockJob = launch { |
Francesco Vasco | 14328d1 | 2017-07-26 15:31:15 +0200 | [diff] [blame] | 103 | mutex.lock(secondOwner) |
| 104 | } |
| 105 | |
| 106 | assertTrue(mutex.holdsLock(firstOwner)) |
| 107 | assertFalse(mutex.holdsLock(secondOwner)) |
| 108 | |
| 109 | // owner secondOwner |
| 110 | mutex.unlock(firstOwner) |
| 111 | secondLockJob.join() |
| 112 | |
| 113 | assertFalse(mutex.holdsLock(firstOwner)) |
| 114 | assertTrue(mutex.holdsLock(secondOwner)) |
| 115 | |
| 116 | mutex.unlock(secondOwner) |
| 117 | |
| 118 | // no lock |
| 119 | assertFalse(mutex.holdsLock(firstOwner)) |
| 120 | assertFalse(mutex.holdsLock(secondOwner)) |
| 121 | } |
Roman Elizarov | 8bd5254 | 2017-02-14 15:51:58 +0300 | [diff] [blame] | 122 | } |