Skip to content

Commit

Permalink
chore: Introduction of redis-backed daemon-mode tests
Browse files Browse the repository at this point in the history
  • Loading branch information
keelerm84 committed Oct 4, 2024
1 parent 859f84d commit 2239f27
Show file tree
Hide file tree
Showing 12 changed files with 428 additions and 14 deletions.
3 changes: 2 additions & 1 deletion framework/helpers/channels_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import (
"testing"
"time"

"github.com/launchdarkly/sdk-test-harness/v2/framework/opt"
"github.com/stretchr/testify/assert"

"github.com/launchdarkly/sdk-test-harness/v2/framework/opt"
)

func TestNonBlockingSend(t *testing.T) {
Expand Down
3 changes: 2 additions & 1 deletion framework/ldtest/errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package ldtest
import (
"testing"

"github.com/launchdarkly/sdk-test-harness/v2/framework/ldtest/internal"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/launchdarkly/sdk-test-harness/v2/framework/ldtest/internal"
)

func TestStacktrace(t *testing.T) {
Expand Down
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@ require (
github.com/launchdarkly/go-sdk-common/v3 v3.1.0
github.com/launchdarkly/go-server-sdk-evaluation/v3 v3.0.0
github.com/launchdarkly/go-test-helpers/v2 v2.3.2
github.com/redis/go-redis/v9 v9.6.1
github.com/stretchr/testify v1.7.0
golang.org/x/exp v0.0.0-20220823124025-807a23277127
gopkg.in/yaml.v3 v3.0.0
)

require (
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/launchdarkly/go-semver v1.0.2 // indirect
github.com/mailru/easyjson v0.7.6 // indirect
Expand Down
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs=
github.com/bsm/gomega v1.27.10 h1:yeMWxP2pV2fG3FgAODIY8EiRE3dy0aeFYt4l7wh6yKA=
github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44=
github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
github.com/fatih/color v1.13.0 h1:8LOYc1KYPPmyKMuN8QV2DNRWNbLo6LZ0iLs8+mlH53w=
github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk=
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
Expand Down Expand Up @@ -32,6 +38,8 @@ github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/redis/go-redis/v9 v9.6.1 h1:HHDteefn6ZkTtY5fGUE8tj8uy85AHk6zP7CpzIAM0y4=
github.com/redis/go-redis/v9 v9.6.1/go.mod h1:0C0c6ycQsdpVNQpxb1njEQIqkx5UcsM8FJCQLgE9+RA=
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand Down
3 changes: 2 additions & 1 deletion sdktests/custom_matchers_contexts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import (
"encoding/json"
"testing"

"github.com/stretchr/testify/require"

"github.com/launchdarkly/go-sdk-common/v3/ldcontext"
m "github.com/launchdarkly/go-test-helpers/v2/matchers"
"github.com/stretchr/testify/require"
)

func TestJSONMatchesContext(t *testing.T) {
Expand Down
116 changes: 116 additions & 0 deletions sdktests/server_side_data_system_base.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package sdktests

import (
"time"

"github.com/redis/go-redis/v9"
"github.com/stretchr/testify/require"

"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"
)

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
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"))
}
35 changes: 35 additions & 0 deletions sdktests/server_side_data_system_persistence_store.go
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
}
Loading

0 comments on commit 2239f27

Please sign in to comment.