Skip to content

Commit

Permalink
feat: Stale return value instead of default (#132)
Browse files Browse the repository at this point in the history
* stale return value instead of default

* fixup! stale return value instead of default

* fixup! stale return value instead of default

* fixup! stale return value instead of default

---------

Co-authored-by: Nicklas Lundin <nicklasl@spotify.com>
  • Loading branch information
vahidlazio and nicklasl committed Apr 22, 2024
1 parent c532f9f commit 611200c
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,6 @@ internal fun <T> FlagResolution?.getEvaluation(
)
}

// handle stale case
if (this.context != context) {
return Evaluation(
value = defaultValue,
reason = resolvedFlag.reason,
errorCode = ErrorCode.RESOLVE_STALE
)
}

// handle flag found
val flagValue = resolvedFlag.value
val resolvedValue: ConfidenceValue =
Expand All @@ -61,9 +52,14 @@ internal fun <T> FlagResolution?.getEvaluation(
return when (resolvedFlag.reason) {
ResolveReason.RESOLVE_REASON_MATCH -> {
val value = getTyped<T>(resolvedValue) ?: defaultValue
val resolveReason = if (this.context != context) {
ResolveReason.RESOLVE_REASON_STALE
} else {
resolvedFlag.reason
}
return Evaluation(
value = value,
reason = resolvedFlag.reason,
reason = resolveReason,
variant = resolvedFlag.variant
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,52 @@ internal class ConfidenceFeatureProviderTests {
assertNull(evalString2.errorCode)
}

@Test
fun testStaleValueReturnValueAndStaleReason() = runTest {
val testDispatcher = UnconfinedTestDispatcher(testScheduler)
val mockConfidence = getConfidence(testDispatcher)
val eventHandler = EventHandler(testDispatcher)
val confidenceFeatureProvider = ConfidenceFeatureProvider.create(
context = mockContext,
eventHandler = eventHandler,
confidence = mockConfidence,
dispatcher = testDispatcher
)
val context = ImmutableContext("foo").toConfidenceContext().map
whenever(flagResolverClient.resolve(eq(listOf()), eq(context))).thenReturn(
Result.Success(
FlagResolution(
context,
resolvedFlags.list,
"token1"
)
)
)

val evaluationContext = ImmutableContext("foo")
confidenceFeatureProvider.initialize(evaluationContext)
advanceUntilIdle()

verify(flagResolverClient, times(1)).resolve(any(), eq(context))

val evalString = confidenceFeatureProvider.getStringEvaluation(
"test-kotlin-flag-1.mystring",
"default",
evaluationContext
)
assertEquals(evalString.reason, Reason.TARGETING_MATCH.name)
assertEquals(evalString.value, "red")

mockConfidence.putContext("hello", ConfidenceValue.String("new context"))
val newContextEval = confidenceFeatureProvider.getStringEvaluation(
"test-kotlin-flag-1.mystring",
"default",
evaluationContext
)
assertEquals(newContextEval.reason, Reason.STALE.name)
assertEquals(newContextEval.value, "red")
}

@Test
fun testApplyFromStoredCache() = runTest {
val cacheFile = File(mockContext.filesDir, APPLY_FILE_NAME)
Expand Down

0 comments on commit 611200c

Please sign in to comment.