@@ -10,33 +10,26 @@ import kotlin.test.*
10
10
class TestCoroutineSchedulerTest {
11
11
/* * Tests that `TestCoroutineScheduler` attempts to detect if there are several instances of it. */
12
12
@Test
13
- fun testContextElement () {
14
- runBlockingTest {
15
- assertFailsWith<IllegalStateException > {
16
- withContext(TestCoroutineDispatcher ()) {
17
- }
13
+ fun testContextElement () = runBlockingTest {
14
+ assertFailsWith<IllegalStateException > {
15
+ withContext(TestCoroutineDispatcher ()) {
18
16
}
19
17
}
20
18
}
21
19
22
20
/* * Tests that, as opposed to [DelayController.advanceTimeBy] or [TestCoroutineScope.advanceTimeBy],
23
21
* [TestCoroutineScheduler.advanceTimeBy] doesn't run the tasks scheduled at the target moment. */
24
22
@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
- }
23
+ fun testAdvanceTimeByDoesNotRunCurrent () = runBlockingTest {
24
+ var entered = false
25
+ launch {
26
+ delay(15 )
27
+ entered = true
39
28
}
29
+ testScheduler.advanceTimeBy(15 )
30
+ assertFalse(entered)
31
+ testScheduler.runCurrent()
32
+ assertTrue(entered)
40
33
}
41
34
42
35
/* * Tests that [TestCoroutineScheduler.advanceTimeBy] doesn't accept negative delays. */
@@ -51,32 +44,78 @@ class TestCoroutineSchedulerTest {
51
44
/* * Tests that if [TestCoroutineScheduler.advanceTimeBy] encounters an arithmetic overflow, all the tasks scheduled
52
45
* until the moment [Long.MAX_VALUE] get run. */
53
46
@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)
47
+ fun testAdvanceTimeByEnormousDelays () = runBlockingTest {
48
+ val initialDelay = 10L
49
+ delay(initialDelay)
50
+ assertEquals(initialDelay, currentTime)
51
+ var enteredInfinity = false
52
+ launch {
53
+ delay(Long .MAX_VALUE - 1 ) // delay(Long.MAX_VALUE) does nothing
54
+ assertEquals(Long .MAX_VALUE , currentTime)
55
+ enteredInfinity = true
56
+ }
57
+ var enteredNearInfinity = false
58
+ launch {
59
+ delay(Long .MAX_VALUE - initialDelay - 1 )
60
+ assertEquals(Long .MAX_VALUE - 1 , currentTime)
61
+ enteredNearInfinity = true
62
+ }
63
+ testScheduler.advanceTimeBy(Long .MAX_VALUE )
64
+ assertFalse(enteredInfinity)
65
+ assertTrue(enteredNearInfinity)
66
+ assertEquals(Long .MAX_VALUE , currentTime)
67
+ testScheduler.runCurrent()
68
+ assertTrue(enteredInfinity)
69
+ }
70
+
71
+ /* * Tests the basic functionality of [TestCoroutineScheduler.advanceTimeBy]. */
72
+ @Test
73
+ fun testAdvanceTimeBy () {
74
+ val scheduler = TestCoroutineScheduler ()
75
+ val scope = TestCoroutineScope (scheduler)
76
+ var stage = 1
77
+ scope.launch {
78
+ delay(1_000 )
79
+ assertEquals(1_000 , scheduler.currentTime)
80
+ stage = 2
81
+ delay(500 )
82
+ assertEquals(1_500 , scheduler.currentTime)
83
+ stage = 3
84
+ delay(501 )
85
+ assertEquals(2_001 , scheduler.currentTime)
86
+ stage = 4
87
+ }
88
+ assertEquals(1 , stage)
89
+ assertEquals(0 , scheduler.currentTime)
90
+ scheduler.advanceTimeBy(2_000 )
91
+ assertEquals(3 , stage)
92
+ assertEquals(2_000 , scheduler.currentTime)
93
+ scheduler.advanceTimeBy(2 )
94
+ assertEquals(4 , stage)
95
+ assertEquals(2_002 , scheduler.currentTime)
96
+ scope.cleanupTestCoroutines()
97
+ }
98
+
99
+ /* * Tests that [TestCoroutineScheduler.runCurrent] will not run new tasks after the current time has advanced. */
100
+ @Test
101
+ fun testRunCurrentNotDrainingQueue () {
102
+ val scheduler = TestCoroutineScheduler ()
103
+ val scope = TestCoroutineScope (scheduler)
104
+ var stage = 1
105
+ scope.launch {
106
+ delay(1 )
107
+ launch {
108
+ delay(1 )
109
+ stage = 3
79
110
}
111
+ scheduler.advanceTimeBy(1 )
112
+ stage = 2
80
113
}
114
+ scheduler.advanceTimeBy(1 )
115
+ assertEquals(1 , stage)
116
+ scheduler.runCurrent()
117
+ assertEquals(2 , stage)
118
+ scheduler.runCurrent()
119
+ assertEquals(3 , stage)
81
120
}
82
121
}
0 commit comments