1 /* 2 * Copyright 2016-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. 3 */ 4 5 package kotlinx.coroutines.test 6 7 import kotlinx.coroutines.* 8 import kotlin.coroutines.* 9 import kotlin.test.* 10 11 @Suppress("DEPRECATION", "DEPRECATION_ERROR") 12 class TestBuildersTest { 13 14 @Test scopeRunBlocking_passesDispatchernull15 fun scopeRunBlocking_passesDispatcher() { 16 val scope = TestCoroutineScope() 17 scope.runBlockingTest { 18 assertSame(scope.coroutineContext[ContinuationInterceptor], coroutineContext[ContinuationInterceptor]) 19 } 20 } 21 22 @Test dispatcherRunBlocking_passesDispatchernull23 fun dispatcherRunBlocking_passesDispatcher() { 24 val dispatcher = TestCoroutineDispatcher() 25 dispatcher.runBlockingTest { 26 assertSame(dispatcher, coroutineContext[ContinuationInterceptor]) 27 } 28 } 29 30 @Test scopeRunBlocking_advancesPreviousDelaynull31 fun scopeRunBlocking_advancesPreviousDelay() { 32 val scope = TestCoroutineScope() 33 val deferred = scope.async { 34 delay(SLOW) 35 3 36 } 37 38 scope.runBlockingTest { 39 assertRunsFast { 40 assertEquals(3, deferred.await()) 41 } 42 } 43 } 44 45 @Test dispatcherRunBlocking_advancesPreviousDelaynull46 fun dispatcherRunBlocking_advancesPreviousDelay() { 47 val dispatcher = TestCoroutineDispatcher() 48 val scope = CoroutineScope(dispatcher) 49 val deferred = scope.async { 50 delay(SLOW) 51 3 52 } 53 54 dispatcher.runBlockingTest { 55 assertRunsFast { 56 assertEquals(3, deferred.await()) 57 } 58 } 59 } 60 61 @Test scopeRunBlocking_disablesImmediatelyOnExitnull62 fun scopeRunBlocking_disablesImmediatelyOnExit() { 63 val scope = TestCoroutineScope() 64 scope.runBlockingTest { 65 assertRunsFast { 66 delay(SLOW) 67 } 68 } 69 70 val deferred = scope.async { 71 delay(SLOW) 72 3 73 } 74 scope.runCurrent() 75 assertTrue(deferred.isActive) 76 77 scope.advanceUntilIdle() 78 assertEquals(3, deferred.getCompleted()) 79 } 80 81 @Test whenInAsync_runBlocking_nestsProperlynull82 fun whenInAsync_runBlocking_nestsProperly() { 83 // this is not a supported use case, but it is possible so ensure it works 84 85 val dispatcher = TestCoroutineDispatcher() 86 val scope = TestCoroutineScope(dispatcher) 87 val deferred = scope.async { 88 delay(1_000) 89 var retval = 2 90 runBlockingTest { 91 delay(1_000) 92 retval++ 93 } 94 retval 95 } 96 97 scope.advanceTimeBy(1_000) 98 scope.launch { 99 assertRunsFast { 100 assertEquals(3, deferred.getCompleted()) 101 } 102 } 103 scope.runCurrent() // execute the launch without changing to immediate dispatch (testing internals) 104 scope.cleanupTestCoroutines() 105 } 106 107 @Test whenInRunBlocking_runBlockingTest_nestsProperlynull108 fun whenInRunBlocking_runBlockingTest_nestsProperly() { 109 // this is not a supported use case, but it is possible so ensure it works 110 111 val scope = TestCoroutineScope() 112 var calls = 0 113 114 scope.runBlockingTest { 115 delay(1_000) 116 calls++ 117 runBlockingTest { 118 val job = launch { 119 delay(1_000) 120 calls++ 121 } 122 assertTrue(job.isActive) 123 advanceUntilIdle() 124 assertFalse(job.isActive) 125 calls++ 126 } 127 ++calls 128 } 129 130 assertEquals(4, calls) 131 } 132 } 133