Skip to content

Commit 7a10945

Browse files
Improve AndroidVersionRepositoryTest.kt
1 parent a77fb71 commit 7a10945

2 files changed

Lines changed: 36 additions & 34 deletions

File tree

app/src/main/java/com/lukaslechner/coroutineusecasesonandroid/usecases/coroutines/usecase14/AndroidVersionRepository.kt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@ class AndroidVersionRepository(
2020
suspend fun loadAndStoreRemoteAndroidVersions(): List<AndroidVersion> {
2121
return scope.async {
2222
val recentVersions = api.getRecentAndroidVersions()
23-
Timber.d("Recent Android versions loaded")
24-
for (recentVersion in recentVersions) {
25-
Timber.d("Insert $recentVersion to database")
26-
database.insert(recentVersion.mapToEntity())
27-
}
28-
recentVersions
29-
}.await()
30-
}
23+
Timber.d("Recent Android versions loaded")
24+
for (recentVersion in recentVersions) {
25+
Timber.d("Insert $recentVersion to database")
26+
database.insert(recentVersion.mapToEntity())
27+
}
28+
recentVersions
29+
}.await()
30+
}
3131

3232
fun clearDatabase() {
3333
scope.launch {
Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.lukaslechner.coroutineusecasesonandroid.usecases.coroutines.usecase14
22

33
import com.lukaslechner.coroutineusecasesonandroid.mock.mockAndroidVersions
4+
import com.lukaslechner.coroutineusecasesonandroid.playground.structuredconcurrency.scope
45
import junit.framework.Assert.assertEquals
56
import kotlinx.coroutines.cancel
67
import kotlinx.coroutines.launch
@@ -33,36 +34,37 @@ class AndroidVersionRepositoryTest {
3334
}
3435

3536
@Test
36-
fun `loadRecentAndroidVersions() should continue to load and store android versions when calling scope gets cancelled`() = runTest {
37-
val fakeDatabase = FakeDatabase()
38-
val fakeApi = FakeApi()
39-
val applicationScope = this
40-
val repository = AndroidVersionRepository(
41-
fakeDatabase,
42-
applicationScope,
43-
api = fakeApi
44-
)
37+
fun `loadRecentAndroidVersions() should continue to load and store android versions when calling scope gets cancelled`() =
38+
runTest {
39+
val fakeDatabase = FakeDatabase()
40+
val fakeApi = FakeApi()
4541

46-
// Sharing the testScheduler with the applicationScope is important!
47-
val viewModelScope = TestScope(this.testScheduler)
48-
val job = viewModelScope.launch {
49-
repository.loadAndStoreRemoteAndroidVersions()
50-
}
42+
val repository = AndroidVersionRepository(
43+
database = fakeDatabase,
44+
scope = this,
45+
api = fakeApi
46+
)
5147

52-
// execute coroutine until delay(1) in the fakeApi
53-
applicationScope.runCurrent()
48+
// Sharing the testScheduler with the applicationScope is important!
49+
val viewModelScope = TestScope(this.testScheduler)
50+
val job = viewModelScope.launch {
51+
repository.loadAndStoreRemoteAndroidVersions()
52+
}
5453

55-
// Check if nothing is inserted into the db before we cancel the scope
56-
assertEquals(false, fakeDatabase.insertedIntoDb)
54+
// execute coroutine until delay(1) in the fakeApi
55+
runCurrent()
5756

58-
// Cancel the scope and check if it was indeed cancelled
59-
viewModelScope.cancel()
60-
assertEquals(true, job.isCancelled)
57+
// Check if nothing is inserted into the db before we cancel the scope
58+
assertEquals(false, fakeDatabase.insertedIntoDb)
6159

62-
// continue coroutine execution after delay(1) in the fakeApi
63-
applicationScope.advanceTimeBy(1)
64-
applicationScope.runCurrent()
60+
// Cancel the scope and check if it was indeed cancelled
61+
viewModelScope.cancel()
62+
assertEquals(true, job.isCancelled)
6563

66-
assertEquals(true, fakeDatabase.insertedIntoDb)
67-
}
64+
// continue coroutine execution after delay(1) in the fakeApi
65+
advanceTimeBy(1)
66+
runCurrent()
67+
68+
assertEquals(true, fakeDatabase.insertedIntoDb)
69+
}
6870
}

0 commit comments

Comments
 (0)