Skip to content

Commit

Permalink
Use fallback value if client is not initialized (#137)
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 authored Oct 5, 2023
1 parent 2b58cc4 commit ed4bee5
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 ed4bee5

Please sign in to comment.