blob: f2c1a39ad4df4e3f74944aca4ff3b1625b4fc1a3 [file] [log] [blame]
Roman Elizarovcb787872018-01-29 18:07:21 +03001/*
Roman Elizarov1f74a2d2018-06-29 19:19:45 +03002 * Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
Roman Elizarovcb787872018-01-29 18:07:21 +03003 */
4
5package kotlinx.coroutines.experimental
6
7import kotlin.coroutines.experimental.*
8import kotlin.test.*
9
10class RunBlockingTest : TestBase() {
Vsevolod Tolstopyatove90cdb02018-08-08 18:04:59 +030011
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 Elizarovcb787872018-01-29 18:07:21 +030024 @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}