/
SharingStressTest.kt
197 lines (178 loc) · 7.35 KB
/
SharingStressTest.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
* Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/
package kotlinx.coroutines.flow
import kotlinx.coroutines.*
import org.junit.*
import org.junit.Test
import java.util.*
import java.util.concurrent.atomic.*
import kotlin.random.*
import kotlin.test.*
import kotlin.time.*
import kotlin.time.TimeSource
@OptIn(ExperimentalTime::class)
class SharingStressTest : TestBase() {
private val testDuration = 1000L * stressTestMultiplier
private val nSubscribers = 5
private val testStarted = TimeSource.Monotonic.markNow()
@get:Rule
val emitterDispatcher = ExecutorRule(1)
@get:Rule
val subscriberDispatcher = ExecutorRule(nSubscribers)
@Test
public fun testNoReplayLazy() =
testStress(0, started = SharingStarted.Lazily)
@Test
public fun testNoReplayWhileSubscribed() =
testStress(0, started = SharingStarted.WhileSubscribed())
@Test
public fun testNoReplayWhileSubscribedTimeout() =
testStress(0, started = SharingStarted.WhileSubscribed(stopTimeoutMillis = 50L))
@Test
public fun testReplay100WhileSubscribed() =
testStress(100, started = SharingStarted.WhileSubscribed())
@Test
public fun testReplay100WhileSubscribedReset() =
testStress(100, started = SharingStarted.WhileSubscribed(replayExpirationMillis = 0L))
@Test
public fun testReplay100WhileSubscribedTimeout() =
testStress(100, started = SharingStarted.WhileSubscribed(stopTimeoutMillis = 50L))
@Test
public fun testStateLazy() =
testStress(1, started = SharingStarted.Lazily)
@Test
public fun testStateWhileSubscribed() =
testStress(1, started = SharingStarted.WhileSubscribed())
@Test
public fun testStateWhileSubscribedReset() =
testStress(1, started = SharingStarted.WhileSubscribed(replayExpirationMillis = 0L))
private fun testStress(replay: Int, started: SharingStarted) = runTest {
log("-- Stress with replay=$replay, started=$started")
val random = Random(1)
val emitIndex = AtomicLong()
val cancelledEmits = HashSet<Long>()
val missingCollects = Collections.synchronizedSet(LinkedHashSet<Long>())
// at most one copy of upstream can be running at any time
val isRunning = AtomicInteger(0)
val upstream = flow {
assertEquals(0, isRunning.getAndIncrement())
try {
while (true) {
val value = emitIndex.getAndIncrement()
try {
emit(value)
} catch (e: CancellationException) {
// emission was cancelled -> could be missing
cancelledEmits.add(value)
throw e
}
}
} finally {
assertEquals(1, isRunning.getAndDecrement())
}
}
val subCount = MutableStateFlow(0)
val sharingJob = Job()
val sharingScope = this + emitterDispatcher + sharingJob
val usingStateFlow = replay == 1
val sharedFlow = if (usingStateFlow)
upstream.stateIn(sharingScope, started, 0L)
else
upstream.shareIn(sharingScope, started, replay)
try {
val subscribers = ArrayList<SubJob>()
withTimeoutOrNull(testDuration) {
// start and stop subscribers
while (true) {
log("Staring $nSubscribers subscribers")
repeat(nSubscribers) {
subscribers += launchSubscriber(sharedFlow, usingStateFlow, subCount, missingCollects)
}
// wait until they all subscribed
subCount.first { it == nSubscribers }
// let them work a bit more & make sure emitter did not hang
val fromEmitIndex = emitIndex.get()
val waitEmitIndex = fromEmitIndex + 100 // wait until 100 emitted
withTimeout(10000) { // wait for at most 10s for something to be emitted
do {
delay(random.nextLong(50L..100L))
} while (emitIndex.get() < waitEmitIndex) // Ok, enough was emitted, wait more if not
}
// Stop all subscribers and ensure they collected something
log("Stopping subscribers (emitted = ${emitIndex.get() - fromEmitIndex})")
subscribers.forEach {
it.job.cancelAndJoin()
assertTrue { it.count > 0 } // something must be collected too
}
subscribers.clear()
log("Intermission")
delay(random.nextLong(10L..100L)) // wait a bit before starting them again
}
}
if (!subscribers.isEmpty()) {
log("Stopping subscribers")
subscribers.forEach { it.job.cancelAndJoin() }
}
} finally {
log("--- Finally: Cancelling sharing job")
sharingJob.cancel()
}
sharingJob.join() // make sure sharing job did not hang
log("Emitter was cancelled ${cancelledEmits.size} times")
log("Collectors missed ${missingCollects.size} values")
for (value in missingCollects) {
assertTrue(value in cancelledEmits, "Value $value is missing for no apparent reason")
}
}
private fun CoroutineScope.launchSubscriber(
sharedFlow: SharedFlow<Long>,
usingStateFlow: Boolean,
subCount: MutableStateFlow<Int>,
missingCollects: MutableSet<Long>
): SubJob {
val subJob = SubJob()
subJob.job = launch(subscriberDispatcher) {
var last = -1L
sharedFlow
.onSubscription {
subCount.increment(1)
}
.onCompletion {
subCount.increment(-1)
}
.collect { j ->
subJob.count++
// last must grow sequentially, no jumping or losses
if (last == -1L) {
last = j
} else {
val expected = last + 1
if (usingStateFlow)
assertTrue(expected <= j)
else {
if (expected != j) {
if (j == expected + 1) {
// if missing just one -- could be race with cancelled emit
missingCollects.add(expected)
} else {
// broken otherwise
assertEquals(expected, j)
}
}
}
last = j
}
}
}
return subJob
}
private class SubJob {
lateinit var job: Job
var count = 0L
}
private fun log(msg: String) = println("${testStarted.elapsedNow().inWholeMilliseconds} ms: $msg")
private fun MutableStateFlow<Int>.increment(delta: Int) {
update { it + delta }
}
}