Roman Elizarov | cb78787 | 2018-01-29 18:07:21 +0300 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-2017 JetBrains s.r.o. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package kotlinx.coroutines.experimental |
| 18 | |
| 19 | import kotlin.coroutines.experimental.* |
| 20 | import kotlin.test.* |
| 21 | |
| 22 | class RunBlockingTest : TestBase() { |
| 23 | @Test |
| 24 | fun testPrivateEventLoop() { |
| 25 | expect(1) |
| 26 | runBlocking { |
| 27 | expect(2) |
| 28 | assertTrue(coroutineContext[ContinuationInterceptor] is EventLoop) |
| 29 | yield() // is supported! |
| 30 | expect(3) |
| 31 | } |
| 32 | finish(4) |
| 33 | } |
| 34 | |
| 35 | @Test |
| 36 | fun testOuterEventLoop() { |
| 37 | expect(1) |
| 38 | runBlocking { |
| 39 | expect(2) |
| 40 | val outerEventLoop = coroutineContext[ContinuationInterceptor] as EventLoop |
| 41 | runBlocking(coroutineContext) { |
| 42 | expect(3) |
| 43 | // still same event loop |
| 44 | assertTrue(coroutineContext[ContinuationInterceptor] === outerEventLoop) |
| 45 | yield() // still works |
| 46 | expect(4) |
| 47 | } |
| 48 | expect(5) |
| 49 | } |
| 50 | finish(6) |
| 51 | } |
| 52 | |
| 53 | @Test |
| 54 | fun testOtherDispatcher() { |
| 55 | expect(1) |
| 56 | val name = "RunBlockingTest.testOtherDispatcher" |
| 57 | val thread = newSingleThreadContext(name) |
| 58 | runBlocking(thread) { |
| 59 | expect(2) |
| 60 | assertTrue(coroutineContext[ContinuationInterceptor] === thread) |
| 61 | assertTrue(Thread.currentThread().name.contains(name)) |
| 62 | yield() // should work |
| 63 | expect(3) |
| 64 | } |
| 65 | finish(4) |
| 66 | thread.close() |
| 67 | } |
| 68 | } |