blob: f2204dcb92a35874c24b569116c52eee82ab50fc [file] [log] [blame]
/*
* Copyright 2016-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package kotlinx.coroutines.experimental.selects
import kotlinx.coroutines.experimental.*
import kotlinx.coroutines.experimental.sync.*
import kotlin.coroutines.experimental.*
import kotlin.test.*
class SelectMutexTest : TestBase() {
@Test
fun testSelectLock() = runTest {
val mutex = Mutex()
expect(1)
launch(coroutineContext) { // ensure that it is not scheduled earlier than needed
finish(4) // after main exits
}
val res = select<String> {
mutex.onLock {
assertTrue(mutex.isLocked)
expect(2)
"OK"
}
}
assertEquals("OK", res)
expect(3)
// will wait for the first coroutine
}
@Test
fun testSelectLockWait() = runTest {
val mutex = Mutex(true) // locked
expect(1)
launch(coroutineContext) {
expect(3)
val res = select<String> {
// will suspended
mutex.onLock {
assertTrue(mutex.isLocked)
expect(6)
"OK"
}
}
assertEquals("OK", res)
expect(7)
}
expect(2)
yield() // to launched coroutine
expect(4)
mutex.unlock()
expect(5)
yield() // to resumed select
finish(8)
}
}