[go: nahoru, domu]

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace most runBlocking calls with runTest in paging-common #555

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,21 @@ import kotlin.test.Ignore
import kotlin.test.Test
import kotlinx.coroutines.CompletableDeferred
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.cancel
import kotlinx.coroutines.cancelAndJoin
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.takeWhile
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.test.runTest
import org.junit.runner.RunWith
import org.junit.runners.JUnit4

/**
* reproduces b/203594733
*/
@OptIn(ExperimentalCoroutinesApi::class)
@RunWith(JUnit4::class)
public class CachedPageEventFlowLeakTest {
private val gcHelper = GarbageCollectionTestHelper()
Expand Down Expand Up @@ -136,17 +138,15 @@ public class CachedPageEventFlowLeakTest {

@Ignore("b/206837348")
@Test
public fun dontLeakCachedPageEventFlows_finished() {
public fun dontLeakCachedPageEventFlows_finished() = runTest {
val scope = CoroutineScope(EmptyCoroutineContext)
val flow = pager.flow.cachedIn(scope, tracker)
runBlocking {
collectPages(
flow = flow,
generationCount = 20,
doneInvalidating = null,
finishCollecting = true
)
}
collectPages(
flow = flow,
generationCount = 20,
doneInvalidating = null,
finishCollecting = true
)
gcHelper.assertLiveObjects(
// see b/204125064
// this should ideally be 0 but right now, we keep the previous generation's state
Expand All @@ -159,72 +159,66 @@ public class CachedPageEventFlowLeakTest {
}

@Test
public fun dontLeakNonCachedFlow_finished() {
runBlocking {
collectPages(
flow = pager.flow,
generationCount = 10,
doneInvalidating = null,
finishCollecting = true
)
}
public fun dontLeakNonCachedFlow_finished() = runTest {
collectPages(
flow = pager.flow,
generationCount = 10,
doneInvalidating = null,
finishCollecting = true
)
gcHelper.assertEverythingIsCollected()
}

@Test
public fun dontLeakPreviousPageInfo_stillCollecting() {
public fun dontLeakPreviousPageInfo_stillCollecting() = runTest {
// reproduces b/204125064
runBlocking {
val doneInvalidating = CompletableDeferred<Unit>()
val collection = launch {
collectPages(
flow = pager.flow,
generationCount = 10,
doneInvalidating = doneInvalidating,
finishCollecting = false
)
}
// make sure we collected enough generations
doneInvalidating.await()
gcHelper.assertLiveObjects(
// see b/204125064
// this should ideally be 0 but right now, we keep the previous generation's state
// to be able to find anchor for the new position but we don't clear it yet. It can
// only be cleared after the new generation loads a page.
Item::class to 20
val doneInvalidating = CompletableDeferred<Unit>()
val collection = launch {
collectPages(
flow = pager.flow,
generationCount = 10,
doneInvalidating = doneInvalidating,
finishCollecting = false
)
collection.cancelAndJoin()
}
// make sure we collected enough generations
doneInvalidating.await()
gcHelper.assertLiveObjects(
// see b/204125064
// this should ideally be 0 but right now, we keep the previous generation's state
// to be able to find anchor for the new position but we don't clear it yet. It can
// only be cleared after the new generation loads a page.
Item::class to 20
)
collection.cancelAndJoin()
}

// Broken: b/206981029
@Ignore
@Test
public fun dontLeakPreviousPageInfoWithCache_stillCollecting() {
public fun dontLeakPreviousPageInfoWithCache_stillCollecting() = runTest {
// reproduces b/204125064
val scope = CoroutineScope(EmptyCoroutineContext)
val flow = pager.flow.cachedIn(scope, tracker)
// reproduces b/204125064
runBlocking {
val doneInvalidating = CompletableDeferred<Unit>()
val collection = launch {
collectPages(
flow = flow,
generationCount = 10,
doneInvalidating = doneInvalidating,
finishCollecting = false
)
}
// make sure we collected enough generations
doneInvalidating.await()
gcHelper.assertLiveObjects(
// see b/204125064
// this should ideally be 0 but right now, we keep the previous generation's state
// to be able to find anchor for the new position but we don't clear it yet. It can
// only be cleared after the new generation loads a page.
Item::class to 20,
CachedPageEventFlow::class to 1
val doneInvalidating = CompletableDeferred<Unit>()
val collection = launch {
collectPages(
flow = flow,
generationCount = 10,
doneInvalidating = doneInvalidating,
finishCollecting = false
)
collection.cancelAndJoin()
}
// make sure we collected enough generations
doneInvalidating.await()
gcHelper.assertLiveObjects(
// see b/204125064
// this should ideally be 0 but right now, we keep the previous generation's state
// to be able to find anchor for the new position but we don't clear it yet. It can
// only be cleared after the new generation loads a page.
Item::class to 20,
CachedPageEventFlow::class to 1
)
collection.cancelAndJoin()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ import kotlinx.coroutines.flow.mapLatest
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.flow.toList
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.test.TestScope
import kotlinx.coroutines.test.UnconfinedTestDispatcher
import kotlinx.coroutines.test.runCurrent
Expand Down Expand Up @@ -365,25 +364,21 @@ class CachingTest {
}

@Test
fun pagesAreClosedProperty() {
fun pagesAreClosedProperty() = testScope.runTest {
val job = SupervisorJob()
val subScope = CoroutineScope(job + Dispatchers.Default)
val pageFlow = buildPageFlow().cachedIn(subScope, tracker)
assertThat(tracker.pageEventFlowCount()).isEqualTo(0)
assertThat(tracker.pageDataFlowCount()).isEqualTo(0)
val items = runBlocking {
pageFlow.collectItemsUntilSize(9)
}
val items = pageFlow.collectItemsUntilSize(9)
val firstList = buildItems(
version = 0,
generation = 0,
start = 0,
size = 9
)
assertThat(tracker.pageDataFlowCount()).isEqualTo(1)
val items2 = runBlocking {
pageFlow.collectItemsUntilSize(21)
}
val items2 = pageFlow.collectItemsUntilSize(21)
assertThat(items2).isEqualTo(
buildItems(
version = 0,
Expand All @@ -395,9 +390,7 @@ class CachingTest {
assertThat(tracker.pageEventFlowCount()).isEqualTo(0)
assertThat(tracker.pageDataFlowCount()).isEqualTo(1)
assertThat(items).isEqualTo(firstList)
runBlocking {
job.cancelAndJoin()
}
job.cancelAndJoin()
assertThat(tracker.pageEventFlowCount()).isEqualTo(0)
assertThat(tracker.pageDataFlowCount()).isEqualTo(0)
}
Expand Down
Loading