Skip to content

Commit da8f7c6

Browse files
committed
Add linearizability tests for Mutex and Semaphore
Fixes #1737
1 parent 6e66695 commit da8f7c6

File tree

3 files changed

+67
-1
lines changed

3 files changed

+67
-1
lines changed

gradle.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ junit_version=4.12
1212
atomicfu_version=0.14.2
1313
knit_version=0.1.3
1414
html_version=0.6.8
15-
lincheck_version=2.5.3
15+
lincheck_version=2.6
1616
dokka_version=0.9.16-rdev-2-mpp-hacks
1717
byte_buddy_version=1.9.3
1818
reactor_vesion=3.2.5.RELEASE
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
3+
*/
4+
@file:Suppress("unused")
5+
package kotlinx.coroutines.linearizability
6+
7+
import kotlinx.coroutines.*
8+
import kotlinx.coroutines.sync.*
9+
import org.jetbrains.kotlinx.lincheck.annotations.Operation
10+
import org.jetbrains.kotlinx.lincheck.verifier.*
11+
import org.junit.*
12+
13+
class MutexLCStressTest : VerifierState() {
14+
private val mutex = Mutex()
15+
16+
@Operation
17+
fun tryLock() = mutex.tryLock()
18+
19+
@Operation(cancellableOnSuspension = true)
20+
suspend fun lock() = mutex.lock()
21+
22+
@Operation(handleExceptionsAsResult = [IllegalStateException::class])
23+
fun unlock() = mutex.unlock()
24+
25+
@Test
26+
fun test() = LCStressOptionsDefault()
27+
.actorsBefore(0)
28+
.check(this::class)
29+
30+
override fun extractState() = mutex.isLocked
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
3+
*/
4+
@file:Suppress("unused")
5+
package kotlinx.coroutines.linearizability
6+
7+
import kotlinx.coroutines.*
8+
import kotlinx.coroutines.sync.*
9+
import org.jetbrains.kotlinx.lincheck.annotations.Operation
10+
import org.jetbrains.kotlinx.lincheck.verifier.*
11+
import org.junit.*
12+
13+
abstract class SemaphoreLCStressTestBase(permits: Int) : VerifierState() {
14+
private val semaphore = Semaphore(permits)
15+
16+
// @Operation
17+
fun tryAcquire() = semaphore.tryAcquire()
18+
19+
@Operation(cancellableOnSuspension = true)
20+
suspend fun acquire() = semaphore.acquire()
21+
22+
@Operation(handleExceptionsAsResult = [IllegalStateException::class])
23+
fun release() = semaphore.release()
24+
25+
@Test
26+
fun test() = LCStressOptionsDefault()
27+
.actorsBefore(0)
28+
.check(this::class)
29+
30+
override fun extractState() = semaphore.availablePermits
31+
}
32+
33+
class Semaphore1LCStressTest : SemaphoreLCStressTestBase(1)
34+
class Semaphore2LCStressTest : SemaphoreLCStressTestBase(2)
35+
class Semaphore4LCStressTest : SemaphoreLCStressTestBase(4)

0 commit comments

Comments
 (0)