Monday, 6 December 2021

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

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>


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

No comments:

Post a Comment