Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Stale return value instead of default #132

Merged
merged 4 commits into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,50 @@ 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)
nicklasl marked this conversation as resolved.
Show resolved Hide resolved
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
Loading