@@ -10,21 +10,49 @@ import org.junit.*
10
10
import org.junit.Assert.*
11
11
import org.junit.runner.*
12
12
import org.junit.runners.*
13
- import kotlin.random.Random
14
13
import java.util.concurrent.atomic.*
14
+ import kotlin.random.*
15
+
16
+ private const val DIRECT = 1
17
+ private const val SELECT = 2
15
18
16
19
/* *
17
20
* Tests cancel atomicity for channel send & receive operations, including their select versions.
18
21
*/
19
22
@RunWith(Parameterized ::class )
20
- class ChannelAtomicCancelStressTest (private val kind : TestChannelKind ) : TestBase() {
23
+ class ChannelAtomicCancelStressTest (
24
+ private val kind : TestChannelKind ,
25
+ private val mode : Mode
26
+ ) : TestBase() {
27
+
28
+ enum class Mode (val send : Int , val receive : Int ) {
29
+ SEND_RECEIVE (DIRECT , DIRECT ),
30
+ SELECT_SEND_RECEIVE (SELECT , DIRECT ),
31
+ SEND_SELECT_RECEIVE (DIRECT , SELECT ),
32
+ SELECT_SEND_SELECT_RECEIVE (SELECT , SELECT ),
33
+ RANDOM (DIRECT or SELECT , DIRECT or SELECT )
34
+ }
35
+
36
+ private fun Random.nextOp (mask : Int ): Int =
37
+ when (mask) {
38
+ DIRECT or SELECT -> nextInt(DIRECT .. SELECT )
39
+ else -> mask
40
+ }
41
+
21
42
companion object {
22
- @Parameterized.Parameters (name = " {0}" )
43
+ @Parameterized.Parameters (name = " {0}, {1} " )
23
44
@JvmStatic
24
- fun params (): Collection <Array <Any >> = TestChannelKind .values().map { arrayOf<Any >(it) }
45
+ fun params (): Collection <Array <Any >> =
46
+ // todo: all channels, all modes only in stress test
47
+ listOf (TestChannelKind .RENDEZVOUS ).flatMap { kind ->
48
+ Mode .values().map { mode ->
49
+ arrayOf<Any >(kind, mode)
50
+ }
51
+ }
25
52
}
26
53
27
- private val TEST_DURATION = 1000L * stressTestMultiplier
54
+ // todo: restore regular duration
55
+ private val TEST_DURATION = 10_000L * stressTestMultiplier
28
56
29
57
private val dispatcher = newFixedThreadPoolContext(2 , " ChannelAtomicCancelStressTest" )
30
58
private val scope = CoroutineScope (dispatcher)
@@ -104,9 +132,9 @@ class ChannelAtomicCancelStressTest(private val kind: TestChannelKind) : TestBas
104
132
var counter = 0
105
133
while (true ) {
106
134
val trySend = lastSent + 1
107
- when (Random .nextInt( 2 )) {
108
- 0 -> channel.send(trySend)
109
- 1 -> select { channel.onSend(trySend) {} }
135
+ when (Random .nextOp(mode.send )) {
136
+ DIRECT -> channel.send(trySend)
137
+ SELECT -> select { channel.onSend(trySend) {} }
110
138
else -> error(" cannot happen" )
111
139
}
112
140
lastSent = trySend // update on success
@@ -131,9 +159,9 @@ class ChannelAtomicCancelStressTest(private val kind: TestChannelKind) : TestBas
131
159
receiver = scope.launch(start = CoroutineStart .ATOMIC ) {
132
160
cancellable(receiverDone) {
133
161
while (true ) {
134
- val received = when (Random .nextInt( 2 )) {
135
- 0 -> channel.receive()
136
- 1 -> select { channel.onReceive { it } }
162
+ val received = when (Random .nextOp(mode.receive )) {
163
+ DIRECT -> channel.receive()
164
+ SELECT -> select { channel.onReceive { it } }
137
165
else -> error(" cannot happen" )
138
166
}
139
167
val expected = lastReceived + 1
0 commit comments