• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
3  */
4 
5 package kotlinx.coroutines.debug.junit5
6 
7 import kotlinx.coroutines.*
8 import org.junit.jupiter.api.Test
9 import org.junit.jupiter.api.extension.*
10 import org.junit.jupiter.api.parallel.*
11 
12 class CoroutinesTimeoutExtensionTest {
13 
14     /**
15      * Tests that disabling coroutine creation stacktraces in [CoroutinesTimeoutExtension] does lead to them not being
16      * created.
17      *
18      * Adapted from [CoroutinesTimeoutDisabledTracesTest], an identical test for the JUnit4 rule.
19      *
20      * This test class is not intended to be run manually. Instead, use [CoroutinesTimeoutTest] as the entry point.
21      */
22     class DisabledStackTracesTest {
23         @JvmField
24         @RegisterExtension
25         internal val timeout = CoroutinesTimeoutExtension(500, true, false)
26 
<lambda>null27         private val job = GlobalScope.launch(Dispatchers.Unconfined) { hangForever() }
28 
hangForevernull29         private suspend fun hangForever() {
30             suspendCancellableCoroutine<Unit> {  }
31             expectUnreached()
32         }
33 
34         @Test
<lambda>null35         fun hangingTest() = runBlocking<Unit> {
36             waitForHangJob()
37             expectUnreached()
38         }
39 
waitForHangJobnull40         private suspend fun waitForHangJob() {
41             job.join()
42             expectUnreached()
43         }
44     }
45 
46     /**
47      * Tests that [CoroutinesTimeoutExtension] is installed eagerly and detects the coroutines that were launched before
48      * any test events start happening.
49      *
50      * Adapted from [CoroutinesTimeoutEagerTest], an identical test for the JUnit4 rule.
51      *
52      * This test class is not intended to be run manually. Instead, use [CoroutinesTimeoutTest] as the entry point.
53      */
54     class EagerTest {
55 
56         @JvmField
57         @RegisterExtension
58         internal val timeout = CoroutinesTimeoutExtension(500)
59 
<lambda>null60         private val job = GlobalScope.launch(Dispatchers.Unconfined) { hangForever() }
61 
hangForevernull62         private suspend fun hangForever() {
63             suspendCancellableCoroutine<Unit> {  }
64             expectUnreached()
65         }
66 
67         @Test
<lambda>null68         fun hangingTest() = runBlocking<Unit> {
69             waitForHangJob()
70             expectUnreached()
71         }
72 
waitForHangJobnull73         private suspend fun waitForHangJob() {
74             job.join()
75             expectUnreached()
76         }
77     }
78 
79     /**
80      * Tests that [CoroutinesTimeoutExtension] performs sensibly in some simple scenarios.
81      *
82      * Adapted from [CoroutinesTimeoutTest], an identical test for the JUnit4 rule.
83      *
84      * This test class is not intended to be run manually. Instead, use [CoroutinesTimeoutTest] as the entry point.
85      */
86     class SimpleTest {
87 
88         @JvmField
89         @RegisterExtension
90         internal val timeout = CoroutinesTimeoutExtension(1000, false, true)
91 
92         @Test
<lambda>null93         fun hangingTest() = runBlocking<Unit> {
94             suspendForever()
95             expectUnreached()
96         }
97 
suspendForevernull98         private suspend fun suspendForever() {
99             delay(Long.MAX_VALUE)
100             expectUnreached()
101         }
102 
103         @Test
<lambda>null104         fun throwingTest() = runBlocking<Unit> {
105             throw RuntimeException()
106         }
107 
108         @Test
<lambda>null109         fun successfulTest() = runBlocking {
110             val job = launch {
111                 yield()
112             }
113 
114             job.join()
115         }
116     }
117 }
118 
expectUnreachednull119 private fun expectUnreached(): Nothing {
120     error("Should not be reached")
121 }