File tree Expand file tree Collapse file tree 1 file changed +43
-0
lines changed
app/src/test/java/com/lukaslechner/coroutineusecasesonandroid/playground/testing Expand file tree Collapse file tree 1 file changed +43
-0
lines changed Original file line number Diff line number Diff line change
1
+ package com.lukaslechner.coroutineusecasesonandroid.playground.testing
2
+
3
+ import kotlinx.coroutines.CoroutineScope
4
+ import kotlinx.coroutines.delay
5
+ import kotlinx.coroutines.launch
6
+ import kotlinx.coroutines.test.runBlockingTest
7
+ import org.junit.Test
8
+
9
+
10
+ class SystemUnderTest {
11
+
12
+ suspend fun functionWithDelay (): Int {
13
+ delay(1000 )
14
+ return 42
15
+ }
16
+ }
17
+
18
+ fun CoroutineScope.functionThatStartsNewCoroutine () {
19
+ launch {
20
+ delay(1000 )
21
+ println (" Coroutine completed!" )
22
+ }
23
+ }
24
+
25
+ class TestClass {
26
+
27
+ @Test
28
+ fun `functionWithDelay should return 42` () = runBlockingTest {
29
+
30
+ val realTimeStart = System .currentTimeMillis()
31
+ val virtualTimeStart = currentTime
32
+
33
+ functionThatStartsNewCoroutine()
34
+ advanceTimeBy(1000 )
35
+
36
+ val realTimeDuration = System .currentTimeMillis() - realTimeStart
37
+ val virtualTimeDuration = currentTime - virtualTimeStart
38
+
39
+ println (" Test took $realTimeDuration real ms" )
40
+ println (" Test took $virtualTimeDuration virtual ms" )
41
+ }
42
+
43
+ }
You can’t perform that action at this time.
0 commit comments