Skip to content

Commit

Permalink
feat: Test that client-side SDKs send correct version in events. (#240)
Browse files Browse the repository at this point in the history
This isn't the ideal modification, but it changes things to ensure that
by default client-side SDKs are sent a flagVersion in addition to a
version and that they are not the same by default.

Events should use the flagVersion when available.
  • Loading branch information
kinyoklion authored Oct 8, 2024
1 parent 87d47b4 commit 0b4df84
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 13 deletions.
4 changes: 3 additions & 1 deletion data/generic_factory.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package data

import (
o "github.com/launchdarkly/sdk-test-harness/v2/framework/opt"
"github.com/launchdarkly/sdk-test-harness/v2/mockld"
"github.com/launchdarkly/sdk-test-harness/v2/servicedef"

Expand Down Expand Up @@ -40,7 +41,8 @@ func NewMemoizingClientSideFlagFactory(
f := &MemoizingFactory[servicedef.ValueType, mockld.ClientSDKFlagWithKey]{
factoryFn: factoryFn,
transformVersionFn: func(f mockld.ClientSDKFlagWithKey, v int) mockld.ClientSDKFlagWithKey {
f.Version = v
f.Version = v + 10
f.FlagVersion = o.Some(v)
return f
},
nextVersion: startingVersion,
Expand Down
4 changes: 2 additions & 2 deletions sdktests/client_side_events_eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func doClientSideFeatureEventTests(t *ldtest.T) {
matchFeatureEvent := IsValidFeatureEventWithConditions(
t, false, context,
m.JSONProperty("key").Should(m.Equal(flag.Key)),
m.JSONProperty("version").Should(m.Equal(flag.Version)),
m.JSONProperty("version").Should(m.Equal(flag.FlagVersion.Value())),
m.JSONProperty("value").Should(m.JSONEqual(expectedValue)),
m.JSONOptProperty("variation").Should(m.JSONEqual(expectedVariation)),
maybeReason(withReason, expectedReason),
Expand Down Expand Up @@ -326,7 +326,7 @@ func doClientSideDebugEventTests(t *ldtest.T) {
HasAnyCreationDate(),
m.JSONProperty("key").Should(m.Equal(flag.Key)),
HasContextObjectWithMatchingKeys(context),
m.JSONProperty("version").Should(m.Equal(flag.Version)),
m.JSONProperty("version").Should(m.Equal(flag.FlagVersion.Value())),
m.JSONProperty("value").Should(m.JSONEqual(result.Value)),
m.JSONProperty("variation").Should(m.JSONEqual(flag.Variation)),
maybeReason(withReasons, expectedReason),
Expand Down
3 changes: 2 additions & 1 deletion sdktests/client_side_events_experimentation.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ func doClientSideExperimentationEventTests(t *ldtest.T) {
flagKey := "flag-key"
data := mockld.NewClientSDKDataBuilder().
Flag(flagKey, mockld.ClientSDKFlag{
Version: flagVersion,
FlagVersion: o.Some(flagVersion),
Version: flagVersion + 20,
Value: expectedValue,
Variation: o.Some(expectedVariation),
Reason: o.Some(expectedReason),
Expand Down
22 changes: 13 additions & 9 deletions sdktests/client_side_events_summary.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,23 @@ func doClientSideSummaryEventTests(t *ldtest.T) {
func doClientSideSummaryEventBasicTest(t *ldtest.T) {
flag1Key := "flag1"
flag1Result1 := mockld.ClientSDKFlag{
Value: ldvalue.String("value1-a"),
Variation: o.Some(0),
Version: 1,
Value: ldvalue.String("value1-a"),
Variation: o.Some(0),
FlagVersion: o.Some(1),
Version: 11,
}
flag1Result2 := mockld.ClientSDKFlag{
Value: ldvalue.String("value1-b"),
Variation: o.Some(2),
Version: 2,
Value: ldvalue.String("value1-b"),
Variation: o.Some(2),
FlagVersion: o.Some(2),
Version: 12,
}
flag2Key := "flag2"
flag2Result := mockld.ClientSDKFlag{
Value: ldvalue.String("value-b"),
Variation: o.Some(2),
Version: 2,
// Omitting FlagVersion to check fallback logic.
Version: 13,
}

contextA := ldcontext.New("user-a")
Expand Down Expand Up @@ -80,14 +83,15 @@ func doClientSideSummaryEventBasicTest(t *ldtest.T) {
m.KV(flag1Key, m.MapOf(
m.KV("default", m.JSONEqual(default1)),
m.KV("counters", m.ItemsInAnyOrder(
flagCounter(flag1Result1.Value, flag1Result1.Variation.Value(), flag1Result1.Version, 2),
flagCounter(flag1Result2.Value, flag1Result2.Variation.Value(), flag1Result2.Version, 1),
flagCounter(flag1Result1.Value, flag1Result1.Variation.Value(), flag1Result1.FlagVersion.Value(), 2),
flagCounter(flag1Result2.Value, flag1Result2.Variation.Value(), flag1Result2.FlagVersion.Value(), 1),
)),
m.KV("contextKinds", anyContextKindsList()),
)),
m.KV(flag2Key, m.MapOf(
m.KV("default", m.JSONEqual(default2)),
m.KV("counters", m.ItemsInAnyOrder(
// Did not include a FlagVersion, so it should use version.
flagCounter(flag2Result.Value, flag2Result.Variation.Value(), flag2Result.Version, 1),
)),
m.KV("contextKinds", anyContextKindsList()),
Expand Down

0 comments on commit 0b4df84

Please sign in to comment.