Skip to content

Commit

Permalink
use stale event for the testOfflineScenarioLoadsStoredCache
Browse files Browse the repository at this point in the history
  • Loading branch information
vahidlazio committed Sep 5, 2023
1 parent 806a76d commit 89e2bb0
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class ConfidenceFeatureProvider private constructor(
private val client: ConfidenceClient,
private val flagApplier: FlagApplier,
private val eventsPublisher: EventsPublisher,
private val dispatcher: CoroutineDispatcher
dispatcher: CoroutineDispatcher
) : FeatureProvider {
private val job = SupervisorJob()
private val coroutineScope = CoroutineScope(job + dispatcher)
Expand All @@ -61,6 +61,7 @@ class ConfidenceFeatureProvider private constructor(

override fun initialize(initialContext: EvaluationContext?) {
if (initialContext == null) return
eventsPublisher.publish(OpenFeatureEvents.ProviderStale)

// refresh cache with the last stored data
storage.read()?.let(cache::refresh)
Expand Down Expand Up @@ -300,7 +301,7 @@ class ConfidenceFeatureProvider private constructor(
}
}

sealed class InitialisationStrategy {
object FetchAndActivate : InitialisationStrategy()
object ActivateAndFetchAsync : InitialisationStrategy()
sealed interface InitialisationStrategy {
object FetchAndActivate : InitialisationStrategy
object ActivateAndFetchAsync : InitialisationStrategy
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package dev.openfeature.contrib.providers.cache

import dev.openfeature.contrib.providers.client.ResolvedFlag
import dev.openfeature.sdk.EvaluationContext

interface DiskStorage {
fun store(
resolvedFlags: List<ResolvedFlag>,
resolveToken: String,
evaluationContext: EvaluationContext
): CacheData
fun read(): CacheData?

fun clear()
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,6 @@ import java.io.File

const val FLAGS_FILE_NAME = "confidence_flags_cache.json"

interface DiskStorage {
fun store(
resolvedFlags: List<ResolvedFlag>,
resolveToken: String,
evaluationContext: EvaluationContext
): CacheData
fun read(): CacheData?

fun clear()
}

class StorageFileCache private constructor(context: Context) : DiskStorage {
private val file: File = File(context.filesDir, FLAGS_FILE_NAME)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@ import dev.openfeature.sdk.ImmutableStructure
import dev.openfeature.sdk.Reason
import dev.openfeature.sdk.Value
import dev.openfeature.sdk.async.awaitProviderReady
import dev.openfeature.sdk.events.EventHandler
import dev.openfeature.sdk.events.OpenFeatureEvents
import junit.framework.TestCase
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.test.UnconfinedTestDispatcher
import kotlinx.coroutines.test.runTest
import org.junit.Before
import org.junit.Test
Expand Down Expand Up @@ -61,12 +64,16 @@ class StorageFileCacheTests {
@Test
fun testOfflineScenarioLoadsStoredCache() = runTest {
val mockClient: ConfidenceClient = mock()
val testDispatcher = UnconfinedTestDispatcher(testScheduler)
val eventPublisher = EventHandler.eventsPublisher(testDispatcher)
eventPublisher.publish(OpenFeatureEvents.ProviderStale)
val cache1 = InMemoryCache()
whenever(mockClient.resolve(eq(listOf()), any())).thenReturn(ResolveResponse.Resolved(ResolveFlags(resolvedFlags, "token1")))
val provider1 = ConfidenceFeatureProvider.create(
context = mockContext,
clientSecret = "",
client = mockClient,
eventsPublisher = eventPublisher,
cache = cache1
)
runBlocking {
Expand Down

0 comments on commit 89e2bb0

Please sign in to comment.