Skip to content

Commit

Permalink
Merge branch 'main' into update-openfeature-events
Browse files Browse the repository at this point in the history
  • Loading branch information
vahidlazio committed Jul 27, 2023
2 parents d770f73 + 862400f commit 1d98d35
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 41 deletions.
14 changes: 14 additions & 0 deletions ConfidenceDemoApp/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import java.io.File
import java.util.Properties

plugins {
id("com.android.application")
id("org.jetbrains.kotlin.android")
Expand All @@ -20,11 +23,22 @@ object Versions {
const val uiTestManifest = "1.2.0"
}

val localPropertiesFile = File(rootProject.projectDir, "local.properties")
val localProperties = Properties()

// Load the properties from local.properties file
if (localPropertiesFile.exists()) {
localProperties.load(localPropertiesFile.inputStream())
}

val clientSecret: String = localProperties.getProperty("CLIENT_SECRET")?: "CLIENT_SECRET"

android {
namespace = "com.example.confidencedemoapp"
compileSdk = 33

defaultConfig {
buildConfigField("String","CLIENT_SECRET", "\"$clientSecret\"")
applicationId = "com.example.confidencedemoapp"
minSdk = 21
targetSdk = 33
Expand Down
3 changes: 0 additions & 3 deletions ConfidenceDemoApp/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data
android:name="com.example.confidencedemoapp.clientSecret"
android:value="@string/client_secret" />
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.example.confidencedemoapp

object ClientSecretProvider {
fun clientSecret(): String = BuildConfig.CLIENT_SECRET
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
package com.example.confidencedemoapp

import android.app.Application
import android.content.pm.PackageManager
import android.util.Log
import androidx.compose.ui.graphics.Color
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import dev.openfeature.contrib.providers.ConfidenceFeatureProvider
import dev.openfeature.sdk.*
import dev.openfeature.sdk.Client
import dev.openfeature.sdk.EvaluationContext
import dev.openfeature.sdk.FlagEvaluationDetails
import dev.openfeature.sdk.ImmutableContext
import dev.openfeature.sdk.OpenFeatureAPI
import dev.openfeature.sdk.Value
import dev.openfeature.sdk.async.awaitProviderReady
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.util.*
import java.util.UUID

class MainVm(app: Application) : AndroidViewModel(app) {

Expand All @@ -31,12 +35,7 @@ class MainVm(app: Application) : AndroidViewModel(app) {

init {
val start = System.currentTimeMillis()
val applicationContext = app.applicationContext
val metadataBundle = applicationContext.packageManager.getApplicationInfo(
applicationContext.packageName,
PackageManager.GET_META_DATA
).metaData
val clientSecret = metadataBundle.getString("com.example.confidencedemoapp.clientSecret")!!
val clientSecret = ClientSecretProvider.clientSecret()
OpenFeatureAPI.setProvider(
ConfidenceFeatureProvider.create(
app.applicationContext,
Expand All @@ -45,7 +44,6 @@ class MainVm(app: Application) : AndroidViewModel(app) {
initialContext = ctx
)
client = OpenFeatureAPI.getClient()

viewModelScope.launch {
withContext(Dispatchers.IO) {
awaitProviderReady()
Expand Down
1 change: 0 additions & 1 deletion ConfidenceDemoApp/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
<resources>
<string name="app_name">ConfidenceDemoApp</string>
<string name="client_secret">Some client secret</string>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ class EventProcessor<INPUT, BatchProcessInputs, DATA>(
// ensures we don't have any shared mutability
val data: DATA = onInitialised()

// Try send events retrieved via "onInitialised()"
onProcessBatch(
data,
dataSentChannel,
coroutineScope,
exceptionHandler
)

// the select clause makes sure that we don't
// share the data/file write operations between coroutines
// at any certain time there is only one of these events being handled
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,31 @@ import java.io.File
import java.nio.file.Files
import java.time.Instant

private const val cacheFileData = "{\n" +
" \"token1\": {\n" +
" \"fdema-kotlin-flag-0\": {\n" +
" \"time\": \"2023-06-26T11:55:33.443Z\",\n" +
" \"sent\": true\n" +
" }\n" +
" },\n" +
" \"token2\": {\n" +
" \"fdema-kotlin-flag-2\": {\n" +
" \"time\": \"2023-06-26T11:55:33.444Z\",\n" +
" \"sent\": true\n" +
" },\n" +
" \"fdema-kotlin-flag-3\": {\n" +
" \"time\": \"2023-06-26T11:55:33.445Z\",\n" +
" \"sent\": false\n" +
" }\n" +
" },\n" +
" \"token3\": {\n" +
" \"fdema-kotlin-flag-4\": {\n" +
" \"time\": \"2023-06-26T11:55:33.446Z\",\n" +
" \"sent\": false\n" +
" }\n" +
" }\n" +
"}\n"

@OptIn(ExperimentalCoroutinesApi::class)
internal class ConfidenceFeatureProviderTests {
private val mockClient: ConfidenceClient = mock()
Expand Down Expand Up @@ -404,34 +429,9 @@ internal class ConfidenceFeatureProviderTests {
}

@Test
fun testApplyCacheRemovesSentResolveTokens() = runTest {
fun testOnProcessBatchOnInitAndEval() = runTest {
val cacheFile = File(mockContext.filesDir, APPLY_FILE_NAME)
cacheFile.writeText(
"{\n" +
" \"token1\": {\n" +
" \"fdema-kotlin-flag-0\": {\n" +
" \"time\": \"2023-06-26T11:55:33.443Z\",\n" +
" \"sent\": true\n" +
" }\n" +
" },\n" +
" \"token2\": {\n" +
" \"fdema-kotlin-flag-2\": {\n" +
" \"time\": \"2023-06-26T11:55:33.444Z\",\n" +
" \"sent\": true\n" +
" },\n" +
" \"fdema-kotlin-flag-3\": {\n" +
" \"time\": \"2023-06-26T11:55:33.445Z\",\n" +
" \"sent\": false\n" +
" }\n" +
" },\n" +
" \"token3\": {\n" +
" \"fdema-kotlin-flag-4\": {\n" +
" \"time\": \"2023-06-26T11:55:33.446Z\",\n" +
" \"sent\": false\n" +
" }\n" +
" }\n" +
"}\n"
)
cacheFile.writeText(cacheFileData)

val testDispatcher = UnconfinedTestDispatcher(testScheduler)
val confidenceFeatureProvider = ConfidenceFeatureProvider.create(
Expand Down Expand Up @@ -461,6 +461,27 @@ internal class ConfidenceFeatureProviderTests {
evaluationContext
)

advanceUntilIdle()
verify(mockClient, times(0)).apply(any(), eq("token1"))
verify(mockClient, times(2)).apply(any(), eq("token2"))
verify(mockClient, times(1)).apply(any(), eq("token3"))
assertEquals(0, Json.parseToJsonElement(cacheFile.readText()).jsonObject.size)
}

@Test
fun testOnProcessBatchOnInit() = runTest {
val cacheFile = File(mockContext.filesDir, APPLY_FILE_NAME)
cacheFile.writeText(cacheFileData)

val testDispatcher = UnconfinedTestDispatcher(testScheduler)
val test = ConfidenceFeatureProvider.create(
context = mockContext,
clientSecret = "",
cache = InMemoryCache(),
client = mockClient,
dispatcher = testDispatcher
)

advanceUntilIdle()
verify(mockClient, times(0)).apply(any(), eq("token1"))
verify(mockClient, times(1)).apply(any(), eq("token2"))
Expand Down

0 comments on commit 1d98d35

Please sign in to comment.