5
5
package kotlinx.coroutines.experimental.guide.test
6
6
7
7
import kotlinx.coroutines.experimental.*
8
+ import kotlinx.coroutines.experimental.internal.*
8
9
import org.junit.Assert.*
9
10
import java.io.*
10
11
import java.util.concurrent.*
@@ -21,14 +22,17 @@ private inline fun <T> outputException(name: String, block: () -> T): T =
21
22
}
22
23
23
24
private const val SHUTDOWN_TIMEOUT = 5000L // 5 sec at most to wait
25
+ private val OUT_ENABLED = systemProp(" guide.tests.sout" , false )
24
26
25
27
fun test (name : String , block : () -> Unit ): List <String > = outputException(name) {
26
- println ( " --- Running test $name " )
27
- val oldOut = System .out
28
+ val sout = System . out
29
+ val oldOut = if ( OUT_ENABLED ) System .out else NullOut
28
30
val oldErr = System .err
29
31
val bytesOut = ByteArrayOutputStream ()
30
32
val tee = TeeOutput (bytesOut, oldOut)
31
33
val ps = PrintStream (tee)
34
+
35
+ oldOut.println (" --- Running test$name " )
32
36
System .setErr(ps)
33
37
System .setOut(ps)
34
38
CommonPool .usePrivatePool()
@@ -53,7 +57,7 @@ fun test(name: String, block: () -> Unit): List<String> = outputException(name)
53
57
CommonPool .restore()
54
58
if (tee.flushLine()) oldOut.println ()
55
59
oldOut.println (" --- done" )
56
- System .setOut(oldOut )
60
+ System .setOut(sout )
57
61
System .setErr(oldErr)
58
62
checkTestThreads(threadsBefore)
59
63
}
@@ -144,37 +148,56 @@ private fun List<String>.checkEqualNumberOfLines(expected: Array<out String>) {
144
148
error(" Expected ${expected.size} lines, but found $size " )
145
149
}
146
150
147
- fun List<String>.verifyLines (vararg expected : String ) {
151
+ fun List<String>.verifyLines (vararg expected : String ) = verify {
148
152
verifyCommonLines(expected)
149
153
checkEqualNumberOfLines(expected)
150
154
}
151
155
152
- fun List<String>.verifyLinesStartWith (vararg expected : String ) {
156
+ fun List<String>.verifyLinesStartWith (vararg expected : String ) = verify {
153
157
verifyCommonLines(expected)
154
158
assertTrue(" Number of lines" , expected.size <= size)
155
159
}
156
160
157
- fun List<String>.verifyLinesArbitraryTime (vararg expected : String ) {
161
+ fun List<String>.verifyLinesArbitraryTime (vararg expected : String ) = verify {
158
162
verifyCommonLines(expected, SanitizeMode .ARBITRARY_TIME )
159
163
checkEqualNumberOfLines(expected)
160
164
}
161
165
162
- fun List<String>.verifyLinesFlexibleThread (vararg expected : String ) {
166
+ fun List<String>.verifyLinesFlexibleThread (vararg expected : String ) = verify {
163
167
verifyCommonLines(expected, SanitizeMode .FLEXIBLE_THREAD )
164
168
checkEqualNumberOfLines(expected)
165
169
}
166
170
167
- fun List<String>.verifyLinesStartUnordered (vararg expected : String ) {
171
+ fun List<String>.verifyLinesStartUnordered (vararg expected : String ) = verify {
168
172
val expectedSorted = expected.sorted().toTypedArray()
169
173
sorted().verifyLinesStart(* expectedSorted)
170
174
}
171
175
172
- fun List<String>.verifyLinesStart (vararg expected : String ) {
176
+ fun List<String>.verifyLinesStart (vararg expected : String ) = verify {
173
177
val n = minOf(size, expected.size)
174
178
for (i in 0 until n) {
175
179
val exp = sanitize(expected[i], SanitizeMode .FLEXIBLE_THREAD )
176
180
val act = sanitize(get(i), SanitizeMode .FLEXIBLE_THREAD )
177
181
assertEquals(" Line ${i + 1 } " , exp, act.substring(0 , minOf(act.length, exp.length)))
178
182
}
179
183
checkEqualNumberOfLines(expected)
184
+ }
185
+
186
+ private object NullOut : PrintStream(NullOutputStream ())
187
+
188
+ private class NullOutputStream : OutputStream () {
189
+ override fun write (b : Int ) = Unit
190
+ }
191
+
192
+ private inline fun List<String>.verify (verification : () -> Unit ) {
193
+ try {
194
+ verification()
195
+ } catch (t: Throwable ) {
196
+ if (! OUT_ENABLED ) {
197
+ println (" Printing [delayed] test output" )
198
+ forEach { println (it) }
199
+ }
200
+
201
+ throw t
202
+ }
180
203
}
0 commit comments