From c428b468e671b069670311fc4ba1cc672dc53ca8 Mon Sep 17 00:00:00 2001 From: Nadia Mayor Date: Fri, 24 Nov 2023 11:50:14 -0300 Subject: [PATCH 1/2] [SDKS-7687] Add test to validate flag sets when fetch ff --- go.mod | 2 +- go.sum | 2 + splitio/client/input_validator_test.go | 85 ++++++++++++++++++++++++++ 3 files changed, 88 insertions(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 944fe4d..3c10469 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/splitio/go-client/v6 go 1.18 require ( - github.com/splitio/go-split-commons/v5 v5.0.1-0.20231115165340-19ae11b6b7d5 + github.com/splitio/go-split-commons/v5 v5.0.1-0.20231122221702-e3b2ef60041d github.com/splitio/go-toolkit/v5 v5.3.2-0.20231106173125-49e72b9823dc ) diff --git a/go.sum b/go.sum index de223f6..f2a68e2 100644 --- a/go.sum +++ b/go.sum @@ -12,6 +12,8 @@ github.com/redis/go-redis/v9 v9.0.4 h1:FC82T+CHJ/Q/PdyLW++GeCO+Ol59Y4T7R4jbgjvkt github.com/redis/go-redis/v9 v9.0.4/go.mod h1:WqMKv5vnQbRuZstUwxQI195wHy+t4PuXDOjzMvcuQHk= github.com/splitio/go-split-commons/v5 v5.0.1-0.20231115165340-19ae11b6b7d5 h1:tXHXzigms5Umk2HCOv72QqXGVypNSrzpl6E0Mwpxoq4= github.com/splitio/go-split-commons/v5 v5.0.1-0.20231115165340-19ae11b6b7d5/go.mod h1:PSkBLDXQW7NAhZ7JO1va7QJyTeDvpE7MEDnTdn5evRM= +github.com/splitio/go-split-commons/v5 v5.0.1-0.20231122221702-e3b2ef60041d h1:Xt94/BkqILwbU65m8yDauU7khOpeFBbzGXrS6CKgOfs= +github.com/splitio/go-split-commons/v5 v5.0.1-0.20231122221702-e3b2ef60041d/go.mod h1:PSkBLDXQW7NAhZ7JO1va7QJyTeDvpE7MEDnTdn5evRM= github.com/splitio/go-toolkit/v5 v5.3.2-0.20231106173125-49e72b9823dc h1:14jdJE/rBEYfs1CO8kOQrj/8azszRFU4yw5FQIGpoJg= github.com/splitio/go-toolkit/v5 v5.3.2-0.20231106173125-49e72b9823dc/go.mod h1:xYhUvV1gga9/1029Wbp5pjnR6Cy8nvBpjw99wAbsMko= github.com/twmb/murmur3 v1.1.6 h1:mqrRot1BRxm+Yct+vavLMou2/iJt0tNVTTC0QoIjaZg= diff --git a/splitio/client/input_validator_test.go b/splitio/client/input_validator_test.go index f6a5f99..9a4ad78 100644 --- a/splitio/client/input_validator_test.go +++ b/splitio/client/input_validator_test.go @@ -1,9 +1,14 @@ package client import ( + "compress/gzip" + "encoding/json" "fmt" + "io/ioutil" "math" "math/rand" + "net/http" + "net/http/httptest" "strings" "sync" "testing" @@ -518,6 +523,86 @@ func TestLocalhostTrafficType(t *testing.T) { mW.Reset() } +func TestInMemoryFactoryFlagSets(t *testing.T) { + var splitsMock, _ = ioutil.ReadFile("../../testdata/splits_mock.json") + var splitMock, _ = ioutil.ReadFile("../../testdata/split_mock.json") + + postChannel := make(chan string, 1) + + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + switch r.URL.Path { + case "/splitChanges": + if r.RequestURI != "/splitChanges?sets=a%2Cc%2Cd&since=-1" { + t.Error("wrong RequestURI for flag sets") + } + fmt.Fprintln(w, fmt.Sprintf(string(splitsMock), splitMock)) + return + case "/segmentChanges/___TEST___": + w.Header().Add("Content-Encoding", "gzip") + gzw := gzip.NewWriter(w) + defer gzw.Close() + fmt.Fprintln(gzw, "Hello, client") + return + case "/testImpressions/bulk": + case "/events/bulk": + for header := range r.Header { + if (header == "SplitSDKMachineIP") || (header == "SplitSDKMachineName") { + t.Error("Should not insert one of SplitSDKMachineIP, SplitSDKMachineName") + } + } + + rBody, _ := ioutil.ReadAll(r.Body) + var dataInPost []map[string]interface{} + err := json.Unmarshal(rBody, &dataInPost) + if err != nil { + t.Error(err) + return + } + + if len(dataInPost) < 1 { + t.Error("It should send data") + } + fmt.Fprintln(w, "ok") + postChannel <- "finished" + case "/segmentChanges": + default: + fmt.Fprintln(w, "ok") + return + } + })) + defer ts.Close() + cfg := conf.Default() + cfg.LabelsEnabled = true + cfg.IPAddressesEnabled = true + cfg.Advanced.EventsURL = ts.URL + cfg.Advanced.SdkURL = ts.URL + cfg.Advanced.TelemetryServiceURL = ts.URL + cfg.Advanced.AuthServiceURL = ts.URL + cfg.Advanced.ImpressionListener = &ImpressionListenerTest{} + cfg.TaskPeriods.ImpressionSync = 60 + cfg.TaskPeriods.EventsSync = 60 + cfg.Advanced.StreamingEnabled = false + cfg.Advanced.FlagSetFilter = []string{"a", "_b", "a", "a", "c", "d", "_d"} + + factory, _ := NewSplitFactory("test", cfg) + client := factory.Client() + errBlock := client.BlockUntilReady(10) + + if errBlock != nil { + t.Error("client should be ready") + } + + if !client.isReady() { + t.Error("InMemory should be ready") + } + + mW.Reset() + if mW.Length() > 0 { + t.Error("Wrong message") + } + mW.Reset() +} + func TestNotReadyYet(t *testing.T) { nonReadyUsages := 0 logger := getMockedLogger() From d308e41195d2f21f16d3a12ab66ef4c51508f9f6 Mon Sep 17 00:00:00 2001 From: Nadia Mayor Date: Mon, 27 Nov 2023 10:17:36 -0300 Subject: [PATCH 2/2] [SDKS-7687] Add test case in input_validator --- splitio/client/factory.go | 2 +- splitio/client/input_validator_test.go | 64 +++++++++++++++++++++++++- 2 files changed, 64 insertions(+), 2 deletions(-) diff --git a/splitio/client/factory.go b/splitio/client/factory.go index 54865d3..c387446 100644 --- a/splitio/client/factory.go +++ b/splitio/client/factory.go @@ -406,7 +406,7 @@ func setupRedisFactory(apikey string, cfg *conf.SplitSdkConfig, logger logging.L if len(cfg.Advanced.FlagSetFilter) != 0 { cfg.Advanced.FlagSetFilter = []string{} - logger.Debug("FlagSets filter is not applicable for Consumer modes where the SDK does not keep rollout data in sync. FlagSet filter was discarded") + logger.Warning("FlagSets filter is not applicable for Consumer modes where the SDK does not keep rollout data in sync. FlagSet filter was discarded") } flagSetFilter := flagsets.NewFlagSetFilter([]string{}) diff --git a/splitio/client/input_validator_test.go b/splitio/client/input_validator_test.go index 9a4ad78..3a47f7d 100644 --- a/splitio/client/input_validator_test.go +++ b/splitio/client/input_validator_test.go @@ -15,6 +15,7 @@ import ( "time" "github.com/splitio/go-client/v6/splitio/conf" + commonsCfg "github.com/splitio/go-split-commons/v5/conf" spConf "github.com/splitio/go-split-commons/v5/conf" "github.com/splitio/go-split-commons/v5/dtos" "github.com/splitio/go-split-commons/v5/flagsets" @@ -26,6 +27,7 @@ import ( "github.com/splitio/go-split-commons/v5/storage/inmemory/mutexmap" "github.com/splitio/go-split-commons/v5/storage/inmemory/mutexqueue" "github.com/splitio/go-split-commons/v5/storage/mocks" + "github.com/splitio/go-split-commons/v5/storage/redis" "github.com/splitio/go-split-commons/v5/synchronizer" "github.com/splitio/go-toolkit/v5/logging" ) @@ -565,6 +567,20 @@ func TestInMemoryFactoryFlagSets(t *testing.T) { fmt.Fprintln(w, "ok") postChannel <- "finished" case "/segmentChanges": + case "/metrics/config": + rBody, _ := ioutil.ReadAll(r.Body) + var dataInPost dtos.Config + err := json.Unmarshal(rBody, &dataInPost) + if err != nil { + t.Error(err) + return + } + if dataInPost.FlagSetsInvalid != 4 { + t.Error("invalid flag sets should be 4") + } + if dataInPost.FlagSetsTotal != 7 { + t.Error("total flag sets should be 7") + } default: fmt.Fprintln(w, "ok") return @@ -586,7 +602,7 @@ func TestInMemoryFactoryFlagSets(t *testing.T) { factory, _ := NewSplitFactory("test", cfg) client := factory.Client() - errBlock := client.BlockUntilReady(10) + errBlock := client.BlockUntilReady(15) if errBlock != nil { t.Error("client should be ready") @@ -601,6 +617,52 @@ func TestInMemoryFactoryFlagSets(t *testing.T) { t.Error("Wrong message") } mW.Reset() + + client.Destroy() +} + +func TestConsumerFactoryFlagSets(t *testing.T) { + logger := getMockedLogger() + sdkConf := conf.Default() + sdkConf.OperationMode = conf.RedisConsumer + sdkConf.Advanced.FlagSetFilter = []string{"a", "b"} + sdkConf.Logger = logger + + factory, _ := NewSplitFactory("something", sdkConf) + if !mW.Matches("FlagSets filter is not applicable for Consumer modes where the SDK does not keep rollout data in sync. FlagSet filter was discarded") { + t.Error("Wrong message") + } + if !factory.IsReady() { + t.Error("Factory should be ready immediately") + } + client := factory.Client() + if !client.factory.IsReady() { + t.Error("Client should be ready immediately") + } + + err := client.BlockUntilReady(1) + if err != nil { + t.Error("Error was not expected") + } + + manager := factory.Manager() + if !manager.factory.IsReady() { + t.Error("Manager should be ready immediately") + } + err = manager.BlockUntilReady(1) + if err != nil { + t.Error("Error was not expected") + } + + prefixedClient, _ := redis.NewRedisClient(&commonsCfg.RedisConfig{ + Host: "localhost", + Port: 6379, + Password: "", + Prefix: "", + }, logging.NewLogger(&logging.LoggerOptions{})) + deleteDataGenerated(prefixedClient) + + client.Destroy() } func TestNotReadyYet(t *testing.T) {