Skip to content

Commit 1e9ed89

Browse files
committed
Update ChannelUndeliveredElementSelectOldStressTest
1 parent b94a828 commit 1e9ed89

File tree

1 file changed

+32
-7
lines changed

1 file changed

+32
-7
lines changed

kotlinx-coroutines-core/jvm/test/channels/ChannelUndeliveredElementSelectOldStressTest.kt

+32-7
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ class ChannelUndeliveredElementSelectOldStressTest(private val kind: TestChannel
5252

5353
private val modulo = 1 shl 25
5454
private val mask = (modulo - 1).toLong()
55+
private val sentStatus = ItemStatus() // 1 - send norm, 2 - send select, +2 - did not throw exception
56+
private val receivedStatus = ItemStatus() // 1-6 received
5557
private val failedStatus = ItemStatus() // 1 - failed
5658

5759
lateinit var sender: Job
@@ -111,8 +113,11 @@ class ChannelUndeliveredElementSelectOldStressTest(private val kind: TestChannel
111113
assertEquals(sentCnt - failedToDeliverCnt.value, receivedCnt)
112114
} catch (e: Throwable) {
113115
printProgressSummary(iteration)
116+
printErrorDetails()
114117
throw e
115118
}
119+
sentStatus.clear()
120+
receivedStatus.clear()
116121
failedStatus.clear()
117122
}
118123

@@ -126,15 +131,34 @@ class ChannelUndeliveredElementSelectOldStressTest(private val kind: TestChannel
126131
println(" Duplicated $dupCnt deliveries")
127132
}
128133

134+
private fun printErrorDetails() {
135+
val min = minOf(sentStatus.min, receivedStatus.min, failedStatus.min)
136+
val max = maxOf(sentStatus.max, receivedStatus.max, failedStatus.max)
137+
for (x in min..max) {
138+
val sentCnt = if (sentStatus[x] != 0) 1 else 0
139+
val receivedCnt = if (receivedStatus[x] != 0) 1 else 0
140+
val failedToDeliverCnt = failedStatus[x]
141+
if (sentCnt - failedToDeliverCnt != receivedCnt) {
142+
println("!!! Error for value $x: " +
143+
"sentStatus=${sentStatus[x]}, " +
144+
"receivedStatus=${receivedStatus[x]}, " +
145+
"failedStatus=${failedStatus[x]}"
146+
)
147+
}
148+
}
149+
}
150+
151+
129152
private fun launchSender() {
130153
sender = scope.launch(start = CoroutineStart.ATOMIC) {
131154
cancellable(senderDone) {
132155
var counter = 0
133156
while (true) {
134157
val trySendData = Data(sentCnt++)
135-
selectOld<Unit> {
136-
channel.onSend(trySendData) {}
137-
}
158+
val sendMode = Random.nextInt(2) + 1
159+
sentStatus[trySendData.x] = sendMode
160+
selectOld<Unit> { channel.onSend(trySendData) {} }
161+
sentStatus[trySendData.x] = sendMode + 2
138162
when {
139163
// must artificially slow down LINKED_LIST sender to avoid overwhelming receiver and going OOM
140164
kind == TestChannelKind.LINKED_LIST -> while (sentCnt > lastReceived + 100) yield()
@@ -156,14 +180,15 @@ class ChannelUndeliveredElementSelectOldStressTest(private val kind: TestChannel
156180
receiver = scope.launch(start = CoroutineStart.ATOMIC) {
157181
cancellable(receiverDone) {
158182
while (true) {
159-
selectOld<Unit> {
160-
channel.onReceive {
161-
it.onReceived()
183+
selectOld<Unit> {
184+
channel.onReceive { receivedData ->
185+
receivedData.onReceived()
162186
receivedCnt++
163-
val received = it.x
187+
val received = receivedData.x
164188
if (received <= lastReceived)
165189
dupCnt++
166190
lastReceived = received
191+
receivedStatus[received] = 1
167192
}
168193
}
169194
}

0 commit comments

Comments
 (0)