-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathMutexStressTest.kt
134 lines (123 loc) · 4.36 KB
/
MutexStressTest.kt
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package kotlinx.coroutines.sync
import kotlinx.coroutines.testing.*
import kotlinx.coroutines.*
import kotlinx.coroutines.exceptions.*
import kotlinx.coroutines.selects.*
import kotlin.test.*
class MutexStressTest : TestBase() {
private val n = 1000 * stressTestMultiplier // It mostly stresses K/N as JVM Mutex is tested by lincheck
@Test
fun testDefaultDispatcher() = runTest { testBody(Dispatchers.Default) }
@Test
fun testSingleThreadContext() = runTest {
newSingleThreadContext("testSingleThreadContext").use {
testBody(it)
}
}
@Test
fun testMultiThreadedContextWithSingleWorker() = runTest {
newFixedThreadPoolContext(1, "testMultiThreadedContextWithSingleWorker").use {
testBody(it)
}
}
@Test
fun testMultiThreadedContext() = runTest {
newFixedThreadPoolContext(8, "testMultiThreadedContext").use {
testBody(it)
}
}
@Suppress("SuspendFunctionOnCoroutineScope")
private suspend fun CoroutineScope.testBody(dispatcher: CoroutineDispatcher) {
val k = 100
var shared = 0
val mutex = Mutex()
val jobs = List(n) {
launch(dispatcher) {
repeat(k) {
mutex.lock()
shared++
mutex.unlock()
}
}
}
jobs.forEach { it.join() }
assertEquals(n * k, shared)
}
@Test
fun stressUnlockCancelRace() = runTest {
val n = 10_000 * stressTestMultiplier
val mutex = Mutex(true) // create a locked mutex
newSingleThreadContext("SemaphoreStressTest").use { pool ->
repeat(n) {
// Initially, we hold the lock and no one else can `lock`,
// otherwise it's a bug.
assertTrue(mutex.isLocked)
var job1EnteredCriticalSection = false
val job1 = launch(start = CoroutineStart.UNDISPATCHED) {
mutex.lock()
job1EnteredCriticalSection = true
mutex.unlock()
}
// check that `job1` didn't finish the call to `acquire()`
assertEquals(false, job1EnteredCriticalSection)
val job2 = launch(pool) {
mutex.unlock()
}
// Because `job2` executes in a separate thread, this
// cancellation races with the call to `unlock()`.
job1.cancelAndJoin()
job2.join()
assertFalse(mutex.isLocked)
mutex.lock()
}
}
}
@Test
fun stressUnlockCancelRaceWithSelect() = runTest {
val n = 10_000 * stressTestMultiplier
val mutex = Mutex(true) // create a locked mutex
newSingleThreadContext("SemaphoreStressTest").use { pool ->
repeat(n) {
// Initially, we hold the lock and no one else can `lock`,
// otherwise it's a bug.
assertTrue(mutex.isLocked)
var job1EnteredCriticalSection = false
val job1 = launch(start = CoroutineStart.UNDISPATCHED) {
select<Unit> {
mutex.onLock {
job1EnteredCriticalSection = true
mutex.unlock()
}
}
}
// check that `job1` didn't finish the call to `acquire()`
assertEquals(false, job1EnteredCriticalSection)
val job2 = launch(pool) {
mutex.unlock()
}
// Because `job2` executes in a separate thread, this
// cancellation races with the call to `unlock()`.
job1.cancelAndJoin()
job2.join()
assertFalse(mutex.isLocked)
mutex.lock()
}
}
}
@Test
fun testShouldBeUnlockedOnCancellation() = runTest {
val mutex = Mutex()
val n = 1000 * stressTestMultiplier
repeat(n) {
val job = launch(Dispatchers.Default) {
mutex.lock()
mutex.unlock()
}
mutex.withLock {
job.cancel()
}
job.join()
assertFalse { mutex.isLocked }
}
}
}