Skip to content

Commit

Permalink
Cover DefaultLoadingHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
matt-ramotar committed Jul 14, 2024
1 parent d13997d commit 448bb24
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ internal interface LoadingHandler<Id : Identifier<Id>, K : Comparable<K>, V : Id
addNextToQueue: Boolean = true
)

suspend fun handlePrependLoading(loadParams: PagingSource.LoadParams<K>)
suspend fun handlePrependLoading(loadParams: PagingSource.LoadParams<K>, addNextToQueue: Boolean = true)
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,18 @@ internal class DefaultLoadingHandler<Id : Identifier<Id>, K : Comparable<K>, V :
private val middlewareCache = mutableMapOf<PagingSource.LoadParams<K>, PagingSource.LoadParams<K>>()

override suspend fun handleAppendLoading(loadParams: PagingSource.LoadParams<K>, addNextToQueue: Boolean) =
handleLoading(loadParams, LoadDirection.Append, addNextToQueue)
handleLoading(loadParams, addNextToQueue)

override suspend fun handlePrependLoading(loadParams: PagingSource.LoadParams<K>) =
handleLoading(loadParams, LoadDirection.Prepend)
override suspend fun handlePrependLoading(loadParams: PagingSource.LoadParams<K>, addNextToQueue: Boolean) =
handleLoading(loadParams, addNextToQueue)

private suspend fun handleLoading(loadParams: PagingSource.LoadParams<K>, direction: LoadDirection, addNextToQueue: Boolean = true) {
private suspend fun handleLoading(loadParams: PagingSource.LoadParams<K>, addNextToQueue: Boolean) {

logger.debug(
"""
Trying to load
Load params: $loadParams
Direction: $direction
Direction: ${loadParams.direction}
""".trimIndent()
)

Expand All @@ -73,7 +73,7 @@ internal class DefaultLoadingHandler<Id : Identifier<Id>, K : Comparable<K>, V :
""".trimIndent()
)

updateLoadingState(direction)
updateLoadingState(loadParams.direction)

try {

Expand All @@ -83,17 +83,17 @@ internal class DefaultLoadingHandler<Id : Identifier<Id>, K : Comparable<K>, V :
fetchingStateHolder.updateMaxRequestSoFar(modifiedParams.key)
fetchingStateHolder.updateMinRequestSoFar(modifiedParams.key)

loadPage(modifiedParams, direction, addNextToQueue)
loadPage(modifiedParams, loadParams.direction, addNextToQueue)
} catch (pagingError: Throwable) {
logger.error(
"""
Caught error
Load params: $loadParams
Direction: $direction
Direction: ${loadParams.direction}
""".trimIndent(),
pagingError
)
handleError(pagingError, modifiedParams, direction)
handleError(pagingError, modifiedParams, loadParams.direction)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ internal class RealLoadingHandler<Id : Identifier<Id>, K : Comparable<K>, V : Id
}


override suspend fun handlePrependLoading(loadParams: PagingSource.LoadParams<K>) {
override suspend fun handlePrependLoading(loadParams: PagingSource.LoadParams<K>, addNextToQueue: Boolean) {
val modifiedParams = applyMiddleware(loadParams)

pagingStateManager.updateWithPrependLoading()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ class DefaultLoadingHandlerTest {
val strategy = LoadStrategy.SkipCache
val direction = LoadDirection.Append
val loadParams = PagingSource.LoadParams(key, strategy, direction)
val expectedPosts = List(size) { createFakePost(it + 21) }
val expectedPosts = List(size) { createFakePost(it + 1) }
val errorHandlingStrategy = ErrorHandlingStrategy.PassThrough

loadingHandler = DefaultLoadingHandler(
Expand Down Expand Up @@ -227,4 +227,48 @@ class DefaultLoadingHandlerTest {
verifySuspend(exactly(1)) { pagingStateManager.updateWithAppendError(any()) }
verifySuspend(exactly(1)) { queueManager.updateExistingPendingJob(eq(loadParams.key), eq(false), eq(true)) }
}

@Test
fun handlePrependLoading_givenPassThroughErrorStrategy_whenError_thenShouldUpdateStates() = runTest {
// Given
val cursor = "21"
val size = 20
val key = TimelineRequest(cursor, size)
val strategy = LoadStrategy.SkipCache
val direction = LoadDirection.Prepend
val loadParams = PagingSource.LoadParams(key, strategy, direction)
val expectedPosts = List(size) { createFakePost(it + 21) }
val errorHandlingStrategy = ErrorHandlingStrategy.PassThrough

loadingHandler = DefaultLoadingHandler(
store,
pagingStateManager,
queueManager,
fetchingStateHolder,
errorHandlingStrategy,
middleware,
operationApplier,
retryBookkeeper,
logger,
exponentialBackoff
)

val expectedStoreFlow = flowOf(
PageLoadState.Error.Message<CursorIdentifier, TimelineRequest, Post>("", true)
)

everySuspend { store.loadPage(eq(loadParams)) }.returns(expectedStoreFlow)
everySuspend { operationApplier.applyOperations(any(), any(), any(), any()) } returns ItemSnapshotList(expectedPosts)

// When
loadingHandler.handlePrependLoading(loadParams)

// Then
verifySuspend(exactly(1)) { store.loadPage(eq(loadParams)) }
verifySuspend(exactly(1)) { fetchingStateHolder.updateMaxRequestSoFar(eq(key)) }
verifySuspend(exactly(1)) { fetchingStateHolder.updateMinRequestSoFar(eq(key)) }
verifySuspend(exactly(1)) { store.clearPage(eq(loadParams.key)) }
verifySuspend(exactly(1)) { pagingStateManager.updateWithPrependError(any()) }
verifySuspend(exactly(1)) { queueManager.updateExistingPendingJob(eq(loadParams.key), eq(false), eq(true)) }
}
}

0 comments on commit 448bb24

Please sign in to comment.