File tree 4 files changed +63
-16
lines changed
4 files changed +63
-16
lines changed Original file line number Diff line number Diff line change @@ -31,8 +31,7 @@ Its usage is better demonstrated by the example (runnable code is [here](test/Te
31
31
32
32
``` kotlin
33
33
class TestRuleExample {
34
- @Rule
35
- @JvmField
34
+ @get:Rule
36
35
public val timeout = CoroutinesTimeout .seconds(1 )
37
36
38
37
private suspend fun someFunctionDeepInTheStack () {
Original file line number Diff line number Diff line change @@ -21,17 +21,15 @@ import java.util.concurrent.*
21
21
* Example of usage:
22
22
* ```
23
23
* class HangingTest {
24
+ * @get:Rule
25
+ * val timeout = CoroutinesTimeout.seconds(5)
24
26
*
25
- * @Rule
26
- * @JvmField
27
- * val timeout = CoroutinesTimeout.seconds(5)
28
- *
29
- * @Test
30
- * fun testThatHangs() = runBlocking {
31
- * ...
32
- * delay(Long.MAX_VALUE) // somewhere deep in the stack
33
- * ...
34
- * }
27
+ * @Test
28
+ * fun testThatHangs() = runBlocking {
29
+ * ...
30
+ * delay(Long.MAX_VALUE) // somewhere deep in the stack
31
+ * ...
32
+ * }
35
33
* }
36
34
* ```
37
35
*/
@@ -42,6 +40,11 @@ public class CoroutinesTimeout(
42
40
43
41
init {
44
42
require(testTimeoutMs > 0 ) { " Expected positive test timeout, but had $testTimeoutMs " }
43
+ /*
44
+ * Install probes in the constructor, so all the coroutines launched from within
45
+ * target test constructor will be captured
46
+ */
47
+ DebugProbes .install()
45
48
}
46
49
47
50
companion object {
Original file line number Diff line number Diff line change @@ -30,11 +30,10 @@ internal class CoroutinesTimeoutStatement(
30
30
private val testThread = Thread (testResult, " Timeout test thread" ).apply { isDaemon = true }
31
31
32
32
override fun evaluate () {
33
- DebugProbes .install()
34
- testThread.start()
35
- // Await until test is started to take only test execution time into account
36
- testStartedLatch.await()
37
33
try {
34
+ testThread.start()
35
+ // Await until test is started to take only test execution time into account
36
+ testStartedLatch.await()
38
37
testResult.get(testTimeoutMs, TimeUnit .MILLISECONDS )
39
38
return
40
39
} catch (e: TimeoutException ) {
Original file line number Diff line number Diff line change
1
+ /*
2
+ * Copyright 2016-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
3
+ */
4
+
5
+ package kotlinx.coroutines.debug.junit4
6
+
7
+ import kotlinx.coroutines.*
8
+ import org.junit.*
9
+ import org.junit.runners.model.*
10
+
11
+ class CoroutinesTimeoutEagerTest : TestBase () {
12
+
13
+ @Rule
14
+ @JvmField
15
+ public val validation = TestFailureValidation (
16
+ 500 , true ,
17
+ TestResultSpec (
18
+ " hangingTest" , expectedOutParts = listOf (
19
+ " Coroutines dump" ,
20
+ " Test hangingTest timed out after 500 milliseconds" ,
21
+ " BlockingCoroutine{Active}" ,
22
+ " runBlocking" ,
23
+ " at kotlinx.coroutines.debug.junit4.CoroutinesTimeoutEagerTest.hangForever" ,
24
+ " at kotlinx.coroutines.debug.junit4.CoroutinesTimeoutEagerTest.waitForHangJob" ),
25
+ error = TestTimedOutException ::class .java)
26
+ )
27
+
28
+ private val job = GlobalScope .launch(Dispatchers .Unconfined ) { hangForever() }
29
+
30
+ private suspend fun hangForever () {
31
+ suspendCancellableCoroutine<Unit > { }
32
+ expectUnreached()
33
+ }
34
+
35
+ @Test
36
+ fun hangingTest () = runBlocking<Unit > {
37
+ waitForHangJob()
38
+ expectUnreached()
39
+ }
40
+
41
+ private suspend fun waitForHangJob () {
42
+ job.join()
43
+ expectUnreached()
44
+ }
45
+
46
+ }
You can’t perform that action at this time.
0 commit comments