@@ -784,7 +784,7 @@ In `src/contributors/Contributors.kt`, check the implementation of the _CONCURRE
784
784
argument, you can call this function in any context: with a `Default` dispatcher, with
785
785
the main UI thread, or with a custom dispatcher.
786
786
* As you' ll see later, when calling `loadContributorsConcurrent()` from tests, you can call it in the context
787
- with `TestCoroutineDispatcher `, which simplifies testing. That makes this solution much more flexible.
787
+ with `TestDispatcher `, which simplifies testing. That makes this solution much more flexible.
788
788
789
789
2 . To specify the dispatcher on the caller side, apply the following change to the project while
790
790
letting `loadContributorsConcurrent` start coroutines in the inherited context:
@@ -1390,14 +1390,14 @@ total running time drastically decreases:
1390
1390
1391
1391
! [Comparison for total running time](time- comparison.png){width= 700 }
1392
1392
1393
- To use virtual time, replace the `runBlocking` invocation with a `runBlockingTest `. `runBlockingTest ` takes an
1394
- extension lambda to `TestCoroutineScope ` as an argument.
1393
+ To use virtual time, replace the `runBlocking` invocation with a `runTest `. `runTest ` takes an
1394
+ extension lambda to `TestScope ` as an argument.
1395
1395
When you call `delay` in a `suspend ` function inside this special scope, `delay` will increase the virtual time instead
1396
1396
of delaying in real time:
1397
1397
1398
1398
```kotlin
1399
1399
@Test
1400
- fun testDelayInSuspend () = runBlockingTest {
1400
+ fun testDelayInSuspend () = runTest {
1401
1401
val realStartTime = System .currentTimeMillis()
1402
1402
val virtualStartTime = currentTime
1403
1403
@@ -1412,18 +1412,18 @@ suspend fun foo() {
1412
1412
}
1413
1413
```
1414
1414
1415
- You can check the current virtual time using the `currentTime` property of `TestCoroutineScope `.
1415
+ You can check the current virtual time using the `currentTime` property of `TestScope `.
1416
1416
1417
1417
The actual running time in this example is several milliseconds, whereas virtual time equals the delay argument, which
1418
1418
is 1000 milliseconds.
1419
1419
1420
1420
To get the full effect of " virtual" `delay` in child coroutines,
1421
- start all of the child coroutines with `TestCoroutineDispatcher `. Otherwise , it won' t work. This dispatcher is
1422
- automatically inherited from the other `TestCoroutineScope `, unless you provide a different dispatcher:
1421
+ start all of the child coroutines with `TestDispatcher `. Otherwise , it won' t work. This dispatcher is
1422
+ automatically inherited from the other `TestScope `, unless you provide a different dispatcher:
1423
1423
1424
1424
```kotlin
1425
1425
@Test
1426
- fun testDelayInLaunch() = runBlockingTest {
1426
+ fun testDelayInLaunch() = runTest {
1427
1427
val realStartTime = System.currentTimeMillis()
1428
1428
val virtualStartTime = currentTime
1429
1429
@@ -1481,11 +1481,11 @@ Compare the total running times before and after applying your refactoring.
1481
1481
1482
1482
#### Tip for task 8 {initial-collapse-state="collapsed"}
1483
1483
1484
- 1. Replace the `runBlocking` invocation with `runBlockingTest `, and replace `System.currentTimeMillis()` with `currentTime`:
1484
+ 1. Replace the `runBlocking` invocation with `runTest `, and replace `System.currentTimeMillis()` with `currentTime`:
1485
1485
1486
1486
```kotlin
1487
1487
@Test
1488
- fun test() = runBlockingTest {
1488
+ fun test() = runTest {
1489
1489
val startTime = currentTime
1490
1490
// action
1491
1491
val totalTime = currentTime - startTime
@@ -1501,7 +1501,7 @@ Compare the total running times before and after applying your refactoring.
1501
1501
Here are the solutions for the concurrent and channels cases:
1502
1502
1503
1503
```kotlin
1504
- fun testConcurrent () = runBlockingTest {
1504
+ fun testConcurrent () = runTest {
1505
1505
val startTime = currentTime
1506
1506
val result = loadContributorsConcurrent(MockGithubService , testRequestData)
1507
1507
Assert .assertEquals(" Wrong result for 'loadContributorsConcurrent'" , expectedConcurrentResults.users, result)
@@ -1519,7 +1519,7 @@ First, check that the results are available exactly at the expected virtual time
1519
1519
themselves:
1520
1520
1521
1521
```kotlin
1522
- fun testChannels () = runBlockingTest {
1522
+ fun testChannels () = runTest {
1523
1523
val startTime = currentTime
1524
1524
var index = 0
1525
1525
loadContributorsChannels(MockGithubService , testRequestData) { users, _ ->
0 commit comments