Skip to content

Commit

Permalink
Fix preload and exhausted searches (#97)
Browse files Browse the repository at this point in the history
  • Loading branch information
gchristov authored Dec 3, 2023
1 parent de07adf commit 3f83540
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package com.gchristov.thecodinglove.search

import arrow.core.*
import arrow.core.Either
import arrow.core.flatMap
import arrow.core.left
import arrow.core.right
import co.touchlab.kermit.Logger
import com.gchristov.thecodinglove.commonkotlin.JsonSerializer
import com.gchristov.thecodinglove.commonkotlin.error
import com.gchristov.thecodinglove.commonservice.pubsub.BasePubSubHandler
import com.gchristov.thecodinglove.commonservicedata.http.HttpHandler
import com.gchristov.thecodinglove.commonservicedata.pubsub.PubSubDecoder
Expand All @@ -18,7 +22,7 @@ import kotlinx.coroutines.CoroutineDispatcher
class PreloadSearchPubSubHandler(
dispatcher: CoroutineDispatcher,
private val jsonSerializer: JsonSerializer,
log: Logger,
private val log: Logger,
private val preloadSearchResultUseCase: PreloadSearchResultUseCase,
pubSubSubscription: PubSubSubscription,
pubSubDecoder: PubSubDecoder,
Expand All @@ -30,6 +34,8 @@ class PreloadSearchPubSubHandler(
pubSubSubscription = pubSubSubscription,
pubSubDecoder = pubSubDecoder,
) {
private val tag = this::class.simpleName

override fun httpConfig() = HttpHandler.HttpConfig(
method = HttpMethod.Post,
path = "/api/pubsub/search",
Expand All @@ -47,4 +53,13 @@ class PreloadSearchPubSubHandler(
)
.flatMap { it?.right() ?: Exception("Request body is invalid").left<Throwable>() }
.flatMap { preloadSearchResultUseCase(searchSessionId = it.searchSessionId) }
}
.fold(
ifLeft = {
// Swallow but report the error, so that we can investigate. Preload errors should not retry if the
// PubSub body cannot be parsed, or we get any of the search errors, which are currently Exhausted,
// Empty and NotFound, where retrying doesn't really make sense for any of them.
log.error(tag, it) { "Error handling request" }
Either.Right(Unit)
}, ifRight = { Either.Right(Unit) }
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,27 +52,24 @@ class PreloadSearchPubSubHttpHandlerTest {
}

@Test
fun handleMessageError(): TestResult = runBlockingTest(
preloadSearchPubSubMessage = PreloadSearchPubSubCreator.defaultMessage(),
preloadSearchResultInvocationResult = Either.Left(SearchError.Empty(additionalInfo = "test"))
fun handleRequestParseError(): TestResult = runBlockingTest(
preloadSearchPubSubMessage = null,
preloadSearchResultInvocationResult = Either.Left(SearchError.Empty())
) { handler, preloadUseCase, request ->
val result = handler.handlePubSubRequest(request)
preloadUseCase.assertInvokedOnce()
preloadUseCase.assertSearchSessionId("session_123")
assertEquals(
expected = Either.Left(SearchError.Empty(additionalInfo = "test")),
actual = result,
)
preloadUseCase.assertNotInvoked()
assertTrue { result.isRight() }
}

@Test
fun handleMessageParseError(): TestResult = runBlockingTest(
preloadSearchPubSubMessage = null,
preloadSearchResultInvocationResult = Either.Left(SearchError.Empty())
fun handleRequestSearchError(): TestResult = runBlockingTest(
preloadSearchPubSubMessage = PreloadSearchPubSubCreator.defaultMessage(),
preloadSearchResultInvocationResult = Either.Left(SearchError.Empty(additionalInfo = "test"))
) { handler, preloadUseCase, request ->
val result = handler.handlePubSubRequest(request)
preloadUseCase.assertNotInvoked()
assertTrue { result.isLeft() }
preloadUseCase.assertInvokedOnce()
preloadUseCase.assertSearchSessionId("session_123")
assertTrue { result.isRight() }
}

private fun runBlockingTest(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class SearchHttpHandlerTest {
}

@Test
fun handleError(): TestResult = runBlockingTest(
fun handleRequestError(): TestResult = runBlockingTest(
searchSessionId = TestSearchSessionId,
searchQuery = TestSearchQuery,
searchInvocationResult = Either.Left(SearchError.Empty(additionalInfo = "test"))
Expand Down

0 comments on commit 3f83540

Please sign in to comment.