• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package kotlinx.coroutines.test
2 
3 import kotlinx.coroutines.*
4 import kotlinx.coroutines.testing.*
5 import kotlin.coroutines.*
6 import kotlin.test.*
7 
8 @Suppress("DEPRECATION", "DEPRECATION_ERROR")
9 class TestBuildersTest {
10 
11     @Test
scopeRunBlocking_passesDispatchernull12     fun scopeRunBlocking_passesDispatcher() {
13         val scope = TestCoroutineScope()
14         scope.runBlockingTest {
15             assertSame(scope.coroutineContext[ContinuationInterceptor], coroutineContext[ContinuationInterceptor])
16         }
17     }
18 
19     @Test
dispatcherRunBlocking_passesDispatchernull20     fun dispatcherRunBlocking_passesDispatcher() {
21         val dispatcher = TestCoroutineDispatcher()
22         dispatcher.runBlockingTest {
23             assertSame(dispatcher, coroutineContext[ContinuationInterceptor])
24         }
25     }
26 
27     @Test
scopeRunBlocking_advancesPreviousDelaynull28     fun scopeRunBlocking_advancesPreviousDelay() {
29         val scope = TestCoroutineScope()
30         val deferred = scope.async {
31             delay(SLOW)
32             3
33         }
34 
35         scope.runBlockingTest {
36             assertRunsFast {
37                 assertEquals(3, deferred.await())
38             }
39         }
40     }
41 
42     @Test
dispatcherRunBlocking_advancesPreviousDelaynull43     fun dispatcherRunBlocking_advancesPreviousDelay() {
44         val dispatcher = TestCoroutineDispatcher()
45         val scope = CoroutineScope(dispatcher)
46         val deferred = scope.async {
47             delay(SLOW)
48             3
49         }
50 
51         dispatcher.runBlockingTest {
52             assertRunsFast {
53                 assertEquals(3, deferred.await())
54             }
55         }
56     }
57 
58     @Test
scopeRunBlocking_disablesImmediatelyOnExitnull59     fun scopeRunBlocking_disablesImmediatelyOnExit() {
60         val scope = TestCoroutineScope()
61         scope.runBlockingTest {
62             assertRunsFast {
63                 delay(SLOW)
64             }
65         }
66 
67         val deferred = scope.async {
68             delay(SLOW)
69             3
70         }
71         scope.runCurrent()
72         assertTrue(deferred.isActive)
73 
74         scope.advanceUntilIdle()
75         assertEquals(3, deferred.getCompleted())
76     }
77 
78     @Test
whenInRunBlocking_runBlockingTest_nestsProperlynull79     fun whenInRunBlocking_runBlockingTest_nestsProperly() {
80         // this is not a supported use case, but it is possible so ensure it works
81 
82         val scope = TestCoroutineScope()
83         var calls = 0
84 
85         scope.runBlockingTest {
86             delay(1_000)
87             calls++
88             runBlockingTest {
89                 val job = launch {
90                     delay(1_000)
91                     calls++
92                 }
93                 assertTrue(job.isActive)
94                 advanceUntilIdle()
95                 assertFalse(job.isActive)
96                 calls++
97             }
98             ++calls
99         }
100 
101         assertEquals(4, calls)
102     }
103 }
104