Roman Elizarov | c0d71dc | 2017-12-21 22:12:43 +0300 | [diff] [blame] | 1 | |
Roman Elizarov | a7db8ec | 2017-12-21 22:45:12 +0300 | [diff] [blame] | 2 | /* |
Roman Elizarov | 1f74a2d | 2018-06-29 19:19:45 +0300 | [diff] [blame] | 3 | * Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. |
Roman Elizarov | a7db8ec | 2017-12-21 22:45:12 +0300 | [diff] [blame] | 4 | */ |
| 5 | |
Roman Elizarov | c0d71dc | 2017-12-21 22:12:43 +0300 | [diff] [blame] | 6 | @file:Suppress("NAMED_ARGUMENTS_NOT_ALLOWED") // KT-21913 |
| 7 | |
Roman Elizarov | 0950dfa | 2018-07-13 10:33:25 +0300 | [diff] [blame] | 8 | package kotlinx.coroutines |
Roman Elizarov | c0d71dc | 2017-12-21 22:12:43 +0300 | [diff] [blame] | 9 | |
Roman Elizarov | 0950dfa | 2018-07-13 10:33:25 +0300 | [diff] [blame] | 10 | import kotlinx.coroutines.channels.* |
Roman Elizarov | c0d71dc | 2017-12-21 22:12:43 +0300 | [diff] [blame] | 11 | import kotlin.test.* |
| 12 | |
Roman Elizarov | aa461cf | 2018-04-11 13:20:29 +0300 | [diff] [blame] | 13 | class WithTimeoutOrNullTest : TestBase() { |
Roman Elizarov | c0d71dc | 2017-12-21 22:12:43 +0300 | [diff] [blame] | 14 | /** |
| 15 | * Tests a case of no timeout and no suspension inside. |
| 16 | */ |
| 17 | @Test |
| 18 | fun testBasicNoSuspend() = runTest { |
| 19 | expect(1) |
| 20 | val result = withTimeoutOrNull(10_000) { |
| 21 | expect(2) |
| 22 | "OK" |
| 23 | } |
| 24 | assertEquals("OK", result) |
| 25 | finish(3) |
| 26 | } |
| 27 | |
| 28 | /** |
| 29 | * Tests a case of no timeout and one suspension inside. |
| 30 | */ |
| 31 | @Test |
| 32 | fun testBasicSuspend() = runTest { |
| 33 | expect(1) |
| 34 | val result = withTimeoutOrNull(10_000) { |
| 35 | expect(2) |
| 36 | yield() |
| 37 | expect(3) |
| 38 | "OK" |
| 39 | } |
| 40 | assertEquals("OK", result) |
| 41 | finish(4) |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Tests property dispatching of `withTimeoutOrNull` blocks |
| 46 | */ |
| 47 | @Test |
| 48 | fun testDispatch() = runTest { |
| 49 | expect(1) |
Roman Elizarov | 6e3ffb1 | 2018-09-14 13:46:58 +0300 | [diff] [blame] | 50 | launch { |
Roman Elizarov | c0d71dc | 2017-12-21 22:12:43 +0300 | [diff] [blame] | 51 | expect(4) |
| 52 | yield() // back to main |
| 53 | expect(7) |
| 54 | } |
| 55 | expect(2) |
| 56 | // test that it does not yield to the above job when started |
| 57 | val result = withTimeoutOrNull(1000) { |
| 58 | expect(3) |
| 59 | yield() // yield only now |
| 60 | expect(5) |
| 61 | "OK" |
| 62 | } |
| 63 | assertEquals("OK", result) |
| 64 | expect(6) |
| 65 | yield() // back to launch |
| 66 | finish(8) |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Tests that a 100% CPU-consuming loop will react on timeout if it has yields. |
| 71 | */ |
| 72 | @Test |
| 73 | fun testYieldBlockingWithTimeout() = runTest { |
| 74 | expect(1) |
| 75 | val result = withTimeoutOrNull(100) { |
| 76 | while (true) { |
| 77 | yield() |
| 78 | } |
| 79 | } |
Dmitry Khalanskiy | 387d1dd | 2020-02-10 18:42:28 +0300 | [diff] [blame] | 80 | assertNull(result) |
Roman Elizarov | c0d71dc | 2017-12-21 22:12:43 +0300 | [diff] [blame] | 81 | finish(2) |
| 82 | } |
| 83 | |
| 84 | @Test |
Vsevolod Tolstopyatov | 988eb26 | 2018-08-21 19:45:29 +0300 | [diff] [blame] | 85 | fun testSmallTimeout() = runTest { |
| 86 | val channel = Channel<Int>(1) |
| 87 | val value = withTimeoutOrNull(1) { |
| 88 | channel.receive() |
| 89 | } |
Vsevolod Tolstopyatov | 988eb26 | 2018-08-21 19:45:29 +0300 | [diff] [blame] | 90 | assertNull(value) |
| 91 | } |
| 92 | |
| 93 | @Test |
| 94 | fun testThrowException() = runTest(expected = {it is AssertionError}) { |
| 95 | withTimeoutOrNull(Long.MAX_VALUE) { |
| 96 | throw AssertionError() |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | @Test |
Roman Elizarov | 44d28cd | 2018-09-17 15:03:47 +0300 | [diff] [blame] | 101 | fun testInnerTimeout() = runTest( |
Roman Elizarov | c0d71dc | 2017-12-21 22:12:43 +0300 | [diff] [blame] | 102 | expected = { it is CancellationException } |
| 103 | ) { |
Roman Elizarov | 44d28cd | 2018-09-17 15:03:47 +0300 | [diff] [blame] | 104 | withTimeoutOrNull(1000) { |
| 105 | withTimeout(10) { |
Roman Elizarov | c0d71dc | 2017-12-21 22:12:43 +0300 | [diff] [blame] | 106 | while (true) { |
| 107 | yield() |
| 108 | } |
| 109 | } |
| 110 | expectUnreached() // will timeout |
| 111 | } |
| 112 | expectUnreached() // will timeout |
| 113 | } |
| 114 | |
| 115 | @Test |
Vsevolod Tolstopyatov | 988eb26 | 2018-08-21 19:45:29 +0300 | [diff] [blame] | 116 | fun testNestedTimeout() = runTest(expected = { it is TimeoutCancellationException }) { |
| 117 | withTimeoutOrNull(Long.MAX_VALUE) { |
| 118 | // Exception from this withTimeout is not suppressed by withTimeoutOrNull |
| 119 | withTimeout(10) { |
| 120 | delay(Long.MAX_VALUE) |
| 121 | 1 |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | expectUnreached() |
| 126 | } |
| 127 | |
| 128 | @Test |
Roman Elizarov | 44d28cd | 2018-09-17 15:03:47 +0300 | [diff] [blame] | 129 | fun testOuterTimeout() = runTest { |
Roman Elizarov | c0d71dc | 2017-12-21 22:12:43 +0300 | [diff] [blame] | 130 | var counter = 0 |
| 131 | val result = withTimeoutOrNull(250) { |
| 132 | while (true) { |
| 133 | val inner = withTimeoutOrNull(100) { |
| 134 | while (true) { |
| 135 | yield() |
| 136 | } |
| 137 | } |
Dmitry Khalanskiy | 387d1dd | 2020-02-10 18:42:28 +0300 | [diff] [blame] | 138 | assertNull(inner) |
Roman Elizarov | c0d71dc | 2017-12-21 22:12:43 +0300 | [diff] [blame] | 139 | counter++ |
| 140 | } |
| 141 | } |
Dmitry Khalanskiy | 387d1dd | 2020-02-10 18:42:28 +0300 | [diff] [blame] | 142 | assertNull(result) |
Vsevolod Tolstopyatov | e0cf38f | 2018-08-15 14:24:57 +0300 | [diff] [blame] | 143 | check(counter in 1..2) {"Executed: $counter times"} |
Roman Elizarov | c0d71dc | 2017-12-21 22:12:43 +0300 | [diff] [blame] | 144 | } |
Roman Elizarov | a0cc115 | 2018-02-02 10:45:27 +0100 | [diff] [blame] | 145 | |
| 146 | @Test |
| 147 | fun testBadClass() = runTest { |
| 148 | val bad = BadClass() |
| 149 | val result = withTimeoutOrNull(100) { |
| 150 | bad |
| 151 | } |
| 152 | assertSame(bad, result) |
| 153 | } |
| 154 | |
| 155 | class BadClass { |
| 156 | override fun equals(other: Any?): Boolean = error("Should not be called") |
| 157 | override fun hashCode(): Int = error("Should not be called") |
| 158 | override fun toString(): String = error("Should not be called") |
| 159 | } |
Roman Elizarov | aa461cf | 2018-04-11 13:20:29 +0300 | [diff] [blame] | 160 | |
| 161 | @Test |
| 162 | fun testNullOnTimeout() = runTest { |
| 163 | expect(1) |
| 164 | val result = withTimeoutOrNull(100) { |
| 165 | expect(2) |
| 166 | delay(1000) |
| 167 | expectUnreached() |
| 168 | "OK" |
| 169 | } |
Dmitry Khalanskiy | 387d1dd | 2020-02-10 18:42:28 +0300 | [diff] [blame] | 170 | assertNull(result) |
Roman Elizarov | aa461cf | 2018-04-11 13:20:29 +0300 | [diff] [blame] | 171 | finish(3) |
| 172 | } |
| 173 | |
| 174 | @Test |
| 175 | fun testSuppressExceptionWithResult() = runTest { |
| 176 | expect(1) |
| 177 | val result = withTimeoutOrNull(100) { |
| 178 | expect(2) |
| 179 | try { |
| 180 | delay(1000) |
| 181 | } catch (e: CancellationException) { |
| 182 | expect(3) |
| 183 | } |
| 184 | "OK" |
| 185 | } |
Dmitry Khalanskiy | 387d1dd | 2020-02-10 18:42:28 +0300 | [diff] [blame] | 186 | assertNull(result) |
Roman Elizarov | aa461cf | 2018-04-11 13:20:29 +0300 | [diff] [blame] | 187 | finish(4) |
| 188 | } |
| 189 | |
| 190 | @Test |
Vsevolod Tolstopyatov | 06f57aa | 2018-07-24 19:51:21 +0300 | [diff] [blame] | 191 | fun testSuppressExceptionWithAnotherException() = runTest { |
Roman Elizarov | aa461cf | 2018-04-11 13:20:29 +0300 | [diff] [blame] | 192 | expect(1) |
Roman Elizarov | 541a9b6 | 2018-09-25 23:23:34 +0300 | [diff] [blame] | 193 | try { |
| 194 | withTimeoutOrNull(100) { |
| 195 | expect(2) |
| 196 | try { |
| 197 | delay(1000) |
| 198 | } catch (e: CancellationException) { |
| 199 | expect(3) |
| 200 | throw TestException() |
| 201 | } |
| 202 | expectUnreached() |
| 203 | "OK" |
Roman Elizarov | aa461cf | 2018-04-11 13:20:29 +0300 | [diff] [blame] | 204 | } |
| 205 | expectUnreached() |
Roman Elizarov | 541a9b6 | 2018-09-25 23:23:34 +0300 | [diff] [blame] | 206 | } catch (e: TestException) { |
| 207 | // catches TestException |
| 208 | finish(4) |
Vsevolod Tolstopyatov | 06f57aa | 2018-07-24 19:51:21 +0300 | [diff] [blame] | 209 | |
Roman Elizarov | 541a9b6 | 2018-09-25 23:23:34 +0300 | [diff] [blame] | 210 | } |
Roman Elizarov | aa461cf | 2018-04-11 13:20:29 +0300 | [diff] [blame] | 211 | } |
| 212 | |
Vsevolod Tolstopyatov | 4cb5d19 | 2018-04-11 17:42:16 +0300 | [diff] [blame] | 213 | @Test |
| 214 | fun testNegativeTimeout() = runTest { |
| 215 | expect(1) |
| 216 | var result = withTimeoutOrNull(-1) { |
| 217 | expectUnreached() |
| 218 | } |
| 219 | assertNull(result) |
| 220 | result = withTimeoutOrNull(0) { |
| 221 | expectUnreached() |
| 222 | } |
| 223 | assertNull(result) |
| 224 | finish(2) |
| 225 | } |
Roman Elizarov | 52bfdab | 2018-09-27 19:17:16 +0300 | [diff] [blame] | 226 | |
| 227 | @Test |
| 228 | fun testExceptionFromWithinTimeout() = runTest { |
| 229 | expect(1) |
| 230 | try { |
| 231 | expect(2) |
| 232 | withTimeoutOrNull(1000) { |
| 233 | expect(3) |
| 234 | throw TestException() |
| 235 | } |
| 236 | expectUnreached() |
| 237 | } catch (e: TestException) { |
| 238 | finish(4) |
| 239 | } |
| 240 | } |
Vsevolod Tolstopyatov | 510ccfd | 2018-11-06 15:37:53 +0300 | [diff] [blame] | 241 | } |