Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

430
Vistas
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>
over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

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

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda