Skip to content

Commit 7c49d4b

Browse files
committed
Add Semaphore and Mutex linearizability tests
Fixes #1737
1 parent d75d02a commit 7c49d4b

File tree

4 files changed

+68
-1
lines changed

4 files changed

+68
-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.10.9
1818
reactor_vesion=3.2.5.RELEASE

kotlinx-coroutines-core/build.gradle

+2
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ task jvmStressTest(type: Test, dependsOn: compileTestKotlinJvm) {
101101
enableAssertions = true
102102
testLogging.showStandardStreams = true
103103
systemProperty 'kotlinx.coroutines.scheduler.keep.alive.sec', '100000' // any unpark problem hangs test
104+
systemProperty 'kotlinx.coroutines.semaphore.segmentSize', '2'
105+
systemProperty 'kotlinx.coroutines.semaphore.maxSpinCycles', '10'
104106
}
105107

106108
task jdk16Test(type: Test, dependsOn: [compileTestKotlinJvm, checkJdk16]) {
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,34 @@
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
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)

0 commit comments

Comments
 (0)