Skip to content

Commit

Permalink
test: fix issue with leaking events between tests (#74)
Browse files Browse the repository at this point in the history
* test: fix issue with leaking events between tests

* await provider ready using specified EventsHandler
  • Loading branch information
nicklasl authored Oct 5, 2023
1 parent b41f750 commit aa665d4
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import dev.openfeature.sdk.ImmutableStructure
import dev.openfeature.sdk.OpenFeatureAPI
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 kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.runBlocking
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotEquals
Expand All @@ -33,7 +33,6 @@ class ConfidenceIntegrationTests {
@Before
fun setup() {
whenever(mockContext.filesDir).thenReturn(Files.createTempDirectory("tmpTests").toFile())
EventHandler.eventsPublisher().publish(OpenFeatureEvents.ProviderStale)
}

@Test
Expand Down Expand Up @@ -73,16 +72,20 @@ class ConfidenceIntegrationTests {
)
}

val eventsHandler = EventHandler(Dispatchers.IO).apply {
publish(OpenFeatureEvents.ProviderStale)
}
OpenFeatureAPI.setProvider(
ConfidenceFeatureProvider.create(
mockContext,
clientSecret,
initialisationStrategy = InitialisationStrategy.ActivateAndFetchAsync
initialisationStrategy = InitialisationStrategy.ActivateAndFetchAsync,
eventsPublisher = eventsHandler
),
context
)
runBlocking {
awaitProviderReady()
awaitProviderReady(eventsHandler = eventsHandler)
}

val intDetails = OpenFeatureAPI.getClient()
Expand All @@ -100,11 +103,15 @@ class ConfidenceIntegrationTests {

@Test
fun testSimpleResolveInMemoryCache() {
val eventsHandler = EventHandler(Dispatchers.IO).apply {
publish(OpenFeatureEvents.ProviderStale)
}
OpenFeatureAPI.setProvider(
ConfidenceFeatureProvider.create(
mockContext,
clientSecret,
initialisationStrategy = InitialisationStrategy.FetchAndActivate
initialisationStrategy = InitialisationStrategy.FetchAndActivate,
eventsPublisher = eventsHandler
),
ImmutableContext(
targetingKey = UUID.randomUUID().toString(),
Expand All @@ -118,7 +125,7 @@ class ConfidenceIntegrationTests {
)
)
runBlocking {
awaitProviderReady()
awaitProviderReady(eventsHandler = eventsHandler)
}

val intDetails = OpenFeatureAPI.getClient()
Expand All @@ -136,10 +143,13 @@ class ConfidenceIntegrationTests {

@Test
fun testSimpleResolveStoredCache() {
val eventsHandler = EventHandler(Dispatchers.IO).apply {
publish(OpenFeatureEvents.ProviderStale)
}
val cacheFile = File(mockContext.filesDir, FLAGS_FILE_NAME)
assertEquals(0L, cacheFile.length())
OpenFeatureAPI.setProvider(
ConfidenceFeatureProvider.create(mockContext, clientSecret),
ConfidenceFeatureProvider.create(mockContext, clientSecret, eventsPublisher = eventsHandler),
ImmutableContext(
targetingKey = UUID.randomUUID().toString(),
attributes = mutableMapOf(
Expand All @@ -153,7 +163,7 @@ class ConfidenceIntegrationTests {
)

runBlocking {
awaitProviderReady()
awaitProviderReady(eventsHandler = eventsHandler)
}

assertNotEquals(0L, cacheFile.length())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
@file:OptIn(ExperimentalCoroutinesApi::class)

package com.spotify.confidence

import android.content.Context
Expand All @@ -16,6 +18,7 @@ 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.ExperimentalCoroutinesApi
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.test.UnconfinedTestDispatcher
import kotlinx.coroutines.test.runTest
Expand Down
39 changes: 39 additions & 0 deletions Provider/src/test/java/com/spotify/confidence/TestExtensions.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.spotify.confidence

import dev.openfeature.sdk.events.EventHandler
import dev.openfeature.sdk.events.OpenFeatureEvents
import dev.openfeature.sdk.events.observe
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.onStart
import kotlinx.coroutines.flow.take
import kotlinx.coroutines.launch
import kotlinx.coroutines.suspendCancellableCoroutine

suspend fun awaitProviderReady(

eventsHandler: EventHandler,

dispatcher: CoroutineDispatcher = Dispatchers.IO

) = suspendCancellableCoroutine { continuation ->

fun observeProviderReady() = eventsHandler
.observe<OpenFeatureEvents.ProviderReady>()
.onStart {
if (eventsHandler.isProviderReady()) {
this.emit(OpenFeatureEvents.ProviderReady)
}
}

val coroutineScope = CoroutineScope(dispatcher)

coroutineScope.launch {
observeProviderReady()
.take(1)
.collect {
continuation.resumeWith(Result.success(Unit))
}
}
}

0 comments on commit aa665d4

Please sign in to comment.