File tree Expand file tree Collapse file tree 1 file changed +45
-0
lines changed Expand file tree Collapse file tree 1 file changed +45
-0
lines changed Original file line number Diff line number Diff line change
1
+ package samples
2
+
3
+ import kotlinx.coroutines.ExperimentalCoroutinesApi
4
+ import kotlinx.coroutines.coroutineScope
5
+ import kotlinx.coroutines.delay
6
+ import kotlinx.coroutines.launch
7
+ import kotlinx.coroutines.test.runBlockingTest
8
+ import org.junit.Test
9
+
10
+ @UseExperimental(ExperimentalCoroutinesApi ::class )
11
+ class SampleTest {
12
+ @Test
13
+ fun testDelayInSuspend () = runBlockingTest {
14
+ val realStartTime = System .currentTimeMillis()
15
+ val virtualStartTime = currentTime
16
+
17
+ foo()
18
+
19
+ println (" ${System .currentTimeMillis() - realStartTime} ms" ) // ~ 6 ms
20
+ println (" ${currentTime - virtualStartTime} ms" ) // 1000 ms
21
+ }
22
+
23
+ suspend fun foo () {
24
+ delay(1000 ) // auto-advances without delay
25
+ println (" foo" ) // executes eagerly when foo() is called
26
+ }
27
+
28
+ @Test
29
+ fun testDelayInLaunch () = runBlockingTest {
30
+ val realStartTime = System .currentTimeMillis()
31
+ val virtualStartTime = currentTime
32
+
33
+ bar()
34
+
35
+ println (" ${System .currentTimeMillis() - realStartTime} ms" ) // ~ 11 ms
36
+ println (" ${currentTime - virtualStartTime} ms" ) // 1000 ms
37
+ }
38
+
39
+ suspend fun bar () = coroutineScope {
40
+ launch {
41
+ delay(1000 ) // auto-advances without delay
42
+ println (" bar" ) // executes eagerly when bar() is called
43
+ }
44
+ }
45
+ }
You can’t perform that action at this time.
0 commit comments