Skip to content

Commit

Permalink
fix: handle case where flag has no variants
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasheartman committed Dec 18, 2023
1 parent 72f7c0b commit fd12411
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
10 changes: 10 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,16 @@ func (uc *Client) getVariantWithoutMetrics(feature string, options ...VariantOpt
}

if len(f.Variants) == 0 {
if opts.variantFallbackFunc != nil {
variant := opts.variantFallbackFunc(feature, ctx)
if variant != nil {
variant.FeatureEnabled = true
}
return variant
} else if opts.variantFallback != nil {
opts.variantFallback.FeatureEnabled = true
return opts.variantFallback
}
return disabledVariantFeatureEnabled
}

Expand Down
74 changes: 74 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1224,3 +1224,77 @@ func TestGetVariantWithFallbackVariantWhenFeatureDisabled(t *testing.T) {

assert.True(gock.IsDone(), "there should be no more mocks")
}

func TestGetVariantWithFallbackVariantWhenFeatureEnabledButNoVariants(t *testing.T) {
assert := assert.New(t)
defer gock.OffAll()

gock.New(mockerServer).
Post("/client/register").
MatchHeader("UNLEASH-APPNAME", mockAppName).
MatchHeader("UNLEASH-INSTANCEID", mockInstanceId).
Reply(200)

feature := "feature-no-variants"
features := []api.Feature{
{
Name: feature,
Description: "feature-desc",
Enabled: true,
CreatedAt: time.Date(1974, time.May, 19, 1, 2, 3, 4, time.UTC),
Strategy: "default-strategy",
Strategies: []api.Strategy{
{
Id: 1,
Name: "default",
},
},
},
}

gock.New(mockerServer).
Get("/client/features").
Reply(200).
JSON(api.FeatureResponse{
Features: features,
Segments: []api.Segment{},
})

mockListener := &MockedListener{}
mockListener.On("OnReady").Return()
mockListener.On("OnRegistered", mock.AnythingOfType("ClientData"))
mockListener.On("OnCount", feature, true).Return()
mockListener.On("OnError").Return()

client, err := NewClient(
WithUrl(mockerServer),
WithAppName(mockAppName),
WithInstanceId(mockInstanceId),
WithListener(mockListener),
)

assert.NoError(err)
client.WaitForReady()

fallbackVariant := api.Variant{
Name: "fallback-variant",
}

variant := client.GetVariant(feature, WithVariantFallback(&fallbackVariant))

assert.False(variant.Enabled)

assert.True(variant.FeatureEnabled)

assert.Equal(fallbackVariant, *variant)

fallbackFunc := func(feature string, ctx *context.Context) *api.Variant {
return &fallbackVariant
}

variantWithFallbackFunc := client.GetVariant(feature, WithVariantFallbackFunc(fallbackFunc))

assert.Equal(fallbackVariant, *variantWithFallbackFunc)

assert.True(gock.IsDone(), "there should be no more mocks")
}

0 comments on commit fd12411

Please sign in to comment.