1 package kotlinx.coroutines.debug.junit5
2
3 import kotlinx.coroutines.*
4 import org.junit.jupiter.api.Test
5 import org.junit.jupiter.api.extension.*
6 import org.junit.jupiter.api.parallel.*
7
8 class CoroutinesTimeoutExtensionTest {
9
10 /**
11 * Tests that disabling coroutine creation stacktraces in [CoroutinesTimeoutExtension] does lead to them not being
12 * created.
13 *
14 * Adapted from [CoroutinesTimeoutDisabledTracesTest], an identical test for the JUnit4 rule.
15 *
16 * This test class is not intended to be run manually. Instead, use [CoroutinesTimeoutTest] as the entry point.
17 */
18 class DisabledStackTracesTest {
19 @JvmField
20 @RegisterExtension
21 internal val timeout = CoroutinesTimeoutExtension(500, true, false)
22
<lambda>null23 private val job = GlobalScope.launch(Dispatchers.Unconfined) { hangForever() }
24
hangForevernull25 private suspend fun hangForever() {
26 suspendCancellableCoroutine<Unit> { }
27 expectUnreached()
28 }
29
30 @Test
<lambda>null31 fun hangingTest() = runBlocking<Unit> {
32 waitForHangJob()
33 expectUnreached()
34 }
35
waitForHangJobnull36 private suspend fun waitForHangJob() {
37 job.join()
38 expectUnreached()
39 }
40 }
41
42 /**
43 * Tests that [CoroutinesTimeoutExtension] is installed eagerly and detects the coroutines that were launched before
44 * any test events start happening.
45 *
46 * Adapted from [CoroutinesTimeoutEagerTest], an identical test for the JUnit4 rule.
47 *
48 * This test class is not intended to be run manually. Instead, use [CoroutinesTimeoutTest] as the entry point.
49 */
50 class EagerTest {
51
52 @JvmField
53 @RegisterExtension
54 internal val timeout = CoroutinesTimeoutExtension(500)
55
<lambda>null56 private val job = GlobalScope.launch(Dispatchers.Unconfined) { hangForever() }
57
hangForevernull58 private suspend fun hangForever() {
59 suspendCancellableCoroutine<Unit> { }
60 expectUnreached()
61 }
62
63 @Test
<lambda>null64 fun hangingTest() = runBlocking<Unit> {
65 waitForHangJob()
66 expectUnreached()
67 }
68
waitForHangJobnull69 private suspend fun waitForHangJob() {
70 job.join()
71 expectUnreached()
72 }
73 }
74
75 /**
76 * Tests that [CoroutinesTimeoutExtension] performs sensibly in some simple scenarios.
77 *
78 * Adapted from [CoroutinesTimeoutTest], an identical test for the JUnit4 rule.
79 *
80 * This test class is not intended to be run manually. Instead, use [CoroutinesTimeoutTest] as the entry point.
81 */
82 class SimpleTest {
83
84 @JvmField
85 @RegisterExtension
86 internal val timeout = CoroutinesTimeoutExtension(1000, false, true)
87
88 @Test
<lambda>null89 fun hangingTest() = runBlocking<Unit> {
90 suspendForever()
91 expectUnreached()
92 }
93
suspendForevernull94 private suspend fun suspendForever() {
95 delay(Long.MAX_VALUE)
96 expectUnreached()
97 }
98
99 @Test
<lambda>null100 fun throwingTest() = runBlocking<Unit> {
101 throw RuntimeException()
102 }
103
104 @Test
<lambda>null105 fun successfulTest() = runBlocking {
106 val job = launch {
107 yield()
108 }
109
110 job.join()
111 }
112 }
113 }
114
expectUnreachednull115 private fun expectUnreached(): Nothing {
116 error("Should not be reached")
117 }