Skip to content

Commit

Permalink
Use fallback value if client is not initialized
Browse files Browse the repository at this point in the history
If no defaultClient is initialized, we should return the provided
fallback value instead of `false`.
  • Loading branch information
Lucaber committed Oct 4, 2023
1 parent 2b58cc4 commit 66aab60
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
6 changes: 5 additions & 1 deletion unleash.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ type RepositoryListener interface {
// IsEnabled queries the default client whether or not the specified feature is enabled or not.
func IsEnabled(feature string, options ...FeatureOption) bool {
if defaultClient == nil {
return false
var opts featureOption
for _, o := range options {
o(&opts)
}
return handleFallback(opts, feature, opts.ctx).Enabled
}
return defaultClient.IsEnabled(feature, options...)
}
Expand Down
7 changes: 7 additions & 0 deletions unleash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,10 @@ func Test_withVariantsAndANonExistingStrategyName(t *testing.T) {
t.Fatalf("Expected feature to be disabled because Environment does not exist as strategy")
}
}

func Test_IsEnabledWithUninitializedClient(t *testing.T) {
result := unleash.IsEnabled("foo", unleash.WithFallback(true))
if !result {
t.Fatalf("Expected true")
}
}

0 comments on commit 66aab60

Please sign in to comment.