-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Nicklas Lundin <[email protected]>
- Loading branch information
Showing
5 changed files
with
142 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
android/src/test/java/dev/openfeature/sdk/helpers/AutoHealingProvider.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package dev.openfeature.sdk.helpers | ||
|
||
import dev.openfeature.sdk.EvaluationContext | ||
import dev.openfeature.sdk.FeatureProvider | ||
import dev.openfeature.sdk.Hook | ||
import dev.openfeature.sdk.ProviderEvaluation | ||
import dev.openfeature.sdk.ProviderMetadata | ||
import dev.openfeature.sdk.Value | ||
import dev.openfeature.sdk.events.EventHandler | ||
import dev.openfeature.sdk.events.OpenFeatureEvents | ||
import dev.openfeature.sdk.exceptions.OpenFeatureError | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.delay | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.launch | ||
import kotlinx.coroutines.test.TestDispatcher | ||
|
||
class AutoHealingProvider( | ||
val dispatcher: TestDispatcher, | ||
val healDelay: Long = 1000L, | ||
override val hooks: List<Hook<*>> = emptyList() | ||
) : FeatureProvider { | ||
override val metadata: ProviderMetadata = object : ProviderMetadata { | ||
override val name: String = "AutoHealingProvider" | ||
} | ||
private var ready = false | ||
private var eventHandler = EventHandler(dispatcher) | ||
override fun initialize(initialContext: EvaluationContext?) { | ||
CoroutineScope(dispatcher).launch { | ||
ready = false | ||
eventHandler.publish(OpenFeatureEvents.ProviderError(OpenFeatureError.ProviderNotReadyError("AutoHealingProvider is trying to heal"))) | ||
delay(healDelay) | ||
ready = true | ||
eventHandler.publish(OpenFeatureEvents.ProviderReady) | ||
} | ||
} | ||
|
||
override fun shutdown() { | ||
// no-op | ||
} | ||
|
||
override fun onContextSet( | ||
oldContext: EvaluationContext?, | ||
newContext: EvaluationContext | ||
) { | ||
// no-op | ||
} | ||
|
||
override fun getBooleanEvaluation( | ||
key: String, | ||
defaultValue: Boolean, | ||
context: EvaluationContext? | ||
): ProviderEvaluation<Boolean> { | ||
if (!ready) throw OpenFeatureError.FlagNotFoundError(key) | ||
return ProviderEvaluation(!defaultValue) | ||
} | ||
|
||
override fun getStringEvaluation( | ||
key: String, | ||
defaultValue: String, | ||
context: EvaluationContext? | ||
): ProviderEvaluation<String> { | ||
if (!ready) throw OpenFeatureError.FlagNotFoundError(key) | ||
return ProviderEvaluation(defaultValue.reversed()) | ||
} | ||
|
||
override fun getIntegerEvaluation( | ||
key: String, | ||
defaultValue: Int, | ||
context: EvaluationContext? | ||
): ProviderEvaluation<Int> { | ||
if (!ready) throw OpenFeatureError.FlagNotFoundError(key) | ||
return ProviderEvaluation(defaultValue * 100) | ||
} | ||
|
||
override fun getDoubleEvaluation( | ||
key: String, | ||
defaultValue: Double, | ||
context: EvaluationContext? | ||
): ProviderEvaluation<Double> { | ||
if (!ready) throw OpenFeatureError.FlagNotFoundError(key) | ||
return ProviderEvaluation(defaultValue * 100) | ||
} | ||
|
||
override fun getObjectEvaluation( | ||
key: String, | ||
defaultValue: Value, | ||
context: EvaluationContext? | ||
): ProviderEvaluation<Value> { | ||
if (!ready) throw OpenFeatureError.FlagNotFoundError(key) | ||
return ProviderEvaluation(Value.Null) | ||
} | ||
|
||
override fun observe(): Flow<OpenFeatureEvents> = eventHandler.observe() | ||
|
||
override fun getProviderStatus(): OpenFeatureEvents = if (ready) { | ||
OpenFeatureEvents.ProviderReady | ||
} else { | ||
OpenFeatureEvents.ProviderStale | ||
} | ||
} |