@@ -21,19 +21,21 @@ import kotlin.test.Ignore
2121import kotlin.test.Test
2222import kotlinx.coroutines.CompletableDeferred
2323import kotlinx.coroutines.CoroutineScope
24+ import kotlinx.coroutines.ExperimentalCoroutinesApi
2425import kotlinx.coroutines.cancel
2526import kotlinx.coroutines.cancelAndJoin
2627import kotlinx.coroutines.flow.Flow
2728import kotlinx.coroutines.flow.collectLatest
2829import kotlinx.coroutines.flow.takeWhile
2930import kotlinx.coroutines.launch
30- import kotlinx.coroutines.runBlocking
31+ import kotlinx.coroutines.test.runTest
3132import org.junit.runner.RunWith
3233import org.junit.runners.JUnit4
3334
3435/* *
3536 * reproduces b/203594733
3637 */
38+ @OptIn(ExperimentalCoroutinesApi ::class )
3739@RunWith(JUnit4 ::class )
3840public 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}
0 commit comments