-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlockingStackTest.kt
More file actions
54 lines (46 loc) · 1.75 KB
/
BlockingStackTest.kt
File metadata and controls
54 lines (46 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import org.jetbrains.kotlinx.lincheck.LinChecker
import org.jetbrains.kotlinx.lincheck.LoggingLevel.DEBUG
import org.jetbrains.kotlinx.lincheck.annotations.LogLevel
import org.jetbrains.kotlinx.lincheck.annotations.Operation
import org.jetbrains.kotlinx.lincheck.strategy.stress.StressCTest
import org.jetbrains.kotlinx.lincheck.verifier.VerifierState
import org.junit.Test
import java.util.*
import kotlin.collections.ArrayList
import kotlin.coroutines.Continuation
import kotlin.coroutines.resume
import kotlin.coroutines.suspendCoroutine
@LogLevel(DEBUG)
@StressCTest(actorsBefore = 0, threads = 3, actorsPerThread = 2, invocationsPerIteration = 20_000,
sequentialSpecification = BlockingStackSequential::class)
class BlockingStackTest : BlockingStack<Int> {
private val q = BlockingStackImpl<Int>()
@Operation(cancellableOnSuspension = false)
override fun push(element: Int) { q.push(element) }
@Operation(cancellableOnSuspension = false)
override suspend fun pop(): Int = q.pop()
@Test
fun runTest() = LinChecker.check(this::class.java)
}
class BlockingStackSequential : BlockingStack<Int>, VerifierState() {
private val elements = Stack<Int>()
private val waitingReceivers = ArrayList<Continuation<Int>>()
override fun push(element: Int) {
if (waitingReceivers.isNotEmpty()) {
val r = waitingReceivers.removeAt(0)
r.resume(element)
} else {
elements.push(element)
}
}
override suspend fun pop(): Int {
if (elements.isNotEmpty()) {
return elements.pop()
} else {
return suspendCoroutine { cont ->
waitingReceivers.add(cont)
}
}
}
override fun extractState() = elements
}