-
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 3 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,216 @@ 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{}, | ||
}) | ||
|
||
mockListener := &MockedListener{} | ||
mockListener.On("OnReady").Return() | ||
mockListener.On("OnRegistered", mock.AnythingOfType("ClientData")) | ||
mockListener.On("OnCount", feature, false).Return() | ||
mockListener.On("OnError").Return() | ||
|
||
thomasheartman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
client, err := NewClient( | ||
WithUrl(mockerServer), | ||
WithAppName(mockAppName), | ||
WithInstanceId(mockInstanceId), | ||
WithListener(mockListener), | ||
) | ||
|
||
assert.NoError(err) | ||
client.WaitForReady() | ||
|
||
fallbackVariant := api.Variant{ | ||
Name: "fallback-variant", | ||
FeatureEnabled: true, | ||
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. Spacing is a bit weird here 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. That's probably a tabs/spaces thing. Weird that my editor changed to using whatever the other is. Anyway, that line is gone now, so that's not an issue anymore. |
||
} | ||
|
||
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{}, | ||
}) | ||
|
||
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", | ||
FeatureEnabled: false, | ||
} | ||
|
||
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) | ||
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{}, | ||
}) | ||
|
||
mockListener := &MockedListener{} | ||
mockListener.On("OnReady").Return() | ||
mockListener.On("OnRegistered", mock.AnythingOfType("ClientData")) | ||
mockListener.On("OnCount", feature, false).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", | ||
FeatureEnabled: true, | ||
} | ||
|
||
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") | ||
} |
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.