Why should AsyncPagingDataDiffer submitData() freeze and timeout the test?

952 views Asked by At

I'm trying to follow this documentation here concerning how to unit test a PagingData stream on which you're applying transforms. The code I am using is similar:

@ExperimentalCoroutinesApi
@Test
fun testGetFooBarsPaged(): Unit = runTest {
    val differ = AsyncPagingDataDiffer(
        object : DiffUtil.ItemCallback<FooBar>() {
            override fun areItemsTheSame(oldItem: FooBar, newItem: FooBar) = false
            override fun areContentsTheSame(oldItem: FooBar, newItem: FooBar) = false
        },
        object : ListUpdateCallback {
            override fun onChanged(position: Int, count: Int, payload: Any?) {}
            override fun onMoved(fromPosition: Int, toPosition: Int) {}
            override fun onInserted(position: Int, count: Int) {}
            override fun onRemoved(position: Int, count: Int) {}
        }
    )
    val pagingData = fooBarsSdk.getFooBarsPaged(FooBar.Type, pageSize = 1).first()
    differ.submitData(PagingData.from(listOf(FooBar(), FooBar(), FooBar())))
}

The Last two lines in the test here execute and the test terminates successfully:

  1. getFooBarsPaged() retrieves a Flow<PagingData>, on which I can call first() and retrieve the first instance of PagingData<FooBar>.
  2. And on the next line, you can see I can also submit some arbitrary list of FooBar objects to my AsyncPagingDataDiffer object without any problem.

The problem is when I try to submit the PagingData instance I took from the Flow to the AsyncPagingDataDiffer object: differ.submitData(pagingData). This invocation blocks until runTest() timesout after 60 seconds. My question is, why would that happen?

The Pager object and Flow are created like this:

Pager(
    config = PagingConfig(pageSize),
    initialKey = null,
    pagingSourceFactory = factory.asPagingSourceFactory(Dispatchers.Default)
).flow.map { pagingData -> pagingData.map { fooBarEntity -> fooBarEntity.toFooBar() } }

where the data-source factory is retrieved from a Room Dao method such as this:

@Query("SELECT DISTINCT * FROM foobars WHERE type = :type")
fun getAllFooBarsByTypeLive(type: String): DataSource.Factory<Int, FooBarEntity>
1

There are 1 answers

0
sucicf1 On BEST ANSWER

the following works here:

val job = launch {
        val pagingData = Pager(
            config = PagingConfig(20),
            initialKey = null,
            pagingSourceFactory = db.fooBarDao().getAllFooBarsByTypeLive("test")
                .asPagingSourceFactory()
        ).flow.map { pagingData -> pagingData.map { fooBarEntity -> fooBarEntity.toFooBar() } }
            .toList()[2]
        differ.submitData(pagingData)
    }
    job.cancel()

Of corse je need at least 3 elements in your flow. How To Test PagingData From Paging 3

The reason why

val pagingData = fooBarsSdk.getFooBarsPaged(FooBar.Type, pageSize = 1).first()
differ.submitData(PagingData.from(listOf(FooBar(), FooBar(), FooBar())))

works is that you are using "from" and then you don't need launch as you can read here https://developer.android.com/topic/libraries/architecture/paging/test