Fix flaky room-paging test loadEverything_inReverse

Flakiness because waiting for initial refresh would sometimes allow the
following prepend to complete, thus failing the assert on initially
loaded items.

Changed to assert against a snapshot of the refresh instead of peeking
at differ items.
Also refactored other tests' asserts on initial refresh to use
snapshot.

Test: ./gradlew :room:integration-tests:room-testapp-kotlin:cC
Fixes: 213635127
Change-Id: Ie320787180377e94e54c991adf124aa50fc4a08b
diff --git a/room/integration-tests/kotlintestapp/src/androidTest/java/androidx/room/integration/kotlintestapp/test/PagingSourceTest.kt b/room/integration-tests/kotlintestapp/src/androidTest/java/androidx/room/integration/kotlintestapp/test/PagingSourceTest.kt
index bd0ac39..bf212a9 100644
--- a/room/integration-tests/kotlintestapp/src/androidTest/java/androidx/room/integration/kotlintestapp/test/PagingSourceTest.kt
+++ b/room/integration-tests/kotlintestapp/src/androidTest/java/androidx/room/integration/kotlintestapp/test/PagingSourceTest.kt
@@ -17,6 +17,7 @@
 package androidx.room.integration.kotlintestapp.test
 
 import androidx.paging.AsyncPagingDataDiffer
+import androidx.paging.ItemSnapshotList
 import androidx.paging.LoadState
 import androidx.paging.Pager
 import androidx.paging.PagingConfig
@@ -42,7 +43,6 @@
 import androidx.test.core.app.ApplicationProvider
 import androidx.test.espresso.base.MainThread
 import androidx.test.ext.junit.runners.AndroidJUnit4
-import androidx.test.filters.FlakyTest
 import androidx.test.filters.MediumTest
 import androidx.testutils.FilteringExecutor
 import androidx.testutils.withTestTimeout
@@ -53,7 +53,6 @@
 import kotlinx.coroutines.cancel
 import kotlinx.coroutines.cancelAndJoin
 import kotlinx.coroutines.flow.MutableStateFlow
-import kotlinx.coroutines.flow.collect
 import kotlinx.coroutines.flow.collectLatest
 import kotlinx.coroutines.flow.distinctUntilChangedBy
 import kotlinx.coroutines.flow.drop
@@ -143,9 +142,9 @@
         val items = createItems(startId = 15, count = 50)
         db.dao.insert(items)
         runTest {
-            itemStore.awaitInitialLoad()
+            val initialLoad = itemStore.awaitInitialLoad()
             assertThat(
-                itemStore.peekItems()
+                initialLoad
             ).containsExactlyElementsIn(
                 items.createExpected(
                     fromIndex = 0,
@@ -180,9 +179,9 @@
             pagingSourceFactory = { db.dao.loadItemsRaw(query) }
         )
         runTest(pager = pager) {
-            itemStore.awaitInitialLoad()
+            val initialLoad = itemStore.awaitInitialLoad()
             assertThat(
-                itemStore.peekItems()
+                initialLoad
             ).containsExactlyElementsIn(
                 items.createExpected(
                     fromIndex = 0,
@@ -205,7 +204,6 @@
     }
 
     @Test
-    @FlakyTest(bugId = 213635127)
     fun loadEverything_inReverse() {
         // open db
         val items = createItems(startId = 0, count = 100)
@@ -217,9 +215,9 @@
             db.dao.loadItems()
         }
         runTest(pager) {
-            itemStore.awaitInitialLoad()
+            val initialLoad = itemStore.awaitInitialLoad()
             assertThat(
-                itemStore.peekItems()
+                initialLoad
             ).containsExactlyElementsIn(
                 items.createExpected(
                     // Paging 3 implementation loads starting from initial key
@@ -257,9 +255,9 @@
             pagingSourceFactory = { db.dao.loadItemsRaw(query) }
         )
         runTest(pager) {
-            itemStore.awaitInitialLoad()
+            val initialLoad = itemStore.awaitInitialLoad()
             assertThat(
-                itemStore.peekItems()
+                initialLoad
             ).containsExactlyElementsIn(
                 items.createExpected(
                     // Paging 3 implementation loads starting from initial key
@@ -297,9 +295,9 @@
         )
 
         runTest(pager = pager) {
-            itemStore.awaitInitialLoad()
+            val initialLoad = itemStore.awaitInitialLoad()
             assertThat(
-                itemStore.peekItems()
+                initialLoad
             ).containsExactlyElementsIn(
                 // returns items 20 to 28 with 21 null place holders after
                 items.createBoundedExpected(
@@ -343,9 +341,9 @@
         )
 
         runTest(pager = pager) {
-            itemStore.awaitInitialLoad()
+            val initialLoad = itemStore.awaitInitialLoad()
             assertThat(
-                itemStore.peekItems()
+                initialLoad
             ).containsExactlyElementsIn(
                 // returns items 50 to 58 with 11 null place holders after
                 items.createBoundedExpected(
@@ -383,9 +381,9 @@
             db.dao.loadItems()
         }
         runTest(pager = pager) {
-            itemStore.awaitInitialLoad()
+            val initialLoad = itemStore.awaitInitialLoad()
             assertThat(
-                itemStore.peekItems()
+                initialLoad
             ).containsExactlyElementsIn(
                 // should return last page when key is too large
                 items.createExpected(
@@ -418,9 +416,9 @@
             db.dao.loadItems()
         }
         runTest(pager = pager) {
-            itemStore.awaitInitialLoad()
+            val initialLoad = itemStore.awaitInitialLoad()
             assertThat(
-                itemStore.peekItems()
+                initialLoad
             ).containsExactlyElementsIn(
                 items.createExpected(
                     fromIndex = 0,
@@ -459,9 +457,9 @@
         )
 
         runTest(pager) {
-            itemStore.awaitInitialLoad()
+            val initialLoad = itemStore.awaitInitialLoad()
             assertThat(
-                itemStore.peekItems()
+                initialLoad
             ).containsExactlyElementsIn(
                 // should load starting from initial Key = 30
                 items.createExpected(
@@ -567,13 +565,13 @@
         )
 
         runTest(pager) {
-            itemStore.awaitInitialLoad()
+            val initialLoad = itemStore.awaitInitialLoad()
             val initialItems = items.createExpected(
                 fromIndex = 20,
                 toIndex = 20 + CONFIG.initialLoadSize
             )
             assertThat(
-                itemStore.peekItems()
+                initialLoad
             ).containsExactlyElementsIn(
                 // should load starting from initial Key = 20
                 initialItems
@@ -636,9 +634,9 @@
         val items = createItems(startId = 0, count = 90)
         db.dao.insert(items)
         runTest {
-            itemStore.awaitInitialLoad()
+            val initialLoad = itemStore.awaitInitialLoad()
             assertThat(
-                itemStore.peekItems()
+                initialLoad
             ).containsExactlyElementsIn(
                 items.createExpected(
                     fromIndex = 0,
@@ -919,11 +917,13 @@
             }
         }
 
-        suspend fun awaitInitialLoad() = withTestTimeout {
-            withContext(Dispatchers.Main) {
-                generation.filter { it.initialLoadCompleted }.first()
+        suspend fun awaitInitialLoad(): ItemSnapshotList<PagingEntity> =
+            withTestTimeout {
+                 withContext(Dispatchers.Main) {
+                    generation.filter { it.initialLoadCompleted }.first()
+                    asyncDiffer.snapshot()
+                }
             }
-        }
 
         suspend fun awaitGeneration(id: Int) = withTestTimeout {
             withContext(Dispatchers.Main) {