Skip to content

Commit 182f752

Browse files
authored
fix: use config fallback for appName and environment (#70)
1 parent 1b6adb6 commit 182f752

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed

src/main/kotlin/io/getunleash/UnleashClient.kt

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,14 @@ class UnleashClient(
4848
unleashFetcher = fetcher,
4949
cache = cache,
5050
config = unleashConfig,
51-
context = unleashContext,
51+
context = this.getContext(),
5252
unleashConfig.pollingMode
5353
)
5454
is FilePollingMode -> FilePollingPolicy(
5555
unleashFetcher = fetcher,
5656
cache = cache,
5757
config = unleashConfig,
58-
context = unleashContext,
58+
context = this.getContext(),
5959
unleashConfig.pollingMode
6060
)
6161
else -> throw InvalidParameterException("The polling mode parameter is invalid")
@@ -98,13 +98,16 @@ class UnleashClient(
9898
}
9999

100100
override fun updateContext(context: UnleashContext): CompletableFuture<Void> {
101-
refreshPolicy.context = context
102101
this.unleashContext = context
102+
refreshPolicy.context = this.getContext()
103103
return refreshPolicy.refreshAsync()
104104
}
105105

106106
override fun getContext(): UnleashContext {
107-
return unleashContext
107+
return unleashContext.copy(
108+
appName = unleashContext.appName ?: unleashConfig.appName,
109+
environment = unleashContext.environment ?: unleashConfig.environment
110+
)
108111
}
109112

110113
override fun close() {

src/test/kotlin/io/getunleash/UnleashClientTest.kt

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,4 +188,32 @@ class UnleashClientTest {
188188
assertThat(updatedFuture).succeedsWithin(Duration.ofSeconds(2))
189189
}
190190
}
191+
192+
@Test
193+
fun `Context falls back to config values for appName and environment when not provided`() {
194+
val fallbackConfig = config.newBuilder().appName("fallbackApp").environment("fallbackEnv").build()
195+
val incompleteContext = UnleashContext.newBuilder().userId("someUserId").build()
196+
197+
UnleashClient.newBuilder().unleashConfig(fallbackConfig).unleashContext(incompleteContext).build().use { client ->
198+
val usedContext = client.getContext()
199+
200+
assertThat(usedContext.appName).isEqualTo("fallbackApp")
201+
assertThat(usedContext.environment).isEqualTo("fallbackEnv")
202+
assertThat(usedContext.userId).isEqualTo("someUserId")
203+
}
204+
}
205+
206+
@Test
207+
fun `Explicit context takes precedence over config appName and environment`() {
208+
val fallbackConfig = config.newBuilder().appName("fallbackApp").environment("fallbackEnv").build()
209+
val explicitContext = UnleashContext.newBuilder().appName("contextApp").environment("contextEnvironment").build()
210+
211+
UnleashClient.newBuilder().unleashConfig(fallbackConfig).unleashContext(explicitContext).build().use { client ->
212+
val usedContext = client.getContext()
213+
214+
assertThat(usedContext.appName).isEqualTo("contextApp")
215+
assertThat(usedContext.environment).isEqualTo("contextEnvironment")
216+
}
217+
}
218+
191219
}

0 commit comments

Comments
 (0)