@@ -8,8 +8,8 @@ import kotlinx.coroutines.*
8
8
import kotlin.test.*
9
9
10
10
class TestCoroutineSchedulerTest {
11
- @Test
12
11
/* * Tests that `TestCoroutineScheduler` attempts to detect if there are several instances of it. */
12
+ @Test
13
13
fun testContextElement () {
14
14
runBlockingTest {
15
15
assertFailsWith<IllegalStateException > {
@@ -18,4 +18,65 @@ class TestCoroutineSchedulerTest {
18
18
}
19
19
}
20
20
}
21
+
22
+ /* * Tests that, as opposed to [DelayController.advanceTimeBy] or [TestCoroutineScope.advanceTimeBy],
23
+ * [TestCoroutineScheduler.advanceTimeBy] doesn't run the tasks scheduled at the target moment. */
24
+ @Test
25
+ fun testAdvanceTimeByDoesNotRunCurrent () {
26
+ val dispatcher = TestCoroutineDispatcher ()
27
+ dispatcher.runBlockingTest {
28
+ dispatcher.pauseDispatcher {
29
+ var entered = false
30
+ launch {
31
+ delay(15 )
32
+ entered = true
33
+ }
34
+ testScheduler.advanceTimeBy(15 )
35
+ assertFalse(entered)
36
+ testScheduler.runCurrent()
37
+ assertTrue(entered)
38
+ }
39
+ }
40
+ }
41
+
42
+ /* * Tests that [TestCoroutineScheduler.advanceTimeBy] doesn't accept negative delays. */
43
+ @Test
44
+ fun testAdvanceTimeByWithNegativeDelay () {
45
+ val scheduler = TestCoroutineScheduler ()
46
+ assertFailsWith<IllegalArgumentException > {
47
+ scheduler.advanceTimeBy(- 1 )
48
+ }
49
+ }
50
+
51
+ /* * Tests that if [TestCoroutineScheduler.advanceTimeBy] encounters an arithmetic overflow, all the tasks scheduled
52
+ * until the moment [Long.MAX_VALUE] get run. */
53
+ @Test
54
+ fun testAdvanceTimeByEnormousDelays () {
55
+ val dispatcher = TestCoroutineDispatcher ()
56
+ dispatcher.runBlockingTest {
57
+ dispatcher.pauseDispatcher {
58
+ val initialDelay = 10L
59
+ delay(initialDelay)
60
+ assertEquals(initialDelay, currentTime)
61
+ var enteredInfinity = false
62
+ launch {
63
+ delay(Long .MAX_VALUE - 1 ) // delay(Long.MAX_VALUE) does nothing
64
+ assertEquals(Long .MAX_VALUE , currentTime)
65
+ enteredInfinity = true
66
+ }
67
+ var enteredNearInfinity = false
68
+ launch {
69
+ delay(Long .MAX_VALUE - initialDelay - 1 )
70
+ assertEquals(Long .MAX_VALUE - 1 , currentTime)
71
+ enteredNearInfinity = true
72
+ }
73
+ testScheduler.advanceTimeBy(Long .MAX_VALUE )
74
+ assertFalse(enteredInfinity)
75
+ assertTrue(enteredNearInfinity)
76
+ assertEquals(Long .MAX_VALUE , currentTime)
77
+ testScheduler.runCurrent()
78
+ assertTrue(enteredInfinity)
79
+ }
80
+ }
81
+ }
21
82
}
0 commit comments