-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathevent_manager_test.go
More file actions
236 lines (212 loc) · 6.96 KB
/
event_manager_test.go
File metadata and controls
236 lines (212 loc) · 6.96 KB
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package devcycle
import (
"encoding/json"
"fmt"
"github.com/devcyclehq/go-server-sdk/v2/api"
"io"
"log"
"net/http"
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/jarcoal/httpmock"
)
func TestEventManager_QueueEvent(t *testing.T) {
sdkKey, _ := httpConfigMock(200)
c, err := NewClient(sdkKey, &Options{})
require.NoError(t, err)
defer func(c *Client) {
_ = c.Close()
}(c)
_, err = c.Track(User{UserId: "j_test", DeviceModel: "testing"},
Event{Target: "customevent", Type_: "event"})
if err != nil {
t.Fatal(err)
}
}
func TestEventManager_QueueEvent_100_DropEvent(t *testing.T) {
sdkKey, _ := httpConfigMock(200)
c, err := NewClient(sdkKey, &Options{MaxEventQueueSize: 100, FlushEventQueueSize: 10})
require.NoError(t, err)
defer func(c *Client) {
_ = c.Close()
}(c)
errored := false
for i := 0; i < 1000; i++ {
log.Println(i)
_, err = c.Track(User{UserId: "j_test", DeviceModel: "testing"},
Event{Target: "customevent"})
if err != nil {
errored = true
log.Println(err)
break
}
}
if !errored {
t.Fatal("Did not get dropped event warning.")
}
}
func TestEventManager_AggVariableEvaluatedReason(t *testing.T) {
sdkKey := generateTestSDKKey()
httpCustomConfigMock(sdkKey, 200, test_large_config, false)
user := User{UserId: "dontcare", DeviceModel: "testing", CustomData: map[string]interface{}{"data-key-7": "3yejExtXkma4"}}
evalReasons := map[string]float64{}
httpEventsApiMockWithCallback(func(req *http.Request) (*http.Response, error) {
resp := httpmock.NewStringResponse(201, `{}`)
var body BatchEventsBody
rawBody, _ := io.ReadAll(req.Body)
err := json.Unmarshal(rawBody, &body)
for _, b := range body.Batch {
for _, e := range b.Events {
if e.Type_ == api.EventType_AggVariableEvaluated {
if val, ok := e.MetaData["eval"]; ok {
if eval, ok := val.(map[string]interface{}); ok {
for k, v := range eval {
if count, ok := v.(float64); ok {
evalReasons[k] += count
fmt.Printf("%s - Eval count: %f, Total Count: %f\n", k, count, evalReasons[k])
} else {
t.Errorf("Expected eval count to be a float, got %T", v)
}
}
} else {
t.Errorf("Expected eval reason to be a map, got %T", val)
}
} else {
t.Error("Expected eval reason to be present in event metadata")
}
}
}
}
if err != nil {
t.Fatalf("Error unmarshalling request body: %v", err)
}
return resp, nil
})
c, err := NewClient(sdkKey, &Options{
MaxEventQueueSize: 100,
FlushEventQueueSize: 10,
ConfigPollingIntervalMS: time.Second,
EventFlushIntervalMS: time.Second,
})
require.NoError(t, err)
defer func(c *Client) {
_ = c.Close()
}(c)
require.Eventually(t, func() bool {
return c.isInitialized && c.hasConfig()
}, 1*time.Second, 100*time.Millisecond)
for i := 0; i < 2*c.DevCycleOptions.FlushEventQueueSize; i++ {
_, _ = c.Variable(user, "v-key-76", 69)
}
_ = c.FlushEvents()
require.Eventually(t, func() bool {
fmt.Println(httpmock.GetCallCountInfo())
evalReasonsUpdated := len(evalReasons) > 0
return httpmock.GetCallCountInfo()["POST https://events.devcycle.com/v1/events/batch"] > 0 && evalReasonsUpdated
}, 3*time.Second, 100*time.Millisecond)
}
func TestEventManager_AggVariableDefaultedReason(t *testing.T) {
sdkKey := generateTestSDKKey()
httpCustomConfigMock(sdkKey, 200, test_large_config, false)
user := User{UserId: "dontcare", DeviceModel: "testing", CustomData: map[string]interface{}{"data-key-7": "3yejExtXkma4"}}
defaultReasons := map[string]float64{}
httpEventsApiMockWithCallback(func(req *http.Request) (*http.Response, error) {
resp := httpmock.NewStringResponse(201, `{}`)
var body BatchEventsBody
rawBody, _ := io.ReadAll(req.Body)
err := json.Unmarshal(rawBody, &body)
for _, b := range body.Batch {
for _, e := range b.Events {
if e.Type_ == api.EventType_AggVariableDefaulted {
if val, ok := e.MetaData["eval"]; ok {
if eval, ok := val.(map[string]interface{}); ok {
for k, v := range eval {
if count, ok := v.(float64); ok {
defaultReasons[k] += count
fmt.Printf("%s - Default count: %f, Total Count: %f\n", k, count, defaultReasons[k])
} else {
t.Errorf("Expected default count to be a float, got %T", v)
}
}
} else {
t.Errorf("Expected default reason to be a map, got %T", val)
fmt.Println(val)
}
} else {
t.Error("Expected eval reason to be present in event metadata")
}
}
}
}
if err != nil {
t.Fatalf("Error unmarshalling request body: %v", err)
}
return resp, nil
})
c, err := NewClient(sdkKey, &Options{
MaxEventQueueSize: 100,
FlushEventQueueSize: 10,
ConfigPollingIntervalMS: time.Second,
EventFlushIntervalMS: time.Second,
})
require.NoError(t, err)
defer func(c *Client) {
_ = c.Close()
}(c)
require.Eventually(t, func() bool {
return c.isInitialized && c.hasConfig()
}, 1*time.Second, 100*time.Millisecond)
for i := 0; i < 10; i++ {
_, _ = c.Variable(user, "variable-doesn't-exist", 69)
}
_ = c.FlushEvents()
require.Eventually(t, func() bool {
fmt.Println(httpmock.GetCallCountInfo())
evalReasonsUpdated := len(defaultReasons) > 0
return httpmock.GetCallCountInfo()["POST https://events.devcycle.com/v1/events/batch"] > 0 && evalReasonsUpdated
}, 3*time.Second, 100*time.Millisecond)
}
func TestEventManager_QueueEvent_100_Flush(t *testing.T) {
sdkKey, _ := httpConfigMock(200)
c, err := NewClient(sdkKey, &Options{
MaxEventQueueSize: 100,
FlushEventQueueSize: 10,
ConfigPollingIntervalMS: time.Second,
EventFlushIntervalMS: time.Second,
})
require.NoError(t, err)
defer func(c *Client) {
_ = c.Close()
}(c)
require.Eventually(t, func() bool {
return c.isInitialized && c.hasConfig()
}, 1*time.Second, 100*time.Millisecond)
// Track up to FlushEventQueueSize events
for i := 0; i < c.DevCycleOptions.FlushEventQueueSize; i++ {
_, err = c.Track(User{UserId: "j_test", DeviceModel: "testing"},
Event{Target: "customevent", Type_: "event"})
if err != nil {
t.Fatalf("Error tracking event: %v", err)
}
}
// Wait for raw event queue to drain
require.Eventually(t, func() bool {
queueLength, _ := c.eventQueue.internalQueue.UserQueueLength()
return queueLength >= 10
}, 1*time.Second, 100*time.Millisecond)
// Track one more event to trigger an automatic flush
_, err = c.Track(User{UserId: "j_test", DeviceModel: "testing"}, Event{Target: "customevent", Type_: "event"})
if err != nil {
t.Fatalf("Error tracking event: %v", err)
}
// Wait for raw event queue to drain
require.Eventually(t, func() bool {
flushed, reported, dropped := c.eventQueue.Metrics()
return flushed == 1 && reported == 1 && dropped == 0
}, 1*time.Second, 100*time.Millisecond)
require.Eventually(t, func() bool {
fmt.Println(httpmock.GetCallCountInfo())
return httpmock.GetCallCountInfo()["POST https://events.devcycle.com/v1/events/batch"] >= 1
}, 1*time.Second, 100*time.Millisecond)
}