Skip to content

Commit c9867b2

Browse files
committed
Cleanup TestCoroutineScope documentation
1 parent fe820ba commit c9867b2

File tree

2 files changed

+22
-21
lines changed

2 files changed

+22
-21
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ suspend fun main() = coroutineScope {
3939
* Integration with `Window`.
4040
* [test](kotlinx-coroutines-test/README.md) — test utilities for coroutines
4141
* `Dispatchers.setMain` to override `Dispatchers.Main` in tests.
42+
* `TestCoroutineScope` to test suspending functions and coroutines.
4243
* [debug](kotlinx-coroutines-debug/README.md) — debug utilities for coroutines.
4344
* `DebugProbes` API to probe, keep track of, print and dump active coroutines.
4445
* `CoroutinesTimeout` test rule to automatically dump coroutines on test timeout.

kotlinx-coroutines-test/README.md

+21-21
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,13 @@ coroutine. Any calls to `delay` will automatically advance time.
7373

7474
```kotlin
7575
@Test
76-
fun testFoo() = runBlockingTest { // a coroutine with a extra test control
76+
fun testFoo() = runBlockingTest { // a coroutine with an extra test control
7777
val actual = foo()
7878
// ...
7979
}
8080

8181
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
8383
// ...
8484
}
8585
```
@@ -105,8 +105,8 @@ fun testFooWithLaunch() = runBlockingTest {
105105
fun CoroutineScope.foo() {
106106
// This coroutines `Job` is not shared with the test code
107107
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
110110
}
111111
}
112112

@@ -129,24 +129,24 @@ To control time in the test you can use the [DelayController] interface. The blo
129129
fun testFooWithLaunchAndDelay() = runBlockingTest {
130130
foo()
131131
// 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
133133
// foo() coroutine launched by foo has completed here
134134
// ...
135135
}
136136

137137
suspend fun CoroutineScope.foo() {
138138
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)
142142
}
143143
}
144144
```
145145

146146
*Note:* `runBlockingTest` will always attempt to auto-progress time until all coroutines are completed just before
147147
exiting. This is a convenience to avoid having to call [advanceUntilIdle][DelayController.advanceUntilIdle]
148148
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.
150150

151151
### Testing `withTimeout` using `runBlockingTest`
152152

@@ -159,16 +159,16 @@ example an uncompleted `Deferred<Foo>` is provided to the function under test vi
159159
```kotlin
160160
@Test(expected = TimeoutCancellationException::class)
161161
fun testFooWithTimeout() {
162-
val uncompleted = CompletableDeferred<Foo>() // this Deferred<Foo> will never complete
162+
val uncompleted = CompletableDeferred<Foo>() // this Deferred<Foo> will never complete
163163
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
165165
// ...
166166
}
167167

168168
fun CoroutineScope.foo(resultDeferred: Deferred<Foo>) {
169169
launch {
170170
withTimeout(1_000) {
171-
resultDeferred.await() // await() will suspend forever waiting for uncompleted
171+
resultDeferred.await() // await() will suspend forever waiting for uncompleted
172172
// ...
173173
}
174174
}
@@ -196,19 +196,19 @@ fun testFooWithPauseDispatcher() = runBlockingTest {
196196
pauseDispatcher {
197197
foo()
198198
// 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)
200200
// 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
202202
// the coroutine started by foo has called println(2) and has completed here
203203
}
204204
// ...
205205
}
206206

207207
fun CoroutineScope.foo() {
208208
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)
212212
}
213213
}
214214
```
@@ -250,7 +250,7 @@ In simple cases, tests can use the [TestCoroutineScope] created by [runBlockingT
250250
```kotlin
251251
@Test
252252
fun testFoo() = runBlockingTest {
253-
foo() // runBlockingTest passed in a TestCoroutineScope as this
253+
foo() // runBlockingTest passed in a TestCoroutineScope as this
254254
}
255255

256256
fun CoroutineScope.foo() {
@@ -329,7 +329,7 @@ important to ensure that [cleanupTestCoroutines][TestCoroutineDispatcher.cleanup
329329

330330
```kotlin
331331
class TestClass {
332-
val testDispatcher = TestCoroutineDispatcher()
332+
private val testDispatcher = TestCoroutineDispatcher()
333333

334334
@Before
335335
fun setup() {
@@ -398,7 +398,7 @@ either dependency injection, a service locator, or a default parameter.
398398
```kotlin
399399
suspend fun veryExpensiveOne() = withContext(Dispatchers.Default) {
400400
delay(1_000)
401-
1 // for very expensive values of 1
401+
1 // for very expensive values of 1
402402
}
403403
```
404404

@@ -409,7 +409,7 @@ directly, there is no need to inject a `TestCoroutineDispatcher` into this funct
409409

410410
```kotlin
411411
suspend fun veryExpensiveTwo() = withContext(Dispatchers.Default) {
412-
2 // for very expensive values of 2
412+
2 // for very expensive values of 2
413413
}
414414
```
415415

0 commit comments

Comments
 (0)