Skip to content

Commit

Permalink
test: Add test for network exception logging
Browse files Browse the repository at this point in the history
  • Loading branch information
nicklasl committed Jun 27, 2024
1 parent 3279b7c commit 089f903
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -874,6 +874,36 @@ internal class ConfidenceEvaluationTest {
TestCase.assertEquals(ErrorCode.PROVIDER_NOT_READY, evalNull.errorCode)
}

@Test
fun testNetworkErrorIsLogged() = runTest {
val cache = InMemoryCache()
val testDispatcher = UnconfinedTestDispatcher(testScheduler)
val context = mapOf("key" to ConfidenceValue.String("foo"))
val debugLoggerFake = DebugLoggerFake()
val mockConfidence = getConfidence(
testDispatcher,
initialContext = context,
cache = cache,
debugLogger = debugLoggerFake
)
whenever(flagApplierClient.apply(any(), any())).thenReturn(Result.Success(Unit))
whenever(
flagResolverClient.resolve(
eq(listOf()),
any()
)
).thenThrow(Error("Something went terribly wrong on the Internetz"))

mockConfidence.fetchAndActivate()
advanceUntilIdle()
Assert.assertEquals(1, debugLoggerFake.messagesLogged.size)
Assert.assertEquals("Network error", debugLoggerFake.messagesLogged.first().message)
Assert.assertEquals(
"Something went terribly wrong on the Internetz",
debugLoggerFake.messagesLogged.first().throwable?.message
)
}

@Test
fun testInvalidTargetingKey() = runTest {
val cache = InMemoryCache()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package com.spotify.confidence

internal open class DebugLoggerFake : DebugLogger {
var messagesLogged = 0
val messagesLogged = mutableListOf<Msg>()

override fun logEvent(action: String, event: EngineEvent) {
// not important enough to test right now
}

override fun logMessage(message: String, isWarning: Boolean, throwable: Throwable?) {
messagesLogged++
messagesLogged.add(Msg(message, isWarning, throwable))
}

override fun logFlag(action: String, flag: String?) {
Expand All @@ -18,4 +18,6 @@ internal open class DebugLoggerFake : DebugLogger {
override fun logContext(action: String, context: Map<String, ConfidenceValue>) {
// not important enough to test right now
}

data class Msg(val message: String, val isWarning: Boolean, val throwable: Throwable?)
}
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ class EventSenderIntegrationTest {
advanceUntilIdle()
// debugLogger.logMessage is not a unique log. For these events we log:
// flush policy triggered log, uploading batch events log
Assert.assertEquals(18, debugLogger.messagesLogged)
Assert.assertEquals(18, debugLogger.messagesLogged.size)
runBlocking {
val batchReadyFiles = eventStorage.batchReadyFiles()
val totalFiles = directory.walkFiles()
Expand Down Expand Up @@ -213,7 +213,7 @@ class EventSenderIntegrationTest {
advanceUntilIdle()
// debugLogger.logMessage is not a unique log. For these events we log:
// flush policy triggered log, uploading batch events log
Assert.assertEquals(3, debugLogger.messagesLogged)
Assert.assertEquals(3, debugLogger.messagesLogged.size)
Assert.assertEquals("eventDefinitions/my_event", uploadedEvents[0].eventDefinition)
Assert.assertEquals(
mapOf(
Expand Down Expand Up @@ -284,7 +284,7 @@ class EventSenderIntegrationTest {
advanceUntilIdle()
// debugLogger.logMessage is not a unique log. For these events we log:
// flush policy triggered log, uploading batch events log
Assert.assertEquals(12, debugLogger.messagesLogged)
Assert.assertEquals(12, debugLogger.messagesLogged.size)
Assert.assertEquals(uploadRequestCount, eventCount / batchSize)
runBlocking {
val batchReadyFiles = eventStorage.batchReadyFiles()
Expand Down Expand Up @@ -349,7 +349,7 @@ class EventSenderIntegrationTest {
advanceUntilIdle()
// debugLogger.logMessage is not a unique log. For these events we log:
// flush policy triggered log, uploading batch events log
Assert.assertEquals(3, debugLogger.messagesLogged)
Assert.assertEquals(3, debugLogger.messagesLogged.size)
Assert.assertEquals(1, uploader.requests.size)
Assert.assertEquals(2, uploader.requests[0].events.size)
}
Expand Down

0 comments on commit 089f903

Please sign in to comment.