Roman Elizarov | aa461cf | 2018-04-11 13:20:29 +0300 | [diff] [blame] | 1 | /* |
Roman Elizarov | 1f74a2d | 2018-06-29 19:19:45 +0300 | [diff] [blame] | 2 | * Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. |
Roman Elizarov | aa461cf | 2018-04-11 13:20:29 +0300 | [diff] [blame] | 3 | */ |
| 4 | |
| 5 | package kotlinx.coroutines.experimental |
| 6 | |
Vsevolod Tolstopyatov | 06f57aa | 2018-07-24 19:51:21 +0300 | [diff] [blame] | 7 | import java.io.* |
Roman Elizarov | aa461cf | 2018-04-11 13:20:29 +0300 | [diff] [blame] | 8 | import kotlin.test.* |
| 9 | |
| 10 | class WithTimeoutOrNullJvmTest : TestBase() { |
Roman Elizarov | aa461cf | 2018-04-11 13:20:29 +0300 | [diff] [blame] | 11 | @Test |
| 12 | fun testOuterTimeoutFiredBeforeInner() = runTest { |
| 13 | val result = withTimeoutOrNull(100) { |
| 14 | Thread.sleep(200) // wait enough for outer timeout to fire |
| 15 | withContext(NonCancellable) { yield() } // give an event loop a chance to run and process that cancellation |
| 16 | withTimeoutOrNull(100) { |
| 17 | yield() // will cancel because of outer timeout |
| 18 | expectUnreached() |
| 19 | } |
| 20 | expectUnreached() // should not be reached, because it is outer timeout |
| 21 | } |
| 22 | // outer timeout results in null |
| 23 | assertEquals(null, result) |
| 24 | } |
| 25 | } |