-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2854 from Kotlin/concurrent-test
Sharing concurrent tests for jvm and native
- Loading branch information
Showing
14 changed files
with
114 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
kotlinx-coroutines-core/concurrent/test/channels/BroadcastChannelSubStressTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* Copyright 2016-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package kotlinx.coroutines.channels | ||
|
||
import kotlinx.atomicfu.* | ||
import kotlinx.coroutines.* | ||
import kotlin.test.* | ||
|
||
/** | ||
* Creates a broadcast channel and repeatedly opens new subscription, receives event, closes it, | ||
* to stress test the logic of opening the subscription | ||
* to broadcast channel while events are being concurrently sent to it. | ||
*/ | ||
class BroadcastChannelSubStressTest: TestBase() { | ||
|
||
private val nSeconds = 5 * stressTestMultiplier | ||
private val sentTotal = atomic(0L) | ||
private val receivedTotal = atomic(0L) | ||
|
||
@Test | ||
fun testStress() = runTest { | ||
TestBroadcastChannelKind.values().forEach { kind -> | ||
println("--- BroadcastChannelSubStressTest $kind") | ||
val broadcast = kind.create<Long>() | ||
val sender = | ||
launch(context = Dispatchers.Default + CoroutineName("Sender")) { | ||
while (isActive) { | ||
broadcast.send(sentTotal.incrementAndGet()) | ||
} | ||
} | ||
val receiver = | ||
launch(context = Dispatchers.Default + CoroutineName("Receiver")) { | ||
var last = -1L | ||
while (isActive) { | ||
val channel = broadcast.openSubscription() | ||
val i = channel.receive() | ||
check(i >= last) { "Last was $last, got $i" } | ||
if (!kind.isConflated) check(i != last) { "Last was $last, got it again" } | ||
receivedTotal.incrementAndGet() | ||
last = i | ||
channel.cancel() | ||
} | ||
} | ||
var prevSent = -1L | ||
repeat(nSeconds) { sec -> | ||
delay(1000) | ||
val curSent = sentTotal.value | ||
println("${sec + 1}: Sent $curSent, received ${receivedTotal.value}") | ||
check(curSent > prevSent) { "Send stalled at $curSent events" } | ||
prevSent = curSent | ||
} | ||
withTimeout(5000) { | ||
sender.cancelAndJoin() | ||
receiver.cancelAndJoin() | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
...st/channels/RandevouzChannelStressTest.kt → ...st/channels/RandevouzChannelStressTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 3 additions & 3 deletions
6
...s-core/jvm/test/flow/CombineStressTest.kt → ...concurrent/test/flow/CombineStressTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 2 additions & 1 deletion
3
...ore/jvm/test/flow/FlowCancellationTest.kt → ...current/test/flow/FlowCancellationTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
...m/test/internal/LockFreeLinkedListTest.kt → ...t/test/internal/LockFreeLinkedListTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...jvm/test/selects/SelectMutexStressTest.kt → ...ent/test/selects/SelectMutexStressTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.