Skip to content

Remove most usages of TestDispatcher in paging-common tests #523

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

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 @@ -27,20 +27,22 @@ import androidx.paging.PagedList.Config
import androidx.paging.PagingSource.LoadParams.Refresh
import androidx.paging.PagingSource.LoadResult
import androidx.paging.PagingSource.LoadResult.Page
import androidx.testutils.TestDispatcher
import com.google.common.truth.Truth.assertThat
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.test.StandardTestDispatcher
import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.JUnit4

@OptIn(ExperimentalCoroutinesApi::class)
@RunWith(JUnit4::class)
class LegacyPageFetcherTest {
private val testDispatcher = TestDispatcher()
private val testDispatcher = StandardTestDispatcher()
private val data = List(9) { "$it" }

inner class ImmediateListDataSource(val data: List<String>) : PagingSource<Int, String>() {
Expand Down Expand Up @@ -177,7 +179,7 @@ class LegacyPageFetcherTest {
consumer.takeStateChanges()
)

testDispatcher.executeAll()
testDispatcher.scheduler.advanceUntilIdle()

assertEquals(listOf(Result(APPEND, rangeResult(6, 8))), consumer.takeResults())
assertEquals(
Expand All @@ -204,7 +206,7 @@ class LegacyPageFetcherTest {
consumer.takeStateChanges()
)

testDispatcher.executeAll()
testDispatcher.scheduler.advanceUntilIdle()

assertEquals(
listOf(Result(PREPEND, rangeResult(2, 4))),
Expand All @@ -227,7 +229,7 @@ class LegacyPageFetcherTest {
val pager = createPager(consumer, 2, 6)

pager.tryScheduleAppend()
testDispatcher.executeAll()
testDispatcher.scheduler.advanceUntilIdle()

assertEquals(
listOf(
Expand All @@ -248,7 +250,7 @@ class LegacyPageFetcherTest {
)

pager.tryScheduleAppend()
testDispatcher.executeAll()
testDispatcher.scheduler.advanceUntilIdle()

assertEquals(
listOf(
Expand All @@ -275,7 +277,7 @@ class LegacyPageFetcherTest {
val pager = createPager(consumer, 4, 8)

pager.trySchedulePrepend()
testDispatcher.executeAll()
testDispatcher.scheduler.advanceUntilIdle()

assertEquals(
listOf(
Expand All @@ -295,7 +297,7 @@ class LegacyPageFetcherTest {
)

pager.trySchedulePrepend()
testDispatcher.executeAll()
testDispatcher.scheduler.advanceUntilIdle()

assertEquals(
listOf(
Expand Down Expand Up @@ -364,7 +366,7 @@ class LegacyPageFetcherTest {

// try a normal append first
pager.tryScheduleAppend()
testDispatcher.executeAll()
testDispatcher.scheduler.advanceUntilIdle()

assertThat(consumer.takeResults()).containsExactly(
Result(APPEND, rangeResult(3, 5))
Expand All @@ -379,7 +381,7 @@ class LegacyPageFetcherTest {
pagingSource.invalidData = true

pager.tryScheduleAppend()
testDispatcher.executeAll()
testDispatcher.scheduler.advanceUntilIdle()

// the load should return before returning any data
assertThat(consumer.takeResults()).isEmpty()
Expand All @@ -400,7 +402,7 @@ class LegacyPageFetcherTest {

// try a normal prepend first
pager.trySchedulePrepend()
testDispatcher.executeAll()
testDispatcher.scheduler.advanceUntilIdle()

assertThat(consumer.takeResults()).containsExactly(
Result(PREPEND, rangeResult(4, 6))
Expand All @@ -415,7 +417,7 @@ class LegacyPageFetcherTest {
pagingSource.invalidData = true

pager.trySchedulePrepend()
testDispatcher.executeAll()
testDispatcher.scheduler.advanceUntilIdle()

// the load should return before returning any data
assertThat(consumer.takeResults()).isEmpty()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package androidx.paging

import androidx.paging.PagingSource.LoadResult.Page
import androidx.testutils.TestDispatcher
import com.google.common.truth.Truth.assertThat
import kotlin.coroutines.EmptyCoroutineContext
import kotlinx.coroutines.Dispatchers
Expand All @@ -27,6 +26,7 @@ import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.take
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.test.StandardTestDispatcher
import org.junit.Assert
import org.junit.Test
import org.junit.runner.RunWith
Expand All @@ -35,7 +35,9 @@ import java.util.concurrent.Executors
import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertTrue
import kotlinx.coroutines.ExperimentalCoroutinesApi

@OptIn(ExperimentalCoroutinesApi::class)
@RunWith(JUnit4::class)
class LegacyPagingSourceTest {
private val fakePagingState = PagingState(
Expand Down Expand Up @@ -357,22 +359,22 @@ class LegacyPagingSourceTest {
}
}

val testDispatcher = TestDispatcher()
val testDispatcher = StandardTestDispatcher()
val pagingSourceFactory = dataSourceFactory.asPagingSourceFactory(
fetchDispatcher = testDispatcher
).let {
{ it() as LegacyPagingSource }
}

val pagingSource0 = pagingSourceFactory()
testDispatcher.executeAll()
testDispatcher.scheduler.advanceUntilIdle()
assertTrue { pagingSource0.dataSource.isInvalid }
assertTrue { pagingSource0.invalid }
assertTrue { dataSourceFactory.dataSources[0].isInvalid }
assertEquals(dataSourceFactory.dataSources[0], pagingSource0.dataSource)

val pagingSource1 = pagingSourceFactory()
testDispatcher.executeAll()
testDispatcher.scheduler.advanceUntilIdle()
assertFalse { pagingSource1.dataSource.isInvalid }
assertFalse { pagingSource1.invalid }
assertFalse { dataSourceFactory.dataSources[1].isInvalid }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package androidx.paging

import androidx.testutils.TestDispatcher
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
Expand All @@ -33,12 +32,11 @@ import org.mockito.Mockito.verify
import org.mockito.Mockito.verifyNoMoreInteractions
import kotlin.coroutines.EmptyCoroutineContext
import kotlin.test.assertFailsWith
import kotlinx.coroutines.test.StandardTestDispatcher

@OptIn(ExperimentalCoroutinesApi::class)
@RunWith(JUnit4::class)
class PageKeyedDataSourceTest {
private val mainThread = TestDispatcher()
private val backgroundThread = TestDispatcher()

internal data class Item(val name: String)

internal data class Page(val prev: String?, val data: List<Item>, val next: String?)
Expand Down Expand Up @@ -244,7 +242,7 @@ class PageKeyedDataSourceTest {
@Suppress("UNCHECKED_CAST", "DEPRECATION")
val boundaryCallback =
mock(PagedList.BoundaryCallback::class.java) as PagedList.BoundaryCallback<String>
val dispatcher = TestDispatcher()
val dispatcher = StandardTestDispatcher()

val testCoroutineScope = CoroutineScope(EmptyCoroutineContext)
@Suppress("DEPRECATION")
Expand All @@ -259,7 +257,7 @@ class PageKeyedDataSourceTest {

verifyNoMoreInteractions(boundaryCallback)

dispatcher.executeAll()
dispatcher.scheduler.advanceUntilIdle()

// verify boundary callbacks are triggered
verify(boundaryCallback).onItemAtFrontLoaded("A")
Expand Down Expand Up @@ -297,7 +295,7 @@ class PageKeyedDataSourceTest {
@Suppress("UNCHECKED_CAST", "DEPRECATION")
val boundaryCallback =
mock(PagedList.BoundaryCallback::class.java) as PagedList.BoundaryCallback<String>
val dispatcher = TestDispatcher()
val dispatcher = StandardTestDispatcher()

val testCoroutineScope = CoroutineScope(EmptyCoroutineContext)
@Suppress("DEPRECATION")
Expand All @@ -312,7 +310,7 @@ class PageKeyedDataSourceTest {

verifyNoMoreInteractions(boundaryCallback)

dispatcher.executeAll()
dispatcher.scheduler.advanceUntilIdle()

// verify boundary callbacks are triggered
verify(boundaryCallback).onItemAtFrontLoaded("B")
Expand Down Expand Up @@ -512,11 +510,4 @@ class PageKeyedDataSourceTest {
ITEM_LIST = list
}
}

private fun drain() {
while (backgroundThread.queue.isNotEmpty() || mainThread.queue.isNotEmpty()) {
backgroundThread.executeAll()
mainThread.executeAll()
}
}
}
Loading