Skip to content

Commit

Permalink
fix: use config fallback for appName and environment (#70)
Browse files Browse the repository at this point in the history
  • Loading branch information
kwasniew authored Feb 7, 2024
1 parent 1b6adb6 commit 182f752
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/main/kotlin/io/getunleash/UnleashClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,14 @@ class UnleashClient(
unleashFetcher = fetcher,
cache = cache,
config = unleashConfig,
context = unleashContext,
context = this.getContext(),
unleashConfig.pollingMode
)
is FilePollingMode -> FilePollingPolicy(
unleashFetcher = fetcher,
cache = cache,
config = unleashConfig,
context = unleashContext,
context = this.getContext(),
unleashConfig.pollingMode
)
else -> throw InvalidParameterException("The polling mode parameter is invalid")
Expand Down Expand Up @@ -98,13 +98,16 @@ class UnleashClient(
}

override fun updateContext(context: UnleashContext): CompletableFuture<Void> {
refreshPolicy.context = context
this.unleashContext = context
refreshPolicy.context = this.getContext()
return refreshPolicy.refreshAsync()
}

override fun getContext(): UnleashContext {
return unleashContext
return unleashContext.copy(
appName = unleashContext.appName ?: unleashConfig.appName,
environment = unleashContext.environment ?: unleashConfig.environment
)
}

override fun close() {
Expand Down
28 changes: 28 additions & 0 deletions src/test/kotlin/io/getunleash/UnleashClientTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -188,4 +188,32 @@ class UnleashClientTest {
assertThat(updatedFuture).succeedsWithin(Duration.ofSeconds(2))
}
}

@Test
fun `Context falls back to config values for appName and environment when not provided`() {
val fallbackConfig = config.newBuilder().appName("fallbackApp").environment("fallbackEnv").build()
val incompleteContext = UnleashContext.newBuilder().userId("someUserId").build()

UnleashClient.newBuilder().unleashConfig(fallbackConfig).unleashContext(incompleteContext).build().use { client ->
val usedContext = client.getContext()

assertThat(usedContext.appName).isEqualTo("fallbackApp")
assertThat(usedContext.environment).isEqualTo("fallbackEnv")
assertThat(usedContext.userId).isEqualTo("someUserId")
}
}

@Test
fun `Explicit context takes precedence over config appName and environment`() {
val fallbackConfig = config.newBuilder().appName("fallbackApp").environment("fallbackEnv").build()
val explicitContext = UnleashContext.newBuilder().appName("contextApp").environment("contextEnvironment").build()

UnleashClient.newBuilder().unleashConfig(fallbackConfig).unleashContext(explicitContext).build().use { client ->
val usedContext = client.getContext()

assertThat(usedContext.appName).isEqualTo("contextApp")
assertThat(usedContext.environment).isEqualTo("contextEnvironment")
}
}

}

0 comments on commit 182f752

Please sign in to comment.