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 448bb24 commit 5f00da8
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -173,19 +173,19 @@ internal class DefaultLoadingHandler<Id : Identifier<Id>, K : Comparable<K>, V :

private suspend fun passThroughError(error: Throwable, loadParams: PagingSource.LoadParams<K>, direction: LoadDirection) {

// Design decision to remove placeholders when passing an error through
// Page refreshes are not currently supported
// So the page must currently contain placeholders
store.clearPage(loadParams.key)

// Passing the error through
updateErrorState(error, direction)

// Complete the job
completeLoadJob(loadParams, direction)
completeLoadJobAfterError(loadParams, direction)
}

private suspend fun completeLoadJob(loadParams: PagingSource.LoadParams<K>, direction: LoadDirection) {
private suspend fun completeLoadJobAfterError(loadParams: PagingSource.LoadParams<K>, direction: LoadDirection) {
// Design decision to remove placeholders when not retrying after an error
// Page refreshes are not currently supported
// So the page must currently contain placeholders
store.clearPage(loadParams.key)

// Marking the pending job as completed
queueManager.updateExistingPendingJob(loadParams.key, inFlight = false, completed = true)

Expand Down Expand Up @@ -218,7 +218,7 @@ internal class DefaultLoadingHandler<Id : Identifier<Id>, K : Comparable<K>, V :
)

// Complete the job, do nothing else
completeLoadJob(loadParams, direction)
completeLoadJobAfterError(loadParams, direction)
}

ErrorHandlingStrategy.PassThrough -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,4 +271,92 @@ class DefaultLoadingHandlerTest {
verifySuspend(exactly(1)) { pagingStateManager.updateWithPrependError(any()) }
verifySuspend(exactly(1)) { queueManager.updateExistingPendingJob(eq(loadParams.key), eq(false), eq(true)) }
}

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

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.handleAppendLoading(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(0)) { pagingStateManager.updateWithAppendError(any()) }
verifySuspend(exactly(1)) { queueManager.updateExistingPendingJob(eq(loadParams.key), eq(false), eq(true)) }
}

@Test
fun handlePrependLoading_givenIgnoreErrorStrategy_whenError_thenShouldNotUpdatePagingState() = 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.Ignore

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(0)) { pagingStateManager.updateWithPrependError(any()) }
verifySuspend(exactly(1)) { queueManager.updateExistingPendingJob(eq(loadParams.key), eq(false), eq(true)) }
}
}

0 comments on commit 5f00da8

Please sign in to comment.