forked from launchdarkly/go-server-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ldclient_events_test.go
212 lines (175 loc) · 7.16 KB
/
ldclient_events_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package ldclient
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gopkg.in/launchdarkly/go-sdk-common.v2/ldlog"
"gopkg.in/launchdarkly/go-sdk-common.v2/ldlogtest"
"gopkg.in/launchdarkly/go-sdk-common.v2/lduser"
"gopkg.in/launchdarkly/go-sdk-common.v2/ldvalue"
ldevents "gopkg.in/launchdarkly/go-sdk-events.v1"
"gopkg.in/launchdarkly/go-server-sdk.v5/interfaces"
"gopkg.in/launchdarkly/go-server-sdk.v5/internal/sharedtest"
"gopkg.in/launchdarkly/go-server-sdk.v5/ldcomponents"
)
func TestIdentifySendsIdentifyEvent(t *testing.T) {
client := makeTestClient()
defer client.Close()
user := lduser.NewUser("userKey")
err := client.Identify(user)
assert.NoError(t, err)
events := client.eventProcessor.(*sharedtest.CapturingEventProcessor).Events
assert.Equal(t, 1, len(events))
e := events[0].(ldevents.IdentifyEvent)
assert.Equal(t, ldevents.User(user), e.User)
}
func TestIdentifyWithEmptyUserKeySendsNoEvent(t *testing.T) {
client := makeTestClient()
defer client.Close()
err := client.Identify(lduser.NewUser(""))
assert.NoError(t, err) // we don't return an error for this, we just log it
events := client.eventProcessor.(*sharedtest.CapturingEventProcessor).Events
assert.Equal(t, 0, len(events))
}
func TestTrackEventSendsCustomEvent(t *testing.T) {
client := makeTestClient()
defer client.Close()
user := lduser.NewUser("userKey")
key := "eventKey"
err := client.TrackEvent(key, user)
assert.NoError(t, err)
events := client.eventProcessor.(*sharedtest.CapturingEventProcessor).Events
assert.Equal(t, 1, len(events))
e := events[0].(ldevents.CustomEvent)
assert.Equal(t, ldevents.User(user), e.User)
assert.Equal(t, key, e.Key)
assert.Equal(t, ldvalue.Null(), e.Data)
assert.False(t, e.HasMetric)
}
func TestTrackDataSendsCustomEventWithData(t *testing.T) {
client := makeTestClient()
defer client.Close()
user := lduser.NewUser("userKey")
key := "eventKey"
data := ldvalue.ArrayOf(ldvalue.String("a"), ldvalue.String("b"))
err := client.TrackData(key, user, data)
assert.NoError(t, err)
events := client.eventProcessor.(*sharedtest.CapturingEventProcessor).Events
assert.Equal(t, 1, len(events))
e := events[0].(ldevents.CustomEvent)
assert.Equal(t, ldevents.User(user), e.User)
assert.Equal(t, key, e.Key)
assert.Equal(t, data, e.Data)
assert.False(t, e.HasMetric)
}
func TestTrackMetricSendsCustomEventWithMetricAndData(t *testing.T) {
client := makeTestClient()
defer client.Close()
user := lduser.NewUser("userKey")
key := "eventKey"
data := ldvalue.ArrayOf(ldvalue.String("a"), ldvalue.String("b"))
metric := float64(1.5)
err := client.TrackMetric(key, user, metric, data)
assert.NoError(t, err)
events := client.eventProcessor.(*sharedtest.CapturingEventProcessor).Events
assert.Equal(t, 1, len(events))
e := events[0].(ldevents.CustomEvent)
assert.Equal(t, ldevents.User(user), e.User)
assert.Equal(t, key, e.Key)
assert.Equal(t, data, e.Data)
assert.True(t, e.HasMetric)
assert.Equal(t, metric, e.MetricValue)
}
func TestTrackWithEmptyUserKeySendsNoEvent(t *testing.T) {
client := makeTestClient()
defer client.Close()
err := client.TrackEvent("eventkey", lduser.NewUser(""))
assert.NoError(t, err) // we don't return an error for this, we just log it
events := client.eventProcessor.(*sharedtest.CapturingEventProcessor).Events
assert.Equal(t, 0, len(events))
}
func TestTrackMetricWithEmptyUserKeySendsNoEvent(t *testing.T) {
client := makeTestClient()
defer client.Close()
err := client.TrackMetric("eventKey", lduser.NewUser(""), 2.5, ldvalue.Null())
assert.NoError(t, err) // we don't return an error for this, we just log it
events := client.eventProcessor.(*sharedtest.CapturingEventProcessor).Events
assert.Equal(t, 0, len(events))
}
func TestIdentifyWithEventsDisabledDoesNotCauseError(t *testing.T) {
mockLog := ldlogtest.NewMockLog()
client := makeTestClientWithConfig(func(c *Config) {
c.Events = ldcomponents.NoEvents()
c.Logging = ldcomponents.Logging().Loggers(mockLog.Loggers)
})
defer client.Close()
require.NoError(t, client.Identify(lduser.NewUser("")))
assert.Len(t, mockLog.GetOutput(ldlog.Warn), 0)
}
func TestTrackWithEventsDisabledDoesNotCauseError(t *testing.T) {
mockLog := ldlogtest.NewMockLog()
client := makeTestClientWithConfig(func(c *Config) {
c.Events = ldcomponents.NoEvents()
c.Logging = ldcomponents.Logging().Loggers(mockLog.Loggers)
})
defer client.Close()
require.NoError(t, client.TrackEvent("eventkey", lduser.NewUser("")))
require.NoError(t, client.TrackMetric("eventkey", lduser.NewUser(""), 0, ldvalue.Null()))
assert.Len(t, mockLog.GetOutput(ldlog.Warn), 0)
}
func TestWithEventsDisabledDecorator(t *testing.T) {
doTest := func(name string, fn func(*LDClient) interfaces.LDClientInterface, shouldBeSent bool) {
t.Run(name, func(t *testing.T) {
events := &sharedtest.CapturingEventProcessor{}
config := Config{
DataSource: ldcomponents.ExternalUpdatesOnly(),
Events: sharedtest.SingleEventProcessorFactory{Instance: events},
}
client, err := MakeCustomClient("", config, 0)
require.NoError(t, err)
ci := fn(client)
checkEvents := func(action func()) {
events.Events = nil
action()
if shouldBeSent {
assert.Len(t, events.Events, 1, "should have recorded an event, but did not")
} else {
assert.Len(t, events.Events, 0, "should not have recorded an event, but did")
}
}
user := lduser.NewUser("userkey")
checkEvents(func() { _, _ = ci.BoolVariation("flagkey", user, false) })
checkEvents(func() { _, _, _ = ci.BoolVariationDetail("flagkey", user, false) })
checkEvents(func() { _, _ = ci.IntVariation("flagkey", user, 0) })
checkEvents(func() { _, _, _ = ci.IntVariationDetail("flagkey", user, 0) })
checkEvents(func() { _, _ = ci.Float64Variation("flagkey", user, 0) })
checkEvents(func() { _, _, _ = ci.Float64VariationDetail("flagkey", user, 0) })
checkEvents(func() { _, _ = ci.StringVariation("flagkey", user, "") })
checkEvents(func() { _, _, _ = ci.StringVariationDetail("flagkey", user, "") })
checkEvents(func() { _, _ = ci.JSONVariation("flagkey", user, ldvalue.Null()) })
checkEvents(func() { _, _, _ = ci.JSONVariationDetail("flagkey", user, ldvalue.Null()) })
checkEvents(func() { ci.Identify(user) })
checkEvents(func() { ci.TrackEvent("eventkey", user) })
checkEvents(func() { ci.TrackData("eventkey", user, ldvalue.Bool(true)) })
checkEvents(func() { ci.TrackMetric("eventkey", user, 1.5, ldvalue.Null()) })
state := ci.AllFlagsState(user)
assert.True(t, state.IsValid())
})
}
doTest("client.WithEventsDisabled(false)",
func(c *LDClient) interfaces.LDClientInterface { return c.WithEventsDisabled(false) },
true)
doTest("client.WithEventsDisabled(true)",
func(c *LDClient) interfaces.LDClientInterface { return c.WithEventsDisabled(true) },
false)
doTest("client.WithEventsDisabled(true).WithEventsDisabled(false)",
func(c *LDClient) interfaces.LDClientInterface {
return c.WithEventsDisabled(true).WithEventsDisabled(false)
},
true)
doTest("client.WithEventsDisabled(true).WithEventsDisabled(true)",
func(c *LDClient) interfaces.LDClientInterface {
return c.WithEventsDisabled(true).WithEventsDisabled(true)
},
false)
}