Skip to content

Commit

Permalink
test cases for Instance.kt
Browse files Browse the repository at this point in the history
  • Loading branch information
Tan108 committed Oct 1, 2024
1 parent 0948a22 commit 10b325d
Show file tree
Hide file tree
Showing 7 changed files with 1,440 additions and 32 deletions.
2 changes: 1 addition & 1 deletion src/main/kotlin/com/featurevisor/sdk/Instance+Fetch.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ val client = OkHttpClient()

// MARK: - Fetch datafile content
@Throws(IOException::class)
internal fun FeaturevisorInstance.fetchDatafileContent(
suspend fun FeaturevisorInstance.fetchDatafileContent(
url: String,
handleDatafileFetch: DatafileFetchHandler? = null,
completion: (Result<DatafileContent>) -> Unit,
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/com/featurevisor/sdk/Instance+Refresh.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ fun FeaturevisorInstance.stopRefreshing() {
logger?.warn("refreshing has stopped")
}

private fun FeaturevisorInstance.refresh() {
private suspend fun FeaturevisorInstance.refresh() {
logger?.debug("refreshing datafile")
when {
statuses.refreshInProgress -> logger?.warn("refresh in progress, skipping")
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/com/featurevisor/sdk/Instance+Status.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ package com.featurevisor.sdk

data class Statuses(var ready: Boolean, var refreshInProgress: Boolean)

internal fun FeaturevisorInstance.isReady(): Boolean {
fun FeaturevisorInstance.isReady(): Boolean {
return statuses.ready
}
17 changes: 9 additions & 8 deletions src/main/kotlin/com/featurevisor/sdk/Instance+Variable.kt
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
package com.featurevisor.sdk

import com.featurevisor.testRunner.getVariableValues
import com.featurevisor.types.Context
import com.featurevisor.types.FeatureKey
import com.featurevisor.types.VariableKey
import com.featurevisor.types.VariableValue
import com.featurevisor.types.VariableValue.ArrayValue
import com.featurevisor.types.VariableValue.BooleanValue
import com.featurevisor.types.VariableValue.DoubleValue
import com.featurevisor.types.VariableValue.IntValue
import com.featurevisor.types.VariableValue.JsonValue
import com.featurevisor.types.VariableValue.ObjectValue
import com.featurevisor.types.VariableValue.StringValue
import com.featurevisor.types.VariableValue.*
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.decodeFromJsonElement
Expand Down Expand Up @@ -76,8 +71,14 @@ inline fun <reified T : Any> FeaturevisorInstance.getVariableObject(
context: Context,
): T? {
val objectValue = getVariable(featureKey, variableKey, context) as? ObjectValue
val actualValue = objectValue?.value?.keys?.map {
mapOf(
it to getVariableValues(objectValue.value[it]).toString()
)
}?.firstOrNull()

return try {
val encoded = Json.encodeToJsonElement(objectValue?.value)
val encoded = Json.encodeToJsonElement(actualValue)
return Json.decodeFromJsonElement<T>(encoded)
} catch (e: Exception) {
null
Expand Down
21 changes: 17 additions & 4 deletions src/main/kotlin/com/featurevisor/sdk/Instance.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@ package com.featurevisor.sdk
import com.featurevisor.sdk.FeaturevisorError.MissingDatafileOptions
import com.featurevisor.types.*
import com.featurevisor.types.EventName.*
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
import kotlinx.coroutines.*
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.json.Json
import kotlin.coroutines.resume

typealias ConfigureBucketKey = (Feature, Context, BucketKey) -> BucketKey
typealias ConfigureBucketValue = (Feature, Context, BucketValue) -> BucketValue
Expand Down Expand Up @@ -141,6 +139,21 @@ class FeaturevisorInstance private constructor(options: InstanceOptions) {
this.logger?.setLevels(levels)
}

suspend fun onReady(): FeaturevisorInstance {
return suspendCancellableCoroutine { continuation ->
if (this.statuses.ready) {
continuation.resume(this)
}

val cb :(result:Array<out Any>) -> Unit = {
this.emitter.removeListener(READY)
continuation.resume(this)
}

this.emitter.addListener(READY,cb)
}
}

fun setDatafile(datafileJSON: String) {
val data = datafileJSON.toByteArray(Charsets.UTF_8)
try {
Expand Down
13 changes: 13 additions & 0 deletions src/main/kotlin/com/featurevisor/testRunner/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.featurevisor.sdk.FeaturevisorInstance
import com.featurevisor.sdk.InstanceOptions
import com.featurevisor.sdk.emptyDatafile
import com.featurevisor.types.*
import com.featurevisor.types.VariableValue.*
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonElement
Expand Down Expand Up @@ -177,6 +178,18 @@ fun getContextValues(contextValue: AttributeValue?) =
null -> null
}

fun getVariableValues(variableValue: VariableValue?) =
when (variableValue) {
is IntValue -> variableValue.value
is DoubleValue -> variableValue.value
is StringValue -> variableValue.value
is BooleanValue -> variableValue.value
is ArrayValue -> variableValue.values
is JsonValue -> variableValue.value
is ObjectValue -> variableValue.value
null -> null
}

fun checkIfArraysAreEqual(a: Array<Any>, b: Array<Any>): Boolean {
if (a.size != b.size) return false

Expand Down
Loading

0 comments on commit 10b325d

Please sign in to comment.