Skip to content

Commit f526b13

Browse files
committed
More predictable behaviour for example-channel-09 and better diagnostics on test failure
1 parent 9ae9477 commit f526b13

File tree

4 files changed

+15
-10
lines changed

4 files changed

+15
-10
lines changed

coroutines-guide.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1513,7 +1513,7 @@ suspend fun player(name: String, table: Channel<Ball>) {
15131513
for (ball in table) { // receive the ball in a loop
15141514
ball.hits++
15151515
println("$name $ball")
1516-
delay(200) // wait a bit
1516+
delay(300) // wait a bit
15171517
table.send(ball) // send the ball back
15181518
}
15191519
}
@@ -1531,7 +1531,6 @@ pong Ball(hits=2)
15311531
ping Ball(hits=3)
15321532
pong Ball(hits=4)
15331533
ping Ball(hits=5)
1534-
pong Ball(hits=6)
15351534
```
15361535

15371536
<!--- TEST -->

kotlinx-coroutines-core/src/test/kotlin/guide/example-channel-09.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ suspend fun player(name: String, table: Channel<Ball>) {
3535
for (ball in table) { // receive the ball in a loop
3636
ball.hits++
3737
println("$name $ball")
38-
delay(200) // wait a bit
38+
delay(300) // wait a bit
3939
table.send(ball) // send the ball back
4040
}
4141
}

kotlinx-coroutines-core/src/test/kotlin/guide/test/GuideTest.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -341,8 +341,7 @@ class GuideTest {
341341
"pong Ball(hits=2)",
342342
"ping Ball(hits=3)",
343343
"pong Ball(hits=4)",
344-
"ping Ball(hits=5)",
345-
"pong Ball(hits=6)"
344+
"ping Ball(hits=5)"
346345
)
347346
}
348347

kotlinx-coroutines-core/src/test/kotlin/guide/test/TestUtil.kt

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,16 @@ private fun List<String>.verifyCommonLines(expected: Array<out String>, mode: Sa
9999
}
100100
}
101101

102+
private fun List<String>.checkEqualNumberOfLines(expected: Array<out String>) {
103+
if (size > expected.size)
104+
error("Expected ${expected.size} lines, but found $size. Unexpected line '${get(expected.size)}'")
105+
else if (size < expected.size)
106+
error("Expected ${expected.size} lines, but found $size")
107+
}
108+
102109
fun List<String>.verifyLines(vararg expected: String) {
103110
verifyCommonLines(expected)
104-
assertEquals("Number of lines", expected.size, size)
111+
checkEqualNumberOfLines(expected)
105112
}
106113

107114
fun List<String>.verifyLinesStartWith(vararg expected: String) {
@@ -111,17 +118,17 @@ fun List<String>.verifyLinesStartWith(vararg expected: String) {
111118

112119
fun List<String>.verifyLinesArbitraryTime(vararg expected: String) {
113120
verifyCommonLines(expected, SanitizeMode.ARBITRARY_TIME)
114-
assertEquals("Number of lines", expected.size, size)
121+
checkEqualNumberOfLines(expected)
115122
}
116123

117124
fun List<String>.verifyLinesFlexibleTime(vararg expected: String) {
118125
verifyCommonLines(expected, SanitizeMode.FLEXIBLE_TIME)
119-
assertEquals("Number of lines", expected.size, size)
126+
checkEqualNumberOfLines(expected)
120127
}
121128

122129
fun List<String>.verifyLinesFlexibleThread(vararg expected: String) {
123130
verifyCommonLines(expected, SanitizeMode.FLEXIBLE_THREAD)
124-
assertEquals("Number of lines", expected.size, size)
131+
checkEqualNumberOfLines(expected)
125132
}
126133

127134
fun List<String>.verifyLinesStartUnordered(vararg expected: String) {
@@ -136,5 +143,5 @@ fun List<String>.verifyLinesStart(vararg expected: String) {
136143
val act = sanitize(get(i), SanitizeMode.FLEXIBLE_THREAD)
137144
assertEquals("Line ${i + 1}", exp, act.substring(0, minOf(act.length, exp.length)))
138145
}
139-
assertEquals("Number of lines", expected.size, size)
146+
checkEqualNumberOfLines(expected)
140147
}

0 commit comments

Comments
 (0)