Skip to content

Commit 6da3ef5

Browse files
committed
Added sample test
1 parent c242b8e commit 6da3ef5

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

test/samples/SampleTest.kt

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
}

0 commit comments

Comments
 (0)