-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Introduction of redis-backed daemon-mode tests
- Loading branch information
Showing
9 changed files
with
406 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
package sdktests | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/launchdarkly/go-sdk-common/v3/ldcontext" | ||
"github.com/launchdarkly/go-sdk-common/v3/ldvalue" | ||
"github.com/launchdarkly/go-server-sdk-evaluation/v3/ldbuilders" | ||
"github.com/launchdarkly/sdk-test-harness/v2/framework/ldtest" | ||
"github.com/launchdarkly/sdk-test-harness/v2/servicedef" | ||
"github.com/redis/go-redis/v9" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func doServerSideDataSystemTests(t *ldtest.T) { | ||
rdb := redis.NewClient(&redis.Options{ | ||
Addr: "localhost:6379", | ||
Password: "", // no password set | ||
DB: 0, // use default DB | ||
}) | ||
|
||
newServerSideDataSystemTests(t, RedisPersistenceStore{redis: rdb}).Run(t) | ||
} | ||
|
||
type ServerSideDataSystemTests struct { | ||
persistence PersistenceStore | ||
redis *redis.Client | ||
initialFlags map[string]string | ||
} | ||
|
||
func newServerSideDataSystemTests(t *ldtest.T, persistence PersistenceStore) *ServerSideDataSystemTests { | ||
flagKeyBytes, err := | ||
ldbuilders.NewFlagBuilder("flag-key").Version(1). | ||
On(true).Variations(ldvalue.String("off"), ldvalue.String("match"), ldvalue.String("fallthrough")). | ||
OffVariation(0). | ||
FallthroughVariation(2). | ||
Build().MarshalJSON() | ||
require.NoError(t, err) | ||
|
||
initialFlags := map[string]string{"flag-key": string(flagKeyBytes)} | ||
|
||
uncachedFlagKeyBytes, err := | ||
ldbuilders.NewFlagBuilder("uncached-flag-key").Version(1). | ||
On(true).Variations(ldvalue.String("off"), ldvalue.String("match"), ldvalue.String("fallthrough")). | ||
OffVariation(0). | ||
FallthroughVariation(2). | ||
Build().MarshalJSON() | ||
require.NoError(t, err) | ||
|
||
initialFlags["uncached-flag-key"] = string(uncachedFlagKeyBytes) | ||
|
||
return &ServerSideDataSystemTests{ | ||
persistence: persistence, | ||
initialFlags: initialFlags, | ||
} | ||
} | ||
|
||
func (s *ServerSideDataSystemTests) Run(t *ldtest.T) { | ||
t.Run("uses default prefix", s.usesDefaultPrefix) | ||
t.Run("uses custom prefix", s.usesCustomPrefix) | ||
|
||
t.Run("read-only", s.doReadOnlyTests) | ||
} | ||
|
||
func (s *ServerSideDataSystemTests) usesDefaultPrefix(t *ldtest.T) { | ||
require.NoError(t, s.persistence.Reset()) | ||
require.NoError(t, s.persistence.WriteData("launchdarkly:features", s.initialFlags)) | ||
|
||
dataSystem := NewDataSystem() | ||
dataSystem.AddPersistence(servicedef.SDKConfigDataSystemPersistence{ | ||
Store: servicedef.SDKConfigDataSystemPersistenceStore{ | ||
Type: servicedef.Redis, | ||
DSN: s.persistence.DSN(), | ||
}, | ||
Cache: servicedef.SDKConfigDataSystemPersistenceCache{ | ||
Mode: servicedef.Off, | ||
}, | ||
}) | ||
|
||
client := NewSDKClient(t, dataSystem) | ||
pollUntilFlagValueUpdated(t, client, "flag-key", ldcontext.New("user-key"), ldvalue.String("default"), ldvalue.String("fallthrough"), ldvalue.String("default")) | ||
} | ||
|
||
func (s *ServerSideDataSystemTests) usesCustomPrefix(t *ldtest.T) { | ||
require.NoError(t, s.persistence.Reset()) | ||
customPrefix := "custom-prefix" | ||
|
||
dataSystem := NewDataSystem() | ||
dataSystem.AddPersistence(servicedef.SDKConfigDataSystemPersistence{ | ||
Store: servicedef.SDKConfigDataSystemPersistenceStore{ | ||
Type: servicedef.Redis, | ||
Prefix: customPrefix, | ||
DSN: s.persistence.DSN(), | ||
}, | ||
Cache: servicedef.SDKConfigDataSystemPersistenceCache{ | ||
Mode: servicedef.Off, | ||
}, | ||
}) | ||
|
||
client := NewSDKClient(t, dataSystem) | ||
|
||
require.Never( | ||
t, | ||
checkForUpdatedValue(t, client, "flag-key", ldcontext.New("user-key"), ldvalue.String("default"), ldvalue.String("fallthrough"), ldvalue.String("default")), | ||
time.Millisecond*100, | ||
time.Millisecond*20, | ||
"flag value was updated, but it should not have been", | ||
) | ||
|
||
require.NoError(t, s.persistence.WriteData(customPrefix+":features", s.initialFlags)) | ||
|
||
pollUntilFlagValueUpdated(t, client, "flag-key", ldcontext.New("user-key"), ldvalue.String("default"), ldvalue.String("fallthrough"), ldvalue.String("default")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package sdktests | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/redis/go-redis/v9" | ||
) | ||
|
||
type PersistenceStore interface { | ||
DSN() string | ||
|
||
WriteData(key string, data map[string]string) error | ||
|
||
Reset() error | ||
} | ||
|
||
type RedisPersistenceStore struct { | ||
redis *redis.Client | ||
} | ||
|
||
func (r RedisPersistenceStore) DSN() string { | ||
return fmt.Sprintf("redis://%s", r.redis.Options().Addr) | ||
} | ||
|
||
func (r RedisPersistenceStore) Reset() error { | ||
var ctx = context.Background() | ||
return r.redis.FlushAll(ctx).Err() | ||
} | ||
|
||
func (r RedisPersistenceStore) WriteData(key string, data map[string]string) error { | ||
var ctx = context.Background() | ||
_, err := r.redis.HSet(ctx, key, data).Result() | ||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,185 @@ | ||
package sdktests | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/launchdarkly/go-sdk-common/v3/ldcontext" | ||
"github.com/launchdarkly/go-sdk-common/v3/ldreason" | ||
"github.com/launchdarkly/go-sdk-common/v3/ldvalue" | ||
m "github.com/launchdarkly/go-test-helpers/v2/matchers" | ||
h "github.com/launchdarkly/sdk-test-harness/v2/framework/helpers" | ||
"github.com/launchdarkly/sdk-test-harness/v2/framework/ldtest" | ||
o "github.com/launchdarkly/sdk-test-harness/v2/framework/opt" | ||
"github.com/launchdarkly/sdk-test-harness/v2/servicedef" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func (s *ServerSideDataSystemTests) doReadOnlyTests(t *ldtest.T) { | ||
t.Run("daemon mode", s.daemonModeTests) | ||
} | ||
|
||
func (s *ServerSideDataSystemTests) daemonModeTests(t *ldtest.T) { | ||
t.Run("ignores database initialization flag", s.ignoresInitialization) | ||
t.Run("can disable cache", s.canDisableCache) | ||
t.Run("caches flag for duration", s.cachesFlagForDuration) | ||
t.Run("caches flag forever", s.cachesFlagForever) | ||
} | ||
|
||
func (s *ServerSideDataSystemTests) ignoresInitialization(t *ldtest.T) { | ||
dataSystem := NewDataSystem() | ||
dataSystem.AddPersistence(servicedef.SDKConfigDataSystemPersistence{ | ||
Store: servicedef.SDKConfigDataSystemPersistenceStore{ | ||
Type: servicedef.Redis, | ||
DSN: s.persistence.DSN(), | ||
}, | ||
Cache: servicedef.SDKConfigDataSystemPersistenceCache{ | ||
Mode: servicedef.Off, | ||
}, | ||
}) | ||
context := ldcontext.New("user-key") | ||
|
||
require.NoError(t, s.persistence.Reset()) | ||
client := NewSDKClient(t, dataSystem) | ||
|
||
h.RequireEventually(t, func() bool { | ||
result := client.EvaluateFlag(t, servicedef.EvaluateFlagParams{ | ||
FlagKey: "flag-key", | ||
Context: o.Some(context), | ||
ValueType: servicedef.ValueTypeAny, | ||
DefaultValue: ldvalue.String("default"), | ||
Detail: true, | ||
}) | ||
|
||
return result.Reason.IsDefined() && | ||
result.Reason.Value().GetErrorKind() == ldreason.EvalErrorFlagNotFound | ||
}, time.Second, time.Millisecond*20, "flag was found before it should have been") | ||
|
||
require.NoError(t, s.persistence.WriteData("launchdarkly:features", s.initialFlags)) | ||
pollUntilFlagValueUpdated(t, client, "flag-key", context, ldvalue.String("default"), ldvalue.String("fallthrough"), ldvalue.String("default")) | ||
|
||
} | ||
|
||
func (s *ServerSideDataSystemTests) canDisableCache(t *ldtest.T) { | ||
require.NoError(t, s.persistence.Reset()) | ||
require.NoError(t, s.persistence.WriteData("launchdarkly:features", s.initialFlags)) | ||
|
||
dataSystem := NewDataSystem() | ||
dataSystem.AddPersistence(servicedef.SDKConfigDataSystemPersistence{ | ||
Store: servicedef.SDKConfigDataSystemPersistenceStore{ | ||
Type: servicedef.Redis, | ||
DSN: s.persistence.DSN(), | ||
}, | ||
Cache: servicedef.SDKConfigDataSystemPersistenceCache{ | ||
Mode: servicedef.Off, | ||
}, | ||
}) | ||
context := ldcontext.New("user-key") | ||
|
||
client := NewSDKClient(t, dataSystem) | ||
pollUntilFlagValueUpdated(t, client, "flag-key", context, ldvalue.String("default"), ldvalue.String("fallthrough"), ldvalue.String("default")) | ||
|
||
// Completely reset the database so there are no valid flag definitions | ||
require.NoError(t, s.persistence.Reset()) | ||
|
||
h.RequireEventually(t, | ||
checkForUpdatedValue(t, client, "flag-key", context, ldvalue.String("fallthrough"), ldvalue.String("default"), ldvalue.String("default")), | ||
time.Second, time.Millisecond*20, "flag value was NOT updated after cache TTL") | ||
} | ||
|
||
func (s *ServerSideDataSystemTests) cachesFlagForDuration(t *ldtest.T) { | ||
dataSystem := NewDataSystem() | ||
dataSystem.AddPersistence(servicedef.SDKConfigDataSystemPersistence{ | ||
Store: servicedef.SDKConfigDataSystemPersistenceStore{ | ||
Type: servicedef.Redis, | ||
DSN: s.persistence.DSN(), | ||
}, | ||
Cache: servicedef.SDKConfigDataSystemPersistenceCache{ | ||
Mode: servicedef.TTL, | ||
TTL: o.Some(1), | ||
}, | ||
}) | ||
context := ldcontext.New("user-key") | ||
|
||
t.Run("will cache found flag for TTL", func(t *ldtest.T) { | ||
require.NoError(t, s.persistence.Reset()) | ||
client := NewSDKClient(t, dataSystem) | ||
|
||
require.NoError(t, s.persistence.WriteData("launchdarkly:features", s.initialFlags)) | ||
|
||
pollUntilFlagValueUpdated(t, client, "flag-key", context, ldvalue.String("default"), ldvalue.String("fallthrough"), ldvalue.String("default")) | ||
|
||
// Completely reset the database so there are no valid flag definitions | ||
require.NoError(t, s.persistence.Reset()) | ||
|
||
h.RequireNever(t, | ||
checkForUpdatedValue(t, client, "flag-key", context, ldvalue.String("fallthrough"), ldvalue.String("default"), ldvalue.String("default")), | ||
time.Millisecond*500, time.Millisecond*20, "flag value was updated before cache TTL") | ||
|
||
h.RequireEventually(t, | ||
checkForUpdatedValue(t, client, "flag-key", context, ldvalue.String("fallthrough"), ldvalue.String("default"), ldvalue.String("default")), | ||
time.Second, time.Millisecond*20, "flag value was NOT updated after cache TTL") | ||
}) | ||
|
||
t.Run("will cache missing flag for TTL", func(t *ldtest.T) { | ||
require.NoError(t, s.persistence.Reset()) | ||
client := NewSDKClient(t, dataSystem) | ||
|
||
result := client.EvaluateFlag(t, servicedef.EvaluateFlagParams{ | ||
FlagKey: "flag-key", | ||
Context: o.Some(context), | ||
ValueType: servicedef.ValueTypeAny, | ||
DefaultValue: ldvalue.String("default"), | ||
Detail: true, | ||
}) | ||
|
||
m.In(t).Assert(result.Value, m.Equal(ldvalue.String("default"))) | ||
m.In(t).Assert(result.Reason.Value().GetErrorKind(), m.Equal(ldreason.EvalErrorFlagNotFound)) | ||
|
||
require.NoError(t, s.persistence.WriteData("launchdarkly:features", s.initialFlags)) | ||
|
||
h.RequireNever(t, | ||
checkForUpdatedValue(t, client, "flag-key", context, ldvalue.String("default"), ldvalue.String("fallthrough"), ldvalue.String("default")), | ||
time.Microsecond*500, time.Millisecond*20, "flag value was updated before cache TTL") | ||
|
||
h.RequireEventually(t, | ||
checkForUpdatedValue(t, client, "flag-key", context, ldvalue.String("default"), ldvalue.String("fallthrough"), ldvalue.String("default")), | ||
time.Second, time.Millisecond*20, "flag value was NOT updated after cache TTL") | ||
}) | ||
|
||
} | ||
|
||
func (s *ServerSideDataSystemTests) cachesFlagForever(t *ldtest.T) { | ||
dataSystem := NewDataSystem() | ||
dataSystem.AddPersistence(servicedef.SDKConfigDataSystemPersistence{ | ||
Store: servicedef.SDKConfigDataSystemPersistenceStore{ | ||
Type: servicedef.Redis, | ||
DSN: s.persistence.DSN(), | ||
}, | ||
Cache: servicedef.SDKConfigDataSystemPersistenceCache{ | ||
Mode: servicedef.Infinite, | ||
}, | ||
}) | ||
context := ldcontext.New("user-key") | ||
|
||
require.NoError(t, s.persistence.Reset()) | ||
require.NoError(t, s.persistence.WriteData("launchdarkly:features", s.initialFlags)) | ||
|
||
client := NewSDKClient(t, dataSystem) | ||
|
||
h.RequireEventually(t, | ||
checkForUpdatedValue(t, client, "flag-key", context, ldvalue.String("default"), ldvalue.String("fallthrough"), ldvalue.String("default")), | ||
time.Millisecond*500, time.Millisecond*20, "flag value was not changed") | ||
|
||
// Reset the store and verify that the flag value is still cached | ||
require.NoError(t, s.persistence.Reset()) | ||
|
||
// Uncached key is gone, so we should NEVER see it evaluate as expected. | ||
h.RequireNever(t, | ||
checkForUpdatedValue(t, client, "uncached-flag-key", context, ldvalue.String("default"), ldvalue.String("fallthrough"), ldvalue.String("default")), | ||
time.Millisecond*500, time.Millisecond*20, "uncached-flag-key was not determined to be missing") | ||
|
||
// We are caching the old flag version forever, so this should also never revert to the default. | ||
h.RequireNever(t, | ||
checkForUpdatedValue(t, client, "flag-key", context, ldvalue.String("fallthrough"), ldvalue.String("default"), ldvalue.String("default")), | ||
time.Millisecond*500, time.Millisecond*20, "flag value was not changed") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.