Skip to content

Commit c82eef6

Browse files
committed
test: harden error mappings, state flow (#205)
Signed-off-by: Marcin Stepien <marcin.stepien@fluxon.com>
1 parent d657684 commit c82eef6

2 files changed

Lines changed: 39 additions & 0 deletions

File tree

kotlin-sdk/src/commonMain/kotlin/dev/openfeature/kotlin/sdk/Client.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import kotlinx.coroutines.flow.Flow
55
interface Client : Features, Tracking {
66
val metadata: ClientMetadata
77
val hooks: List<Hook<*>>
8+
89
/**
910
* A [Flow] that emits the initial [OpenFeatureStatus] and all subsequent state transitions
1011
* of the Provider handling this client's evaluations. This enables reactive observation

kotlin-sdk/src/commonTest/kotlin/dev/openfeature/kotlin/sdk/OpenFeatureClientTests.kt

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,4 +136,42 @@ class OpenFeatureClientTests {
136136

137137
assertEquals(OpenFeatureStatus.Stale, client.providerStatus)
138138
}
139+
140+
@Test
141+
fun testClientEvaluationShouldReturnProviderNotReadyWhenEvaluatingBeforeProviderIsReady() = runTest {
142+
val client = OpenFeatureAPI.getClient()
143+
val details = client.getBooleanDetails("test-flag", false)
144+
assertEquals(dev.openfeature.kotlin.sdk.exceptions.ErrorCode.PROVIDER_NOT_READY, details.errorCode)
145+
}
146+
147+
@Test
148+
fun testClientEvaluationShouldReturnProviderFatalWhenEvaluatingWithFatalProvider() = runTest {
149+
val fatalProvider = object : FeatureProvider by NoOpProvider() {
150+
override suspend fun initialize(initialContext: EvaluationContext?) {
151+
throw OpenFeatureError.ProviderFatalError("test fatal error")
152+
}
153+
}
154+
OpenFeatureAPI.setProviderAndWait(fatalProvider)
155+
val client = OpenFeatureAPI.getClient()
156+
val details = client.getBooleanDetails("test-flag", false)
157+
assertEquals(dev.openfeature.kotlin.sdk.exceptions.ErrorCode.PROVIDER_FATAL, details.errorCode)
158+
}
159+
160+
@Test
161+
fun testClientEvaluationShouldMapGeneralExceptionsToGeneralErrorCode() = runTest {
162+
val throwingProvider = object : FeatureProvider by NoOpProvider() {
163+
override fun getBooleanEvaluation(
164+
key: String,
165+
defaultValue: Boolean,
166+
context: EvaluationContext?
167+
): ProviderEvaluation<Boolean> {
168+
throw IllegalStateException("Generic unexpected crash")
169+
}
170+
}
171+
OpenFeatureAPI.setProviderAndWait(throwingProvider)
172+
val client = OpenFeatureAPI.getClient()
173+
val details = client.getBooleanDetails("test-flag", false)
174+
assertEquals(dev.openfeature.kotlin.sdk.exceptions.ErrorCode.GENERAL, details.errorCode)
175+
assertEquals("Generic unexpected crash", details.errorMessage)
176+
}
139177
}

0 commit comments

Comments
 (0)