Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support a function to batch get exp #37

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/main/kotlin/com/statsig/sdk/Statsig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -811,5 +811,18 @@ class Statsig {
}
return initialized
}

@JvmStatic
fun batchGetExperimentsAsync(
user: StatsigUser,
experimentNames: List<String>
): CompletableFuture<Map<String, DynamicConfig>> {
if (!checkInitialized()) {
return CompletableFuture.completedFuture(
experimentNames.associateWith { DynamicConfig.empty(it) }
)
}
return statsigServer.batchGetExperimentsAsync(user, experimentNames)
}
}
}
23 changes: 23 additions & 0 deletions src/main/kotlin/com/statsig/sdk/StatsigServer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,12 @@ sealed class StatsigServer {
@JvmSynthetic
internal abstract fun getCustomLogger(): LoggerInterface


abstract fun batchGetExperimentsAsync(
user: StatsigUser,
experimentNames: List<String>
): CompletableFuture<Map<String, DynamicConfig>>

companion object {

@JvmStatic
Expand Down Expand Up @@ -1348,4 +1354,21 @@ private class StatsigServerImpl() :
throw e
}
}

override fun batchGetExperimentsAsync(
user: StatsigUser,
experimentNames: List<String>
): CompletableFuture<Map<String, DynamicConfig>> {
if (!isSDKInitialized()) {
return CompletableFuture.completedFuture(
experimentNames.associateWith { DynamicConfig.empty(it) }
)
}

return statsigScope.future {
experimentNames.associateWith { experimentName ->
getExperiment(user, experimentName)
}
}
}
}
99 changes: 99 additions & 0 deletions src/test/java/com/statsig/sdk/StatsigServerTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package com.statsig.sdk

import junit.framework.TestCase.*
import kotlinx.coroutines.runBlocking
import org.junit.Test

class StatsigServerTest {
@Test
fun `test batchGetExperimentsAsync returns multiple experiments correctly`() = runBlocking {
val server = StatsigServer.create()
server.initialize(
"secret-key",
StatsigOptions()
)

val user = StatsigUser("123")
val experimentNames = listOf("experiment_1", "experiment_2", "experiment_3")

val result = server.batchGetExperimentsAsync(user, experimentNames).get()

assertNotNull(result)
assertEquals(3, result.size)
experimentNames.forEach { name ->
assertTrue(result.containsKey(name))
val config = result[name]
assertNotNull(config)
assertEquals(name, config?.name)

val singleResult = server.getExperimentAsync(user, name).get()
assertEquals(singleResult.value, config?.value)
assertEquals(singleResult.ruleID, config?.ruleID)
}
}

@Test
fun `test batchGetExperimentsAsync returns empty configs when not initialized`() = runBlocking {
val server = StatsigServer.create()
val user = StatsigUser("123")
val experimentNames = listOf("experiment_1", "experiment_2")

val result = server.batchGetExperimentsAsync(user, experimentNames).get()

assertNotNull(result)
assertEquals(2, result.size)
experimentNames.forEach { name ->
assertTrue(result.containsKey(name))
val config = result[name]
assertNotNull(config)
assertEquals(name, config?.name)
assertTrue(config?.value?.isEmpty() ?: false)
}
}

@Test
fun `test batchGetExperimentsAsync with empty experiment list`() = runBlocking {
val server = StatsigServer.create()
server.initialize(
"secret-key",
StatsigOptions()
)

val user = StatsigUser("123")
val experimentNames = emptyList<String>()

val result = server.batchGetExperimentsAsync(user, experimentNames).get()

assertNotNull(result)
assertTrue(result.isEmpty())
}

@Test
fun `test batchGetExperimentsAsync results match individual getExperiment calls`() = runBlocking {
val server = StatsigServer.create()
server.initialize(
"secret-key",
StatsigOptions()
)

val user = StatsigUser("123")
val experimentNames = listOf("experiment_1", "experiment_2")

val batchResults = server.batchGetExperimentsAsync(user, experimentNames).get()
val individualResults = experimentNames.associateWith {
server.getExperimentAsync(user, it).get()
}

assertEquals(individualResults.size, batchResults.size)
experimentNames.forEach { name ->
val batchConfig = batchResults[name]
val individualConfig = individualResults[name]

assertNotNull(batchConfig)
assertNotNull(individualConfig)
assertEquals(individualConfig?.value, batchConfig?.value)
assertEquals(individualConfig?.ruleID, batchConfig?.ruleID)
assertEquals(individualConfig?.name, batchConfig?.name)
}
}
}