Skip to content
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

[SDKS-7687] Add integration tests #209

Merged
merged 3 commits into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion splitio/client/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{})

Expand Down
147 changes: 147 additions & 0 deletions splitio/client/input_validator_test.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
package client

import (
"compress/gzip"
"encoding/json"
"fmt"
"io/ioutil"
"math"
"math/rand"
"net/http"
"net/http/httptest"
"strings"
"sync"
"testing"
"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"
Expand All @@ -21,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"
)
Expand Down Expand Up @@ -518,6 +525,146 @@ 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":
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
}
}))
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(15)

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

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) {
nonReadyUsages := 0
logger := getMockedLogger()
Expand Down