Skip to content

Commit 88c94f6

Browse files
committed
fixed tests
1 parent 6d2fc26 commit 88c94f6

File tree

8 files changed

+301
-598
lines changed

8 files changed

+301
-598
lines changed

go.mod

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ module github.com/splitio/go-client/v6
33
go 1.18
44

55
require (
6-
github.com/splitio/go-split-commons/v7 v7.0.0
7-
github.com/splitio/go-toolkit/v5 v5.4.0
8-
6+
github.com/splitio/go-split-commons/v7 v7.0.1-0.20250930213118-b0b22c397fc4
7+
github.com/splitio/go-toolkit/v5 v5.4.1-0.20250930172659-38274b802d99
8+
github.com/stretchr/testify v1.11.1
99
)
1010

1111
require (
@@ -18,7 +18,6 @@ require (
1818
github.com/pmezard/go-difflib v1.0.0 // indirect
1919
github.com/redis/go-redis/v9 v9.0.4 // indirect
2020
github.com/stretchr/objx v0.5.2 // indirect
21-
github.com/stretchr/testify v1.9.0 // indirect
2221
golang.org/x/exp v0.0.0-20231006140011-7918f672742d // indirect
2322
golang.org/x/sync v0.3.0 // indirect
2423
gopkg.in/yaml.v3 v3.0.1 // indirect

go.sum

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
1818
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
1919
github.com/redis/go-redis/v9 v9.0.4 h1:FC82T+CHJ/Q/PdyLW++GeCO+Ol59Y4T7R4jbgjvktgc=
2020
github.com/redis/go-redis/v9 v9.0.4/go.mod h1:WqMKv5vnQbRuZstUwxQI195wHy+t4PuXDOjzMvcuQHk=
21-
github.com/splitio/go-split-commons/v7 v7.0.0 h1:AP3KBuOYd8hQhNOrOWGDYXFwS1cM52zfC4eBSbwy0HU=
22-
github.com/splitio/go-split-commons/v7 v7.0.0/go.mod h1:7GiUZ/m6r2h4l8xz4d924FXfs8gV3VR6LWrOHILp77I=
23-
github.com/splitio/go-toolkit/v5 v5.4.0 h1:g5WFpRhQomnXCmvfsNOWV4s5AuUrWIZ+amM68G8NBKM=
24-
github.com/splitio/go-toolkit/v5 v5.4.0/go.mod h1:xYhUvV1gga9/1029Wbp5pjnR6Cy8nvBpjw99wAbsMko=
21+
github.com/splitio/go-split-commons/v7 v7.0.1-0.20250930213118-b0b22c397fc4 h1:OK9LLRmEpLghXM5paCrR9zFXTuYTdoiP2P2apwW3C9E=
22+
github.com/splitio/go-split-commons/v7 v7.0.1-0.20250930213118-b0b22c397fc4/go.mod h1:Lsj2n1zm88laFRu+JhlNeXW0x1ndtjQ1H21rLhRFfOs=
23+
github.com/splitio/go-toolkit/v5 v5.4.1-0.20250930172659-38274b802d99 h1:rQo355F9JbdyTMz2X5MU+FeRvkT6rvD1n+GnXdJr33A=
24+
github.com/splitio/go-toolkit/v5 v5.4.1-0.20250930172659-38274b802d99/go.mod h1:SifzysrOVDbzMcOE8zjX02+FG5az4FrR3Us/i5SeStw=
2525
github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
2626
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
27-
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
28-
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
27+
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
28+
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
2929
github.com/twmb/murmur3 v1.1.6 h1:mqrRot1BRxm+Yct+vavLMou2/iJt0tNVTTC0QoIjaZg=
3030
github.com/twmb/murmur3 v1.1.6/go.mod h1:Qq/R7NUyOfr65zD+6Q5IHKsJLwP7exErjN6lyyq3OSQ=
3131
golang.org/x/exp v0.0.0-20231006140011-7918f672742d h1:jtJma62tbqLibJ5sFQz8bKtEM8rJBtfilJ2qTU199MI=

splitio/client/client.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"errors"
55
"fmt"
66
"runtime/debug"
7+
"sort"
78
"strings"
89
"time"
910

@@ -74,6 +75,7 @@ func (c *SplitClient) getEvaluationsResult(matchingKey string, bucketingKey *str
7475
if c.isReady() {
7576
return c.evaluator.EvaluateFeatures(matchingKey, bucketingKey, featureFlags, attributes)
7677
}
78+
sort.Strings(featureFlags)
7779
featureFlagsToPrint := strings.Join(featureFlags, ", ")
7880
c.logger.Warning(fmt.Sprintf("%s: the SDK is not ready, results may be incorrect for feature flags %s. Make sure to wait for SDK readiness before using this method", operation, featureFlagsToPrint))
7981
c.initTelemetry.RecordNonReadyUsage()

splitio/client/factory_test.go

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,27 @@ import (
44
"testing"
55

66
"github.com/splitio/go-split-commons/v7/flagsets"
7+
"github.com/splitio/go-toolkit/v5/logging/mocks"
8+
9+
"github.com/stretchr/testify/assert"
710
)
811

912
func TestPrintWarnings(t *testing.T) {
10-
1113
flagSets, warnings := flagsets.SanitizeMany([]string{"set1", " set2"})
12-
if len(flagSets) != 2 {
13-
t.Error("flag set size should be 2")
14-
}
15-
printWarnings(getMockedLogger(), warnings)
16-
if !mW.Matches("Flag Set name set2 has extra whitespace, trimming") {
17-
t.Error("Wrong message")
18-
}
14+
assert.Len(t, flagSets, 2)
15+
logger := &mocks.LoggerMock{}
16+
logger.On("Warning", []interface{}{"Flag Set name set2 has extra whitespace, trimming"}).Return().Once()
17+
printWarnings(logger, warnings)
18+
1919
flagSets, warnings = flagsets.SanitizeMany([]string{"set1", "Set2"})
20-
if len(flagSets) != 2 {
21-
t.Error("flag set size should be 2")
22-
}
23-
printWarnings(getMockedLogger(), warnings)
24-
if !mW.Matches("Flag Set name Set2 should be all lowercase - converting string to lowercase") {
25-
t.Error("Wrong message")
26-
}
20+
assert.Len(t, flagSets, 2)
21+
logger.On("Warning", []interface{}{"Flag Set name Set2 should be all lowercase - converting string to lowercase"}).Return().Once()
22+
printWarnings(logger, warnings)
23+
2724
flagSets, warnings = flagsets.SanitizeMany([]string{"set1", "@set4"})
28-
if len(flagSets) != 1 {
29-
t.Error("flag set size should be 1")
30-
}
31-
printWarnings(getMockedLogger(), warnings)
32-
if !mW.Matches("you passed @set4, Flag Set must adhere to the regular expressions ^[a-z0-9][_a-z0-9]{0,49}$. This means a Flag Set must start with a letter or number, be in lowercase, alphanumeric and have a max length of 50 characters. @set4 was discarded.") {
33-
t.Error("Wrong message")
34-
}
25+
assert.Len(t, flagSets, 1)
26+
logger.On("Warning", []interface{}{"you passed @set4, Flag Set must adhere to the regular expressions ^[a-z0-9][_a-z0-9]{0,49}$. This means a Flag Set must start with a letter or number, be in lowercase, alphanumeric and have a max length of 50 characters. @set4 was discarded."}).Return().Once()
27+
printWarnings(logger, warnings)
28+
29+
logger.AssertExpectations(t)
3530
}

splitio/client/factory_tracker_test.go

Lines changed: 21 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -4,98 +4,57 @@ import (
44
"testing"
55

66
"github.com/splitio/go-client/v6/splitio/conf"
7-
"github.com/splitio/go-toolkit/v5/logging"
7+
8+
"github.com/splitio/go-toolkit/v5/logging/mocks"
9+
10+
"github.com/stretchr/testify/assert"
11+
"github.com/stretchr/testify/mock"
812
)
913

1014
func TestFactoryTrackerMultipleInstantiation(t *testing.T) {
11-
var mockWriter MockWriter
15+
logger := &mocks.LoggerMock{}
16+
logger.On("Info", mock.Anything).Return()
17+
logger.On("Debug", mock.Anything).Return()
1218
sdkConf := conf.Default()
13-
sdkConf.Logger = logging.NewLogger(&logging.LoggerOptions{
14-
LogLevel: logging.LevelAll,
15-
ErrorWriter: &mockWriter,
16-
WarningWriter: &mockWriter,
17-
InfoWriter: &mockWriter,
18-
DebugWriter: &mockWriter,
19-
VerboseWriter: &mockWriter,
20-
})
19+
sdkConf.Logger = logger
2120
sdkConf.SplitFile = "../../testdata/splits.yaml"
2221

2322
removeInstanceFromTracker(conf.Localhost)
2423
removeInstanceFromTracker("something")
2524

2625
factory, _ := NewSplitFactory(conf.Localhost, sdkConf)
2726
client := factory.Client()
27+
assert.Equal(t, int64(1), factoryInstances[conf.Localhost])
2828

29-
if factoryInstances[conf.Localhost] != 1 {
30-
t.Error("It should be 1")
31-
}
32-
29+
logger.On("Warning", []interface{}{"Factory Instantiation: You already have 1 factory with this SDK Key. We recommend keeping only one instance of the factory at all times (Singleton pattern) and reusing it throughout your application."}).Return().Once()
3330
factory2, _ := NewSplitFactory(conf.Localhost, sdkConf)
3431
_ = factory2.Client()
32+
assert.Equal(t, int64(2), factoryInstances[conf.Localhost])
3533

36-
if factoryInstances[conf.Localhost] != 2 {
37-
t.Error("It should be 2")
38-
}
39-
expected := "Factory Instantiation: You already have 1 factory with this SDK Key. We recommend keeping only one " +
40-
"instance of the factory at all times (Singleton pattern) and reusing it throughout your application."
41-
if !mockWriter.Matches(expected) {
42-
t.Error("Error is distinct from the expected one")
43-
}
44-
34+
logger.On("Warning", []interface{}{"Factory Instantiation: You already have an instance of the Split factory. Make sure you definitely want this additional instance. We recommend keeping only one instance of the factory at all times (Singleton pattern) and reusing it throughout your application."}).Return().Once()
4535
factory4, _ := NewSplitFactory("asdadd", sdkConf)
4636
client2 := factory4.Client()
47-
expected = "Factory Instantiation: You already have an instance of the Split factory. Make sure you definitely want " +
48-
"this additional instance. We recommend keeping only one instance of the factory at all times (Singleton pattern) and " +
49-
"reusing it throughout your application."
50-
if !mockWriter.Matches(expected) {
51-
t.Error("Error is distinct from the expected one")
52-
}
53-
5437
client.Destroy()
5538

56-
if factoryInstances[conf.Localhost] != 1 {
57-
t.Error("It should be 1")
58-
}
59-
60-
if factoryInstances["asdadd"] != 1 {
61-
t.Error("It should be 1")
62-
}
63-
39+
assert.Equal(t, int64(1), factoryInstances[conf.Localhost])
40+
assert.Equal(t, int64(1), factoryInstances["asdadd"])
6441
client.Destroy()
6542

66-
if factoryInstances[conf.Localhost] != 1 {
67-
t.Error("It should be 1")
68-
}
69-
43+
assert.Equal(t, int64(1), factoryInstances[conf.Localhost])
7044
client2.Destroy()
7145

7246
_, exist := factoryInstances["asdadd"]
73-
if exist {
74-
t.Error("It should not exist")
75-
}
47+
assert.False(t, exist)
7648

49+
logger.On("Warning", []interface{}{"Factory Instantiation: You already have 1 factory with this SDK Key. We recommend keeping only one instance of the factory at all times (Singleton pattern) and reusing it throughout your application."}).Return().Once()
7750
factory3, _ := NewSplitFactory(conf.Localhost, sdkConf)
7851
_ = factory3.Client()
79-
expected = "Factory Instantiation: You already have 1 factory with this SDK Key. We recommend keeping only one " +
80-
"instance of the factory at all times (Singleton pattern) and reusing it throughout your application."
81-
if !mockWriter.Matches(expected) {
82-
t.Error("Error is distinct from the expected one")
83-
}
84-
85-
if factoryInstances[conf.Localhost] != 2 {
86-
t.Error("It should be 2")
87-
}
52+
assert.Equal(t, int64(2), factoryInstances[conf.Localhost])
8853

54+
logger.On("Warning", []interface{}{"Factory Instantiation: You already have 2 factories with this SDK Key. We recommend keeping only one instance of the factory at all times (Singleton pattern) and reusing it throughout your application."}).Return().Once()
8955
factory5, _ := NewSplitFactory(conf.Localhost, sdkConf)
9056
_ = factory5.Client()
91-
expected = "Factory Instantiation: You already have 2 factories with this SDK Key. We recommend keeping only one " +
92-
"instance of the factory at all times (Singleton pattern) and reusing it throughout your application."
93-
if !mockWriter.Matches(expected) {
94-
t.Error("Error is distinct from the expected one")
95-
}
96-
if factoryInstances[conf.Localhost] != 3 {
97-
t.Error("It should be 3")
98-
}
57+
assert.Equal(t, int64(3), factoryInstances[conf.Localhost])
9958

10059
removeInstanceFromTracker(conf.Localhost)
10160
removeInstanceFromTracker("asdadd")

splitio/client/input_validator.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ func (i *inputValidation) ValidateTreatmentKey(key interface{}, operation string
120120
if err != nil {
121121
return "", nil, err
122122
}
123-
i.logger.Warning(fmt.Sprintf(operation+": key %s is not of type string, converting", key))
123+
i.logger.Warning(fmt.Sprintf(operation+": key %v is not of type string, converting", key))
124124
}
125125
err = checkIsValidString(sMatchingKey, "key", "key", operation)
126126
if err != nil {
@@ -270,15 +270,15 @@ func (i *inputValidation) validateTrackProperties(properties map[string]interfac
270270
size += len(value)
271271
processed[name] = value
272272
default:
273-
i.logger.Warning("Property %s is of invalid type. Setting value to nil")
273+
i.logger.Warning("Track: Property %s is of invalid type. Setting value to nil")
274274
processed[name] = nil
275275
}
276276

277277
if size > MaxEventLength {
278278
i.logger.Error(
279-
"The maximum size allowed for the properties is 32kb. Event not queued",
279+
"Track: The maximum size allowed for the properties is 32kb. Event not queued",
280280
)
281-
return nil, size, errors.New("The maximum size allowed for the properties is 32kb. Event not queued")
281+
return nil, size, errors.New("Track: The maximum size allowed for the properties is 32kb. Event not queued")
282282
}
283283
}
284284
return processed, size, nil

0 commit comments

Comments
 (0)