Skip to content

Commit 69aa8ae

Browse files
Add playground for testing coroutines
1 parent e4bc506 commit 69aa8ae

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
}

0 commit comments

Comments
 (0)