|
5 | 5 | package kotlinx.coroutines.test
|
6 | 6 |
|
7 | 7 | import kotlinx.coroutines.*
|
8 |
| -import kotlinx.coroutines.flow.* |
9 | 8 | import kotlin.coroutines.*
|
10 | 9 | import kotlin.test.*
|
11 |
| -import kotlin.time.* |
12 | 10 |
|
13 | 11 | class RunTestTest {
|
14 | 12 |
|
@@ -50,4 +48,74 @@ class RunTestTest {
|
50 | 48 | }
|
51 | 49 | }
|
52 | 50 |
|
| 51 | + /** Tests that even the dispatch timeout of `0` is fine if all the dispatches go through the same scheduler. */ |
| 52 | + @Test |
| 53 | + fun testRunTestWithZeroTimeoutWithControlledDispatches() = runTest(dispatchTimeoutMs = 0) { |
| 54 | + // below is some arbitrary concurrent code where all dispatches go through the same scheduler. |
| 55 | + launch { |
| 56 | + delay(2000) |
| 57 | + } |
| 58 | + val deferred = async { |
| 59 | + val job = launch(TestCoroutineDispatcher(testScheduler)) { |
| 60 | + launch { |
| 61 | + delay(500) |
| 62 | + } |
| 63 | + delay(1000) |
| 64 | + } |
| 65 | + job.join() |
| 66 | + } |
| 67 | + deferred.await() |
| 68 | + } |
| 69 | + |
| 70 | + /** Tests that a dispatch timeout of `0` will fail the test if there are some dispatches outside the scheduler. */ |
| 71 | + @Test |
| 72 | + fun testRunTestWithZeroTimeoutWithUncontrolledDispatches() = testResultMap({ fn -> |
| 73 | + assertFailsWith<UncompletedCoroutinesError> { fn() } |
| 74 | + }) { |
| 75 | + runTest(dispatchTimeoutMs = 0) { |
| 76 | + withContext(Dispatchers.Default) { |
| 77 | + delay(10) |
| 78 | + 3 |
| 79 | + } |
| 80 | + throw IllegalStateException("shouldn't be reached") |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + /** Tests that too low of a dispatch timeout causes crashes. */ |
| 85 | + @Test |
| 86 | + fun testRunTestWithSmallTimeout() = testResultMap({ fn -> |
| 87 | + assertFailsWith<UncompletedCoroutinesError> { fn() } |
| 88 | + }) { |
| 89 | + runTest(dispatchTimeoutMs = 100) { |
| 90 | + withContext(Dispatchers.Default) { |
| 91 | + delay(10000) |
| 92 | + 3 |
| 93 | + } |
| 94 | + throw RuntimeException("shouldn't be reached") |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + /** Tests that too low of a dispatch timeout causes crashes. */ |
| 99 | + @Test |
| 100 | + fun testRunTestWithLargeTimeout() = runTest(dispatchTimeoutMs = 5000) { |
| 101 | + withContext(Dispatchers.Default) { |
| 102 | + delay(50) |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + /** Tests uncaught exceptions taking priority over dispatch timeout in error reports. */ |
| 107 | + @Test |
| 108 | + fun testRunTestTimingOutAndThrowing() = testResultMap({ fn -> |
| 109 | + assertFailsWith<RuntimeException> { fn() } |
| 110 | + }) { |
| 111 | + runTest(dispatchTimeoutMs = 1) { |
| 112 | + coroutineContext[CoroutineExceptionHandler]!!.handleException(coroutineContext, IllegalArgumentException()) |
| 113 | + withContext(Dispatchers.Default) { |
| 114 | + delay(10000) |
| 115 | + 3 |
| 116 | + } |
| 117 | + throw RuntimeException("shouldn't be reached") |
| 118 | + } |
| 119 | + } |
| 120 | + |
53 | 121 | }
|
0 commit comments