-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathThreadSafeHeapStressTest.kt
66 lines (60 loc) · 1.98 KB
/
ThreadSafeHeapStressTest.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
/*
* Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/
package kotlinx.coroutines.internal
import kotlinx.coroutines.*
import java.util.concurrent.*
import java.util.concurrent.CancellationException
import kotlin.test.*
class ThreadSafeHeapStressTest : TestBase() {
private class DisposableNode : EventLoopImplBase.DelayedTask(1L) {
override fun run() {
}
}
@Test
fun testConcurrentRemoveDispose() = runTest {
val heap = EventLoopImplBase.DelayedTaskQueue(1)
repeat(10_000 * stressTestMultiplierSqrt) {
withContext(Dispatchers.Default) {
val node = DisposableNode()
val barrier = CyclicBarrier(2)
launch {
heap.addLast(node)
barrier.await()
heap.remove(node)
}
launch {
barrier.await()
Thread.yield()
node.dispose()
}
}
}
}
@Test()
fun testConcurrentAddDispose() = runTest {
repeat(10_000 * stressTestMultiplierSqrt) {
val jobToCancel = Job()
val barrier = CyclicBarrier(2)
val jobToJoin = launch(Dispatchers.Default) {
barrier.await()
jobToCancel.cancelAndJoin()
}
try {
runBlocking { // Use event loop impl
withContext(jobToCancel) {
// This one is to work around heap allocation optimization
launch(start = CoroutineStart.UNDISPATCHED) {
delay(100_000)
}
barrier.await()
delay(100_000)
}
}
} catch (e: CancellationException) {
// Expected exception
}
jobToJoin.join()
}
}
}