Roman Elizarov | cb78787 | 2018-01-29 18:07:21 +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 | cb78787 | 2018-01-29 18:07:21 +0300 | [diff] [blame] | 3 | */ |
| 4 | |
| 5 | package kotlinx.coroutines.experimental |
| 6 | |
| 7 | import kotlin.coroutines.experimental.* |
| 8 | import kotlin.test.* |
| 9 | |
| 10 | class RunBlockingTest : TestBase() { |
Vsevolod Tolstopyatov | e90cdb0 | 2018-08-08 18:04:59 +0300 | [diff] [blame^] | 11 | |
| 12 | @Test |
| 13 | fun testWithTimeoutBusyWait() = runBlocking { |
| 14 | val value = withTimeoutOrNull(10) { |
| 15 | while (isActive) { |
| 16 | // Busy wait |
| 17 | } |
| 18 | "value" |
| 19 | } |
| 20 | |
| 21 | assertEquals("value", value) |
| 22 | } |
| 23 | |
Roman Elizarov | cb78787 | 2018-01-29 18:07:21 +0300 | [diff] [blame] | 24 | @Test |
| 25 | fun testPrivateEventLoop() { |
| 26 | expect(1) |
| 27 | runBlocking { |
| 28 | expect(2) |
| 29 | assertTrue(coroutineContext[ContinuationInterceptor] is EventLoop) |
| 30 | yield() // is supported! |
| 31 | expect(3) |
| 32 | } |
| 33 | finish(4) |
| 34 | } |
| 35 | |
| 36 | @Test |
| 37 | fun testOuterEventLoop() { |
| 38 | expect(1) |
| 39 | runBlocking { |
| 40 | expect(2) |
| 41 | val outerEventLoop = coroutineContext[ContinuationInterceptor] as EventLoop |
| 42 | runBlocking(coroutineContext) { |
| 43 | expect(3) |
| 44 | // still same event loop |
| 45 | assertTrue(coroutineContext[ContinuationInterceptor] === outerEventLoop) |
| 46 | yield() // still works |
| 47 | expect(4) |
| 48 | } |
| 49 | expect(5) |
| 50 | } |
| 51 | finish(6) |
| 52 | } |
| 53 | |
| 54 | @Test |
| 55 | fun testOtherDispatcher() { |
| 56 | expect(1) |
| 57 | val name = "RunBlockingTest.testOtherDispatcher" |
| 58 | val thread = newSingleThreadContext(name) |
| 59 | runBlocking(thread) { |
| 60 | expect(2) |
| 61 | assertTrue(coroutineContext[ContinuationInterceptor] === thread) |
| 62 | assertTrue(Thread.currentThread().name.contains(name)) |
| 63 | yield() // should work |
| 64 | expect(3) |
| 65 | } |
| 66 | finish(4) |
| 67 | thread.close() |
| 68 | } |
| 69 | } |