@@ -73,13 +73,13 @@ coroutine. Any calls to `delay` will automatically advance time.
73
73
74
74
``` kotlin
75
75
@Test
76
- fun testFoo () = runBlockingTest { // a coroutine with a extra test control
76
+ fun testFoo () = runBlockingTest { // a coroutine with an extra test control
77
77
val actual = foo()
78
78
// ...
79
79
}
80
80
81
81
suspend fun foo () {
82
- delay(1_000 ) // auto-advances without delay due to runBlockingTest
82
+ delay(1_000 ) // auto-advances without delay due to runBlockingTest
83
83
// ...
84
84
}
85
85
```
@@ -105,8 +105,8 @@ fun testFooWithLaunch() = runBlockingTest {
105
105
fun CoroutineScope.foo () {
106
106
// This coroutines `Job` is not shared with the test code
107
107
launch {
108
- bar() // executes eagerly when foo() is called due to runBlockingTest
109
- println (1 ) // executes eagerly when foo() is called due to runBlockingTest
108
+ bar() // executes eagerly when foo() is called due to runBlockingTest
109
+ println (1 ) // executes eagerly when foo() is called due to runBlockingTest
110
110
}
111
111
}
112
112
@@ -129,24 +129,24 @@ To control time in the test you can use the [DelayController] interface. The blo
129
129
fun testFooWithLaunchAndDelay () = runBlockingTest {
130
130
foo()
131
131
// the coroutine launched by foo has not completed here, it is suspended waiting for delay(1_000)
132
- advanceTimeBy(1_000 ) // progress time, this will cause the delay to resume
132
+ advanceTimeBy(1_000 ) // progress time, this will cause the delay to resume
133
133
// foo() coroutine launched by foo has completed here
134
134
// ...
135
135
}
136
136
137
137
suspend fun CoroutineScope.foo () {
138
138
launch {
139
- println (1 ) // executes eagerly when foo() is called due to runBlockingTest
140
- delay(1_000 ) // suspends until time is advanced by at least 1_000
141
- println (2 ) // executes after advanceTimeBy(1_000)
139
+ println (1 ) // executes eagerly when foo() is called due to runBlockingTest
140
+ delay(1_000 ) // suspends until time is advanced by at least 1_000
141
+ println (2 ) // executes after advanceTimeBy(1_000)
142
142
}
143
143
}
144
144
```
145
145
146
146
* Note:* ` runBlockingTest ` will always attempt to auto-progress time until all coroutines are completed just before
147
147
exiting. This is a convenience to avoid having to call [ advanceUntilIdle] [ DelayController.advanceUntilIdle ]
148
148
as the last line of many common test cases.
149
- If any coroutines cannot complete by advancing time, a [ UncompletedCoroutinesError] is thrown.
149
+ If any coroutines cannot complete by advancing time, an [ UncompletedCoroutinesError] is thrown.
150
150
151
151
### Testing ` withTimeout ` using ` runBlockingTest `
152
152
@@ -159,16 +159,16 @@ example an uncompleted `Deferred<Foo>` is provided to the function under test vi
159
159
``` kotlin
160
160
@Test(expected = TimeoutCancellationException ::class )
161
161
fun testFooWithTimeout () {
162
- val uncompleted = CompletableDeferred <Foo >() // this Deferred<Foo> will never complete
162
+ val uncompleted = CompletableDeferred <Foo >() // this Deferred<Foo> will never complete
163
163
foo(uncompleted)
164
- advanceTimeBy(1_000 ) // advance time, which will cause the timeout to throw an exception
164
+ advanceTimeBy(1_000 ) // advance time, which will cause the timeout to throw an exception
165
165
// ...
166
166
}
167
167
168
168
fun CoroutineScope.foo (resultDeferred : Deferred <Foo >) {
169
169
launch {
170
170
withTimeout(1_000 ) {
171
- resultDeferred.await() // await() will suspend forever waiting for uncompleted
171
+ resultDeferred.await() // await() will suspend forever waiting for uncompleted
172
172
// ...
173
173
}
174
174
}
@@ -196,19 +196,19 @@ fun testFooWithPauseDispatcher() = runBlockingTest {
196
196
pauseDispatcher {
197
197
foo()
198
198
// the coroutine started by foo has not run yet
199
- runCurrent() // the coroutine started by foo advances to delay(1_000)
199
+ runCurrent() // the coroutine started by foo advances to delay(1_000)
200
200
// the coroutine started by foo has called println(1), and is suspended on delay(1_000)
201
- advanceTimeBy(1_000 ) // progress time, this will cause the delay to resume
201
+ advanceTimeBy(1_000 ) // progress time, this will cause the delay to resume
202
202
// the coroutine started by foo has called println(2) and has completed here
203
203
}
204
204
// ...
205
205
}
206
206
207
207
fun CoroutineScope.foo () {
208
208
launch {
209
- println (1 ) // executes after runCurrent() is called
210
- delay(1_000 ) // suspends until time is advanced by at least 1_000
211
- println (2 ) // executes after advanceTimeBy(1_000)
209
+ println (1 ) // executes after runCurrent() is called
210
+ delay(1_000 ) // suspends until time is advanced by at least 1_000
211
+ println (2 ) // executes after advanceTimeBy(1_000)
212
212
}
213
213
}
214
214
```
@@ -250,7 +250,7 @@ In simple cases, tests can use the [TestCoroutineScope] created by [runBlockingT
250
250
``` kotlin
251
251
@Test
252
252
fun testFoo () = runBlockingTest {
253
- foo() // runBlockingTest passed in a TestCoroutineScope as this
253
+ foo() // runBlockingTest passed in a TestCoroutineScope as this
254
254
}
255
255
256
256
fun CoroutineScope.foo () {
@@ -329,7 +329,7 @@ important to ensure that [cleanupTestCoroutines][TestCoroutineDispatcher.cleanup
329
329
330
330
``` kotlin
331
331
class TestClass {
332
- val testDispatcher = TestCoroutineDispatcher ()
332
+ private val testDispatcher = TestCoroutineDispatcher ()
333
333
334
334
@Before
335
335
fun setup () {
@@ -398,7 +398,7 @@ either dependency injection, a service locator, or a default parameter.
398
398
``` kotlin
399
399
suspend fun veryExpensiveOne () = withContext(Dispatchers .Default ) {
400
400
delay(1_000 )
401
- 1 // for very expensive values of 1
401
+ 1 // for very expensive values of 1
402
402
}
403
403
```
404
404
@@ -409,7 +409,7 @@ directly, there is no need to inject a `TestCoroutineDispatcher` into this funct
409
409
410
410
``` kotlin
411
411
suspend fun veryExpensiveTwo () = withContext(Dispatchers .Default ) {
412
- 2 // for very expensive values of 2
412
+ 2 // for very expensive values of 2
413
413
}
414
414
```
415
415
0 commit comments