-
Notifications
You must be signed in to change notification settings - Fork 5
/
event_test.go
149 lines (129 loc) · 4.27 KB
/
event_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
package featureprobe
import (
"testing"
"time"
"github.com/jarcoal/httpmock"
"github.com/stretchr/testify/assert"
)
func TestEventFlush(t *testing.T) {
recorder := NewEventRecorder("https://featureprobe.com/api/events", 1000, "sdk_key")
versions := []uint64{1, 1, 1, 1, 2}
variations := []int{0, 0, 0, 1, 1}
trackAccessEvents := []bool{true, true, false, true, true}
keys := []string{"some_toggle", "some_toggle", "some_toggle", "some_toggle", "some_toggle2"}
for index, _ := range versions {
recorder.RecordAccess(AccessEvent{
Kind: "access",
Time: time.Now().Unix(),
User: "some_user",
Key: keys[index],
Value: "some_value",
VariationIndex: &variations[index],
Version: &versions[index],
}, trackAccessEvents[index])
}
recorder.RecordCustom(CustomEvent{
Kind: "custom",
Time: time.Now().Unix(),
User: "some_user",
Name: "some_event",
Value: nil,
})
assert.True(t, len(recorder.access.Counters) == 2)
assert.True(t, recorder.access.Counters["some_toggle"][0].Count == 3)
assert.True(t, len(recorder.access.Counters["some_toggle"]) == 2)
assert.True(t, len(recorder.incomingEvents) == 5)
httpmock.ActivateNonDefault(&recorder.httpClient)
httpmock.RegisterResponder("POST", "https://featureprobe.com/api/events",
httpmock.NewStringResponder(200, "{}"))
recorder.Start()
time.Sleep(2 * time.Second)
count := httpmock.GetTotalCallCount()
assert.True(t, count >= 1)
defer httpmock.DeactivateAndReset()
}
func TestEventFlushInvalidUrl(t *testing.T) {
version := uint64(1)
variationIndex := 0
recorder := NewEventRecorder(string([]byte{1, 2, 3}), 1000, "sdk_key")
recorder.RecordAccess(AccessEvent{
Kind: "access",
Time: time.Now().Unix(),
User: "some_user",
Key: "some_toggle",
Value: "some_value",
VariationIndex: &variationIndex,
Version: &version,
}, true)
recorder.RecordAccess(AccessEvent{
Kind: "access",
Time: time.Now().Unix(),
User: "some_user",
Key: "some_toggle",
Value: "some_value",
VariationIndex: &variationIndex,
Version: &version,
}, true)
httpmock.ActivateNonDefault(&recorder.httpClient)
httpmock.RegisterResponder("POST", "https://featureprobe.com/api/events",
httpmock.NewStringResponder(200, "{}"))
recorder.Start()
time.Sleep(2 * time.Second)
count := httpmock.GetTotalCallCount()
assert.Equal(t, 0, count)
defer httpmock.DeactivateAndReset()
}
func TestEventFlushInvalidResp(t *testing.T) {
version := uint64(1)
variationIndex := 0
recorder := NewEventRecorder("https://featureprobe.com/api/events", 1000, "sdk_key")
recorder.RecordAccess(AccessEvent{
Kind: "access",
Time: time.Now().Unix(),
User: "some_user",
Key: "some_toggle",
Value: "some_value",
VariationIndex: &variationIndex,
Version: &version,
}, true)
recorder.RecordAccess(AccessEvent{
Kind: "access",
Time: time.Now().Unix(),
User: "some_user",
Key: "some_toggle",
Value: "some_value",
VariationIndex: &variationIndex,
Version: &version,
}, true)
httpmock.ActivateNonDefault(&recorder.httpClient)
httpmock.RegisterResponder("POST", "https://featureprobe.com/api/events",
httpmock.NewStringResponder(200, "{"))
recorder.Start()
time.Sleep(2 * time.Second)
count := httpmock.GetTotalCallCount()
assert.True(t, count > 0)
defer httpmock.DeactivateAndReset()
}
func TestCloseEvent(t *testing.T) {
version := uint64(1)
variationIndex := 0
recorder := NewEventRecorder("https://featureprobe.com/api/events", 1000, "sdk_key")
recorder.RecordAccess(AccessEvent{
Kind: "access",
Time: time.Now().Unix(),
User: "some_user",
Key: "some_toggle",
Value: "some_value",
VariationIndex: &variationIndex,
Version: &version,
}, true)
httpmock.ActivateNonDefault(&recorder.httpClient)
httpmock.RegisterResponder("POST", "https://featureprobe.com/api/events",
httpmock.NewStringResponder(200, "{}"))
recorder.Start()
time.Sleep(2 * time.Second)
recorder.Stop()
count := httpmock.GetTotalCallCount()
assert.Equal(t, 1, count)
defer httpmock.DeactivateAndReset()
}