-
Notifications
You must be signed in to change notification settings - Fork 54
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: variant fallback usage #171
Changes from all commits
6bdcac9
937df2b
c99a893
d4a5c32
251d167
d725015
bc87bd3
12e10a4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1144,3 +1144,276 @@ func TestClient_VariantFromEnabledFeatureWithNoVariants(t *testing.T) { | |
|
||
assert.True(gock.IsDone(), "there should be no more mocks") | ||
} | ||
|
||
func TestGetVariantWithFallbackVariantWhenFeatureDisabled(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-disabled" | ||
features := []api.Feature{ | ||
{ | ||
Name: feature, | ||
Description: "feature-desc", | ||
Enabled: false, | ||
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{}, | ||
}) | ||
|
||
client, err := NewClient( | ||
WithUrl(mockerServer), | ||
WithAppName(mockAppName), | ||
WithInstanceId(mockInstanceId), | ||
WithListener(&NoopListener{}), | ||
) | ||
|
||
assert.NoError(err) | ||
client.WaitForReady() | ||
|
||
fallbackVariant := api.Variant{ | ||
Name: "fallback-variant", | ||
} | ||
|
||
variant := client.GetVariant(feature, WithVariantFallback(&fallbackVariant)) | ||
|
||
assert.False(variant.Enabled) | ||
|
||
assert.False(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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. While I get that the fallbackVariant = api.Variant{
Name: "fallback-variant",
FeatureEnabled: true,
}
fallbackFunc := func(feature string, ctx *context.Context) *api.Variant {
return &fallbackVariant
} then later did the same assert.False(variantWithFallbackFunc.Enabled)
assert.False(variantWithFallbackFunc.FeatureEnabled) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Removed changing the feature enabled in 251d167 as per your suggestion. With that change, is there still anything here you'd like to address? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nope, lgtm |
||
|
||
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{}, | ||
}) | ||
|
||
client, err := NewClient( | ||
WithUrl(mockerServer), | ||
WithAppName(mockAppName), | ||
WithInstanceId(mockInstanceId), | ||
WithListener(&NoopListener{}), | ||
) | ||
|
||
assert.NoError(err) | ||
client.WaitForReady() | ||
|
||
fallbackVariant := api.Variant{ | ||
Name: "fallback-variant", | ||
} | ||
|
||
variant := client.GetVariant(feature, WithVariantFallback(&fallbackVariant)) | ||
|
||
assert.False(variant.Enabled) | ||
|
||
// Because we return the fallback variant and the variant | ||
// doesn't have a FeatureEnabled property, it will always be | ||
// false, regardless of whether the actual feature is enabled | ||
// or not. | ||
assert.False(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) | ||
This comment was marked as resolved.
Sorry, something went wrong. |
||
|
||
assert.True(gock.IsDone(), "there should be no more mocks") | ||
} | ||
|
||
func TestGetVariantWithFallbackVariantWhenFeatureDoesntExist(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{} | ||
|
||
gock.New(mockerServer). | ||
Get("/client/features"). | ||
Reply(200). | ||
JSON(api.FeatureResponse{ | ||
Features: features, | ||
Segments: []api.Segment{}, | ||
}) | ||
|
||
client, err := NewClient( | ||
WithUrl(mockerServer), | ||
WithAppName(mockAppName), | ||
WithInstanceId(mockInstanceId), | ||
WithListener(&NoopListener{}), | ||
) | ||
|
||
assert.NoError(err) | ||
client.WaitForReady() | ||
|
||
fallbackVariant := api.Variant{ | ||
Name: "fallback-variant", | ||
} | ||
|
||
variant := client.GetVariant(feature, WithVariantFallback(&fallbackVariant)) | ||
|
||
assert.False(variant.Enabled) | ||
|
||
assert.False(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) | ||
This comment was marked as resolved.
Sorry, something went wrong. |
||
|
||
assert.True(gock.IsDone(), "there should be no more mocks") | ||
} | ||
|
||
func TestGetVariant_FallbackVariantFeatureEnabledSettingIsLeftUnchanged(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) | ||
|
||
enabledFeatureNoVariants := "enabled-feature" | ||
disabledFeature := "disabled-feature" | ||
features := []api.Feature{ | ||
{ | ||
Name: disabledFeature, | ||
Description: "feature-desc", | ||
Enabled: false, | ||
CreatedAt: time.Date(1974, time.May, 19, 1, 2, 3, 4, time.UTC), | ||
Strategy: "default-strategy", | ||
Strategies: []api.Strategy{ | ||
{ | ||
Id: 1, | ||
Name: "default", | ||
}, | ||
}, | ||
}, | ||
{ | ||
Name: enabledFeatureNoVariants, | ||
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{}, | ||
}) | ||
|
||
client, err := NewClient( | ||
WithUrl(mockerServer), | ||
WithAppName(mockAppName), | ||
WithInstanceId(mockInstanceId), | ||
WithListener(&NoopListener{}), | ||
) | ||
|
||
assert.NoError(err) | ||
client.WaitForReady() | ||
|
||
fallbackVariantFeatureEnabled := api.Variant{ | ||
Name: "fallback-variant-feature-enabled", | ||
FeatureEnabled: true, | ||
} | ||
fallbackVariantFeatureDisabled := api.Variant{ | ||
Name: "fallback-variant-feature-disabled", | ||
FeatureEnabled: false, | ||
} | ||
|
||
variantForEnabledFeatureNoVariants := client.GetVariant(enabledFeatureNoVariants, WithVariantFallback(&fallbackVariantFeatureDisabled)) | ||
|
||
assert.False(variantForEnabledFeatureNoVariants.FeatureEnabled) | ||
|
||
variantForDisabledFeature := client.GetVariant(disabledFeature, WithVariantFallback(&fallbackVariantFeatureEnabled)) | ||
|
||
assert.True(variantForDisabledFeature.FeatureEnabled) | ||
|
||
assert.True(gock.IsDone(), "there should be no more mocks") | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know when we ever hit this? I couldn't find a way to trigger it. One of the tests uses a default strategy (which is always enabled), but with
f.Enabled
set to false. This seems to still hit the previousstrategyResult.enabled
, though, so I suspect this isn't used anymore, but I'll leave it in combined with missing flags regardless.