@@ -11,13 +11,15 @@ import kotlin.coroutines.CoroutineContext
11
11
*
12
12
* Testing libraries may expose this interface to tests instead of [TestCoroutineDispatcher].
13
13
*/
14
+ @ExperimentalCoroutinesApi
14
15
interface DelayController {
15
16
/* *
16
17
* Returns the current virtual clock-time as it is known to this Dispatcher.
17
18
*
18
19
* @param unit The [TimeUnit] in which the clock-time must be returned.
19
20
* @return The virtual clock-time
20
21
*/
22
+ @ExperimentalCoroutinesApi
21
23
fun currentTime (unit : TimeUnit = TimeUnit .MILLISECONDS ): Long
22
24
23
25
/* *
@@ -30,27 +32,31 @@ interface DelayController {
30
32
* @param unit The [TimeUnit] in which [delayTime] and the return value is expressed.
31
33
* @return The amount of delay-time that this Dispatcher's clock has been forwarded.
32
34
*/
35
+ @ExperimentalCoroutinesApi
33
36
fun advanceTimeBy (delayTime : Long , unit : TimeUnit = TimeUnit .MILLISECONDS ): Long
34
37
35
38
/* *
36
39
* Moves the current virtual clock forward just far enough so the next delay will return.
37
40
*
38
41
* @return the amount of delay-time that this Dispatcher's clock has been forwarded.
39
42
*/
43
+ @ExperimentalCoroutinesApi
40
44
fun advanceTimeToNextDelayed (): Long
41
45
42
46
/* *
43
47
* Immediately execute all pending tasks and advance the virtual clock-time to the last delay.
44
48
*
45
49
* @return the amount of delay-time that this Dispatcher's clock has been forwarded.
46
50
*/
51
+ @ExperimentalCoroutinesApi
47
52
fun advanceUntilIdle (): Long
48
53
49
54
/* *
50
55
* Run any tasks that are pending at or before the current virtual clock-time.
51
56
*
52
57
* Calling this function will never advance the clock.
53
58
*/
59
+ @ExperimentalCoroutinesApi
54
60
fun runCurrent ()
55
61
56
62
/* *
@@ -59,6 +65,7 @@ interface DelayController {
59
65
* @throws UncompletedCoroutinesError if any pending tasks are active, however it will not throw for suspended
60
66
* coroutines.
61
67
*/
68
+ @ExperimentalCoroutinesApi
62
69
@Throws(UncompletedCoroutinesError ::class )
63
70
fun cleanupTestCoroutines ()
64
71
@@ -71,6 +78,7 @@ interface DelayController {
71
78
* This is useful when testing functions that that start a coroutine. By pausing the dispatcher assertions or
72
79
* setup may be done between the time the coroutine is created and started.
73
80
*/
81
+ @ExperimentalCoroutinesApi
74
82
suspend fun pauseDispatcher (block : suspend () -> Unit )
75
83
76
84
/* *
@@ -79,6 +87,7 @@ interface DelayController {
79
87
* When paused the dispatcher will not execute any coroutines automatically, and you must call [runCurrent], or one
80
88
* of [advanceTimeBy], [advanceTimeToNextDelayed], or [advanceUntilIdle] to execute coroutines.
81
89
*/
90
+ @ExperimentalCoroutinesApi
82
91
fun pauseDispatcher ()
83
92
84
93
/* *
@@ -88,6 +97,7 @@ interface DelayController {
88
97
* time and execute coroutines scheduled in the future use one of [advanceTimeBy], [advanceTimeToNextDelayed],
89
98
* or [advanceUntilIdle].
90
99
*/
100
+ @ExperimentalCoroutinesApi
91
101
fun resumeDispatcher ()
92
102
93
103
@Deprecated(" This API has been deprecated to integrate with Structured Concurrency." ,
@@ -112,12 +122,13 @@ interface DelayController {
112
122
/* *
113
123
* Thrown when a test has completed by there are tasks that are not completed or cancelled.
114
124
*/
125
+ @ExperimentalCoroutinesApi
115
126
class UncompletedCoroutinesError (message : String , cause : Throwable ? = null ): AssertionError(message, cause)
116
127
117
128
/* *
118
129
* [CoroutineDispatcher] that can be used in tests for both immediate and lazy execution of coroutines.
119
130
*
120
- * By default, [TestCoroutineDispatcher] will be immediate. That means any tasks scheduled to be run immediately will
131
+ * By default, [TestCoroutineDispatcher] will be immediate. That means any tasks scheduled to be run without delay will
121
132
* be immediately executed. If they were scheduled with a delay, the virtual clock-time must be advanced via one of the
122
133
* methods on [DelayController]
123
134
*
@@ -127,6 +138,7 @@ class UncompletedCoroutinesError(message: String, cause: Throwable? = null): Ass
127
138
*
128
139
* @see DelayController
129
140
*/
141
+ @ExperimentalCoroutinesApi
130
142
class TestCoroutineDispatcher :
131
143
CoroutineDispatcher (),
132
144
Delay ,
@@ -171,7 +183,7 @@ class TestCoroutineDispatcher:
171
183
}
172
184
}
173
185
174
- override fun toString (): String = " TestCoroutineDispatcher[time=$time ns]"
186
+ override fun toString (): String = " TestCoroutineDispatcher[time=${ time} ns]"
175
187
176
188
private fun post (block : Runnable ) =
177
189
queue.addLast(TimedRunnable (block, counter++ ))
0 commit comments