Skip to content

Commit 9b82469

Browse files
committed
Remove most usages of TestDispatcher in paging-common tests
1 parent 2c2b206 commit 9b82469

File tree

4 files changed

+66
-71
lines changed

4 files changed

+66
-71
lines changed

paging/paging-common/src/test/kotlin/androidx/paging/LegacyPageFetcherTest.kt

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,22 @@ import androidx.paging.PagedList.Config
2727
import androidx.paging.PagingSource.LoadParams.Refresh
2828
import androidx.paging.PagingSource.LoadResult
2929
import androidx.paging.PagingSource.LoadResult.Page
30-
import androidx.testutils.TestDispatcher
3130
import com.google.common.truth.Truth.assertThat
3231
import kotlinx.coroutines.DelicateCoroutinesApi
32+
import kotlinx.coroutines.ExperimentalCoroutinesApi
3333
import kotlinx.coroutines.GlobalScope
3434
import kotlinx.coroutines.runBlocking
35+
import kotlinx.coroutines.test.StandardTestDispatcher
3536
import org.junit.Assert.assertEquals
3637
import org.junit.Assert.assertTrue
3738
import org.junit.Test
3839
import org.junit.runner.RunWith
3940
import org.junit.runners.JUnit4
4041

42+
@OptIn(ExperimentalCoroutinesApi::class)
4143
@RunWith(JUnit4::class)
4244
class LegacyPageFetcherTest {
43-
private val testDispatcher = TestDispatcher()
45+
private val testDispatcher = StandardTestDispatcher()
4446
private val data = List(9) { "$it" }
4547

4648
inner class ImmediateListDataSource(val data: List<String>) : PagingSource<Int, String>() {
@@ -177,7 +179,7 @@ class LegacyPageFetcherTest {
177179
consumer.takeStateChanges()
178180
)
179181

180-
testDispatcher.executeAll()
182+
testDispatcher.scheduler.advanceUntilIdle()
181183

182184
assertEquals(listOf(Result(APPEND, rangeResult(6, 8))), consumer.takeResults())
183185
assertEquals(
@@ -204,7 +206,7 @@ class LegacyPageFetcherTest {
204206
consumer.takeStateChanges()
205207
)
206208

207-
testDispatcher.executeAll()
209+
testDispatcher.scheduler.advanceUntilIdle()
208210

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

229231
pager.tryScheduleAppend()
230-
testDispatcher.executeAll()
232+
testDispatcher.scheduler.advanceUntilIdle()
231233

232234
assertEquals(
233235
listOf(
@@ -248,7 +250,7 @@ class LegacyPageFetcherTest {
248250
)
249251

250252
pager.tryScheduleAppend()
251-
testDispatcher.executeAll()
253+
testDispatcher.scheduler.advanceUntilIdle()
252254

253255
assertEquals(
254256
listOf(
@@ -275,7 +277,7 @@ class LegacyPageFetcherTest {
275277
val pager = createPager(consumer, 4, 8)
276278

277279
pager.trySchedulePrepend()
278-
testDispatcher.executeAll()
280+
testDispatcher.scheduler.advanceUntilIdle()
279281

280282
assertEquals(
281283
listOf(
@@ -295,7 +297,7 @@ class LegacyPageFetcherTest {
295297
)
296298

297299
pager.trySchedulePrepend()
298-
testDispatcher.executeAll()
300+
testDispatcher.scheduler.advanceUntilIdle()
299301

300302
assertEquals(
301303
listOf(
@@ -364,7 +366,7 @@ class LegacyPageFetcherTest {
364366

365367
// try a normal append first
366368
pager.tryScheduleAppend()
367-
testDispatcher.executeAll()
369+
testDispatcher.scheduler.advanceUntilIdle()
368370

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

381383
pager.tryScheduleAppend()
382-
testDispatcher.executeAll()
384+
testDispatcher.scheduler.advanceUntilIdle()
383385

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

401403
// try a normal prepend first
402404
pager.trySchedulePrepend()
403-
testDispatcher.executeAll()
405+
testDispatcher.scheduler.advanceUntilIdle()
404406

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

417419
pager.trySchedulePrepend()
418-
testDispatcher.executeAll()
420+
testDispatcher.scheduler.advanceUntilIdle()
419421

420422
// the load should return before returning any data
421423
assertThat(consumer.takeResults()).isEmpty()

paging/paging-common/src/test/kotlin/androidx/paging/LegacyPagingSourceTest.kt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package androidx.paging
1818

1919
import androidx.paging.PagingSource.LoadResult.Page
20-
import androidx.testutils.TestDispatcher
2120
import com.google.common.truth.Truth.assertThat
2221
import kotlin.coroutines.EmptyCoroutineContext
2322
import kotlinx.coroutines.Dispatchers
@@ -27,6 +26,7 @@ import kotlinx.coroutines.flow.filter
2726
import kotlinx.coroutines.flow.first
2827
import kotlinx.coroutines.flow.take
2928
import kotlinx.coroutines.runBlocking
29+
import kotlinx.coroutines.test.StandardTestDispatcher
3030
import org.junit.Assert
3131
import org.junit.Test
3232
import org.junit.runner.RunWith
@@ -35,7 +35,9 @@ import java.util.concurrent.Executors
3535
import kotlin.test.assertEquals
3636
import kotlin.test.assertFalse
3737
import kotlin.test.assertTrue
38+
import kotlinx.coroutines.ExperimentalCoroutinesApi
3839

40+
@OptIn(ExperimentalCoroutinesApi::class)
3941
@RunWith(JUnit4::class)
4042
class LegacyPagingSourceTest {
4143
private val fakePagingState = PagingState(
@@ -357,22 +359,22 @@ class LegacyPagingSourceTest {
357359
}
358360
}
359361

360-
val testDispatcher = TestDispatcher()
362+
val testDispatcher = StandardTestDispatcher()
361363
val pagingSourceFactory = dataSourceFactory.asPagingSourceFactory(
362364
fetchDispatcher = testDispatcher
363365
).let {
364366
{ it() as LegacyPagingSource }
365367
}
366368

367369
val pagingSource0 = pagingSourceFactory()
368-
testDispatcher.executeAll()
370+
testDispatcher.scheduler.advanceUntilIdle()
369371
assertTrue { pagingSource0.dataSource.isInvalid }
370372
assertTrue { pagingSource0.invalid }
371373
assertTrue { dataSourceFactory.dataSources[0].isInvalid }
372374
assertEquals(dataSourceFactory.dataSources[0], pagingSource0.dataSource)
373375

374376
val pagingSource1 = pagingSourceFactory()
375-
testDispatcher.executeAll()
377+
testDispatcher.scheduler.advanceUntilIdle()
376378
assertFalse { pagingSource1.dataSource.isInvalid }
377379
assertFalse { pagingSource1.invalid }
378380
assertFalse { dataSourceFactory.dataSources[1].isInvalid }

paging/paging-common/src/test/kotlin/androidx/paging/PageKeyedDataSourceTest.kt

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
package androidx.paging
1818

19-
import androidx.testutils.TestDispatcher
2019
import kotlinx.coroutines.CoroutineScope
2120
import kotlinx.coroutines.Dispatchers
2221
import kotlinx.coroutines.ExperimentalCoroutinesApi
@@ -33,12 +32,11 @@ import org.mockito.Mockito.verify
3332
import org.mockito.Mockito.verifyNoMoreInteractions
3433
import kotlin.coroutines.EmptyCoroutineContext
3534
import kotlin.test.assertFailsWith
35+
import kotlinx.coroutines.test.StandardTestDispatcher
3636

37+
@OptIn(ExperimentalCoroutinesApi::class)
3738
@RunWith(JUnit4::class)
3839
class PageKeyedDataSourceTest {
39-
private val mainThread = TestDispatcher()
40-
private val backgroundThread = TestDispatcher()
41-
4240
internal data class Item(val name: String)
4341

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

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

260258
verifyNoMoreInteractions(boundaryCallback)
261259

262-
dispatcher.executeAll()
260+
dispatcher.scheduler.advanceUntilIdle()
263261

264262
// verify boundary callbacks are triggered
265263
verify(boundaryCallback).onItemAtFrontLoaded("A")
@@ -297,7 +295,7 @@ class PageKeyedDataSourceTest {
297295
@Suppress("UNCHECKED_CAST", "DEPRECATION")
298296
val boundaryCallback =
299297
mock(PagedList.BoundaryCallback::class.java) as PagedList.BoundaryCallback<String>
300-
val dispatcher = TestDispatcher()
298+
val dispatcher = StandardTestDispatcher()
301299

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

313311
verifyNoMoreInteractions(boundaryCallback)
314312

315-
dispatcher.executeAll()
313+
dispatcher.scheduler.advanceUntilIdle()
316314

317315
// verify boundary callbacks are triggered
318316
verify(boundaryCallback).onItemAtFrontLoaded("B")
@@ -512,11 +510,4 @@ class PageKeyedDataSourceTest {
512510
ITEM_LIST = list
513511
}
514512
}
515-
516-
private fun drain() {
517-
while (backgroundThread.queue.isNotEmpty() || mainThread.queue.isNotEmpty()) {
518-
backgroundThread.executeAll()
519-
mainThread.executeAll()
520-
}
521-
}
522513
}

0 commit comments

Comments
 (0)