|
| 1 | +/* |
| 2 | + * Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. |
| 3 | + */ |
| 4 | + |
| 5 | +@file:Suppress("NAMED_ARGUMENTS_NOT_ALLOWED", "UNREACHABLE_CODE") |
| 6 | + |
| 7 | +// KT-21913 |
| 8 | + |
| 9 | +package kotlinx.coroutines |
| 10 | + |
| 11 | +import kotlin.test.* |
| 12 | +import kotlin.time.Duration |
| 13 | +import kotlin.time.ExperimentalTime |
| 14 | +import kotlin.time.milliseconds |
| 15 | +import kotlin.time.seconds |
| 16 | + |
| 17 | +@ExperimentalTime |
| 18 | +class WithTimeoutDurationTest : TestBase() { |
| 19 | + /** |
| 20 | + * Tests a case of no timeout and no suspension inside. |
| 21 | + */ |
| 22 | + @Test |
| 23 | + fun testBasicNoSuspend() = runTest { |
| 24 | + expect(1) |
| 25 | + val result = withTimeout(10.seconds) { |
| 26 | + expect(2) |
| 27 | + "OK" |
| 28 | + } |
| 29 | + assertEquals("OK", result) |
| 30 | + finish(3) |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * Tests a case of no timeout and one suspension inside. |
| 35 | + */ |
| 36 | + @Test |
| 37 | + fun testBasicSuspend() = runTest { |
| 38 | + expect(1) |
| 39 | + val result = withTimeout(10.seconds) { |
| 40 | + expect(2) |
| 41 | + yield() |
| 42 | + expect(3) |
| 43 | + "OK" |
| 44 | + } |
| 45 | + assertEquals("OK", result) |
| 46 | + finish(4) |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Tests proper dispatching of `withTimeout` blocks |
| 51 | + */ |
| 52 | + @Test |
| 53 | + fun testDispatch() = runTest { |
| 54 | + expect(1) |
| 55 | + launch { |
| 56 | + expect(4) |
| 57 | + yield() // back to main |
| 58 | + expect(7) |
| 59 | + } |
| 60 | + expect(2) |
| 61 | + // test that it does not yield to the above job when started |
| 62 | + val result = withTimeout(1.seconds) { |
| 63 | + expect(3) |
| 64 | + yield() // yield only now |
| 65 | + expect(5) |
| 66 | + "OK" |
| 67 | + } |
| 68 | + assertEquals("OK", result) |
| 69 | + expect(6) |
| 70 | + yield() // back to launch |
| 71 | + finish(8) |
| 72 | + } |
| 73 | + |
| 74 | + |
| 75 | + /** |
| 76 | + * Tests that a 100% CPU-consuming loop will react on timeout if it has yields. |
| 77 | + */ |
| 78 | + @Test |
| 79 | + fun testYieldBlockingWithTimeout() = runTest( |
| 80 | + expected = { it is CancellationException } |
| 81 | + ) { |
| 82 | + withTimeout(100.milliseconds) { |
| 83 | + while (true) { |
| 84 | + yield() |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Tests that [withTimeout] waits for children coroutines to complete. |
| 91 | + */ |
| 92 | + @Test |
| 93 | + fun testWithTimeoutChildWait() = runTest { |
| 94 | + expect(1) |
| 95 | + withTimeout(100.milliseconds) { |
| 96 | + expect(2) |
| 97 | + // launch child with timeout |
| 98 | + launch { |
| 99 | + expect(4) |
| 100 | + } |
| 101 | + expect(3) |
| 102 | + // now will wait for child before returning |
| 103 | + } |
| 104 | + finish(5) |
| 105 | + } |
| 106 | + |
| 107 | + @Test |
| 108 | + fun testBadClass() = runTest { |
| 109 | + val bad = BadClass() |
| 110 | + val result = withTimeout(100.milliseconds) { |
| 111 | + bad |
| 112 | + } |
| 113 | + assertSame(bad, result) |
| 114 | + } |
| 115 | + |
| 116 | + class BadClass { |
| 117 | + override fun equals(other: Any?): Boolean = error("Should not be called") |
| 118 | + override fun hashCode(): Int = error("Should not be called") |
| 119 | + override fun toString(): String = error("Should not be called") |
| 120 | + } |
| 121 | + |
| 122 | + @Test |
| 123 | + fun testExceptionOnTimeout() = runTest { |
| 124 | + expect(1) |
| 125 | + try { |
| 126 | + withTimeout(100.milliseconds) { |
| 127 | + expect(2) |
| 128 | + delay(1000.milliseconds) |
| 129 | + expectUnreached() |
| 130 | + "OK" |
| 131 | + } |
| 132 | + } catch (e: CancellationException) { |
| 133 | + assertEquals("Timed out waiting for 100 ms", e.message) |
| 134 | + finish(3) |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + @Test |
| 139 | + fun testSuppressExceptionWithResult() = runTest( |
| 140 | + expected = { it is CancellationException } |
| 141 | + ) { |
| 142 | + expect(1) |
| 143 | + withTimeout(100.milliseconds) { |
| 144 | + expect(2) |
| 145 | + try { |
| 146 | + delay(1000.milliseconds) |
| 147 | + } catch (e: CancellationException) { |
| 148 | + finish(3) |
| 149 | + } |
| 150 | + "OK" |
| 151 | + } |
| 152 | + expectUnreached() |
| 153 | + } |
| 154 | + |
| 155 | + @Test |
| 156 | + fun testSuppressExceptionWithAnotherException() = runTest { |
| 157 | + expect(1) |
| 158 | + try { |
| 159 | + withTimeout(100.milliseconds) { |
| 160 | + expect(2) |
| 161 | + try { |
| 162 | + delay(1000.milliseconds) |
| 163 | + } catch (e: CancellationException) { |
| 164 | + expect(3) |
| 165 | + throw TestException() |
| 166 | + } |
| 167 | + expectUnreached() |
| 168 | + "OK" |
| 169 | + } |
| 170 | + expectUnreached() |
| 171 | + } catch (e: TestException) { |
| 172 | + finish(4) |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + @Test |
| 177 | + fun testNegativeTimeout() = runTest { |
| 178 | + expect(1) |
| 179 | + try { |
| 180 | + withTimeout(-1.milliseconds) { |
| 181 | + expectUnreached() |
| 182 | + "OK" |
| 183 | + } |
| 184 | + } catch (e: TimeoutCancellationException) { |
| 185 | + assertEquals("Timed out immediately", e.message) |
| 186 | + finish(2) |
| 187 | + } |
| 188 | + } |
| 189 | + |
| 190 | + @Test |
| 191 | + fun testExceptionFromWithinTimeout() = runTest { |
| 192 | + expect(1) |
| 193 | + try { |
| 194 | + expect(2) |
| 195 | + withTimeout(1.seconds) { |
| 196 | + expect(3) |
| 197 | + throw TestException() |
| 198 | + } |
| 199 | + expectUnreached() |
| 200 | + } catch (e: TestException) { |
| 201 | + finish(4) |
| 202 | + } |
| 203 | + } |
| 204 | + |
| 205 | + @Test |
| 206 | + fun testIncompleteWithTimeoutState() = runTest { |
| 207 | + lateinit var timeoutJob: Job |
| 208 | + val handle = withTimeout(Duration.INFINITE) { |
| 209 | + timeoutJob = coroutineContext[Job]!! |
| 210 | + timeoutJob.invokeOnCompletion { } |
| 211 | + } |
| 212 | + |
| 213 | + handle.dispose() |
| 214 | + timeoutJob.join() |
| 215 | + assertTrue(timeoutJob.isCompleted) |
| 216 | + assertFalse(timeoutJob.isActive) |
| 217 | + assertFalse(timeoutJob.isCancelled) |
| 218 | + } |
| 219 | +} |
0 commit comments