8000 [GH] Replace most runBlocking calls with runTest in `paging-common` · androidx/androidx@143a6f5 · GitHub
[go: up one dir, main page]

Skip to content

Commit 143a6f5

Browse files
veyndancopybara-github
authored andcommitted
[GH] Replace most runBlocking calls with runTest in paging-common
The below test is the only remaining instance of `runBlocking` in `paging-common`. The rest of the test uses a lot of Java specific stuff, and in Multiplatform Paging, I hadn't attempted to multiplatformize it yet. https://github.com/androidx/androidx/blob/88877d72f62df51fc9dd040da322c9046aa81f67/paging/paging-common/src/test/kotlin/androidx/paging/SingleRunnerTest.kt#L151-L190 Test: ./gradlew test connectedCheck Bug: 270612487 This is an imported pull request from #555. Resolves #555 Github-Pr-Head-Sha: 4749aac GitOrigin-RevId: 3a59fb4 Change-Id: Ic8275ee6a26e870d71cbf4bc57275db84b14b031
1 parent fa5d1a8 commit 143a6f5

12 files changed

+308
-333
lines changed

paging/paging-common/src/test/kotlin/androidx/paging/CachedPageEventFlowLeakTest.kt

Lines changed: 55 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,21 @@ import kotlin.test.Ignore
2121
import kotlin.test.Test
2222
import kotlinx.coroutines.CompletableDeferred
2323
import kotlinx.coroutines.CoroutineScope
24+
import kotlinx.coroutines.ExperimentalCoroutinesApi
2425
import kotlinx.coroutines.cancel
2526
import kotlinx.coroutines.cancelAndJoin
2627
import kotlinx.coroutines.flow.Flow
2728
import kotlinx.coroutines.flow.collectLatest
2829
import kotlinx.coroutines.flow.takeWhile
2930
import kotlinx.coroutines.launch
30-
import kotlinx.coroutines.runBlocking
31+
import kotlinx.coroutines.test.runTest
3132
import org.junit.runner.RunWith
3233
import org.junit.runners.JUnit4
3334

3435
/**
3536
* reproduces b/203594733
3637
*/
38+
@OptIn(ExperimentalCoroutinesApi::class)
3739
@RunWith(JUnit4::class)
3840
public class CachedPageEventFlowLeakTest {
3941
private val gcHelper = GarbageCollectionTestHelper()
@@ -136,17 +138,15 @@ public class CachedPageEventFlowLeakTest {
136138

137139
@Ignore("b/206837348")
138140
@Test
139-
public fun dontLeakCachedPageEventFlows_finished() {
141+
public fun dontLeakCachedPageEventFlows_finished() = runTest {
140142
val scope = CoroutineScope(EmptyCoroutineContext)
141143
val flow = pager.flow.cachedIn(scope, tracker)
142-
runBlocking {
143-
collectPages(
144-
flow = flow,
145-
generationCount = 20,
146-
doneInvalidating = null,
147-
finishCollecting = true
148-
)
149-
}
144+
collectPages(
145+
flow = flow,
146+
generationCount = 20,
147+
doneInvalidating = null,
148+
finishCollecting = true
149+
)
150150
gcHelper.assertLiveObjects(
151151
// see b/204125064
152152
// this should ideally be 0 but right now, we keep the previous generation's state
@@ -159,72 +159,66 @@ public class CachedPageEventFlowLeakTest {
159159
}
160160

161161
@Test
162-
public fun dontLeakNonCachedFlow_finished() {
163-
runBlocking {
164-
collectPages(
165-
flow = pager.flow,
166-
generationCount = 10,
167-
doneInvalidating = null,
168-
finishCollecting = true
169-
)
170-
}
162+
public fun dontLeakNonCachedFlow_finished() = runTest {
163+
collectPages(
164+
flow = pager.flow,
165+
generationCount = 10,
166+
doneInvalidating = null,
167+
finishCollecting = true
168+
)
171169
gcHelper.assertEverythingIsCollected()
172170
}
173171

174172
@Test
175-
public fun dontLeakPreviousPageInfo_stillCollecting() {
173+
public fun dontLeakPreviousPageInfo_stillCollecting() = runTest {
176174
// reproduces b/204125064
177-
runBlocking {
178-
val doneInvalidating = CompletableDeferred<Unit>()
179-
val collection = launch {
180-
collectPages(
181-
flow = pager.flow,
182-
generationCount = 10,
183-
doneInvalidating = doneInvalidating,
184-
finishCollecting = false
185-
)
186-
}
187-
// make sure we collected enough generations
188-
doneInvalidating.await()
189-
gcHelper.assertLiveObjects(
190-
// see b/204125064
191-
// this should ideally be 0 but right now, we keep the previous generation's state
192-
// to be able to find anchor for the new position but we don't clear it yet. It can
193-
// only be cleared after the new generation loads a page.
194-
Item::class to 20
175+
val doneInvalidating = CompletableDeferred<Unit>()
176+
val collection = launch {
177+
collectPages(
178+
flow = pager.flow,
179+
generationCount = 10,
180+
doneInvalidating = doneInvalidating,
181+
finishCollecting = false
195182
)
196-
collection.cancelAndJoin()
197183
}
184+
// make sure we collected enough generations
185+
doneInvalidating.await()
186+
gcHelper.assertLiveObjects(
187+
// see b/204125064
188+
// this should ideally be 0 but right now, we keep the previous generation's state
189+
// to be able to find anchor for the new position but we don't clear it yet. It can
190+
// only be cleared after the new generation loads a page.
191+
Item::class to 20
192+
)
193+
collection.cancelAndJoin()
198194
}
199195

200196
// Broken: b/206981029
201197
@Ignore
202198
@Test
203-
public fun dontLeakPreviousPageInfoWithCache_stillCollecting() {
199+
public fun dontLeakPreviousPageInfoWithCache_stillCollecting() = runTest {
200+
// reproduces b/204125064
204201
val scope = CoroutineScope(EmptyCoroutineContext)
205202
val flow = pager.flow.cachedIn(scope, tracker)
206-
// reproduces b/204125064
207-
runBlocking {
208-
val doneInvalidating = CompletableDeferred<Unit>()
209-
val collection = launch {
210-
collectPages(
211-
flow = flow,
212-
generationCount = 10,
213-
doneInvalidating = doneInvalidating,
214-
finishCollecting = false
215-
)
216-
}
217-
// make sure we collected enough generations
218-
doneInvalidating.await()
219-
gcHelper.assertLiveObjects(
220-
// see b/204125064
221-
// this should ideally be 0 but right now, we keep the previous generation's state
222-
// to be able to find anchor for the new position but we don't clear it yet. It can
223-
// only be cleared after the new generation loads a page.
224-
Item::class to 20,
225-
CachedPageEventFlow::class to 1
203+
val doneInvalidating = CompletableDeferred<Unit>()
204+
val collection = launch {
205+
collectPages(
206+
flow = flow,
207+
generationCount = 10,
208+
doneInvalidating = doneInvalidating,
209+
finishCollecting = false
226210
)
227-
collection.cancelAndJoin()
228211
}
212+
// make sure we collected enough generations
213+
doneInvalidating.await()
214+
gcHelper.assertLiveObjects(
215+
// see b/204125064
216+
// this should ideally be 0 but right now, we keep the previous generation's state
217+
// to be able to find anchor for the new position but we don't clear it yet. It can
218+
// only be cleared after the new generation loads a page.
219+
Item::class to 20,
220+
CachedPageEventFlow::class to 1
221+
)
222+
collection.cancelAndJoin()
229223
}
230224
}

paging/paging-common/src/test/kotlin/androidx/paging/CachingTest.kt

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ import kotlinx.coroutines.flow.mapLatest
3737
import kotlinx.coroutines.flow.onEach
3838
import kotlinx.coroutines.flow.toList
3939
import kotlinx.coroutines.launch
40-
import kotlinx.coroutines.runBlocking
4140
import kotlinx.coroutines.test.TestScope
4241
import kotlinx.coroutines.test.UnconfinedTestDispatcher
4342
import kotlinx.coroutines.test.runCurrent
@@ -365,25 +364,21 @@ class CachingTest {
365364
}
366365

367366
@Test
368-
fun pagesAreClosedProperty() {
367+
fun pagesAreClosedProperty() = testScope.runTest {
369368
val job = SupervisorJob()
370369
val subScope = CoroutineScope(job + Dispatchers.Default)
371370
val pageFlow = buildPageFlow().cachedIn(subScope, tracker)
372371
assertThat(tracker.pageEventFlowCount()).isEqualTo(0)
373372
assertThat(tracker.pageDataFlowCount()).isEqualTo(0)
374-
val items = runBlocking {
375-
pageFlow.collectItemsUntilSize(9)
376-
}
373+
val items = pageFlow.collectItemsUntilSize(9)
377374
val firstList = buildItems(
378375
version = 0,
379376
generation = 0,
380377
start = 0,
381378
size = 9
382379
)
383380
assertThat(tracker.pageDataFlowCount()).isEqualTo(1)
384-
val items2 = runBlocking {
385-
pageFlow.collectItemsUntilSize(21)
386-
}
381+
val items2 = pageFlow.collectItemsUntilSize(21)
387382
assertThat(items2).isEqualTo(
388383
buildItems(
389384
version = 0,
@@ -395,9 +390,7 @@ class CachingTest {
395390
assertThat(tracker.pageEventFlowCount()).isEqualTo(0)
396391
assertThat(tracker.pageDataFlowCount()).isEqualTo(1)
397392
assertThat(items).isEqualTo(firstList)
398-
runBlocking {
399-
job.cancelAndJoin()
400-
}
393+
job.cancelAndJoin()
401394
assertThat(tracker.pageEventFlowCount()).isEqualTo(0)
402395
assertThat(tracker.pageDataFlowCount()).isEqualTo(0)
403396
}

0 commit comments

Comments
 (0)
0