-
Notifications
You must be signed in to change notification settings - Fork 327
/
Copy pathtilt_analytics_test.go
228 lines (205 loc) · 5.74 KB
/
tilt_analytics_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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package analytics
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/windmilleng/wmclient/pkg/analytics"
)
type testCase struct {
name string
opt analytics.Opt
expectRecord bool
}
var testTags = map[string]string{"bar": "baz"}
func testCases(expectedWhenOptedIn, expectedWhenOptedOut, expectedWhenNoOpt bool) []testCase {
return []testCase{
{"opt in", analytics.OptIn, expectedWhenOptedIn},
{"opt out", analytics.OptOut, expectedWhenOptedOut},
{"opt default", analytics.OptDefault, expectedWhenNoOpt},
}
}
type optSetting struct {
opt analytics.Opt
}
func (os *optSetting) SetOpt(opt analytics.Opt) error {
os.opt = opt
return nil
}
func TestCount(t *testing.T) {
for _, test := range testCases(true, false, false) {
t.Run(test.name, func(t *testing.T) {
ma := analytics.NewMemoryAnalytics()
os := &optSetting{}
a := NewTiltAnalytics(test.opt, os, ma)
a.Count("foo", testTags, 1)
var expectedCounts []analytics.CountEvent
if test.expectRecord {
expectedCounts = append(expectedCounts, analytics.CountEvent{
Name: "foo",
Tags: testTags,
N: 1,
})
}
assert.Equal(t, expectedCounts, ma.Counts)
})
}
}
func TestIncr(t *testing.T) {
for _, test := range testCases(true, false, false) {
t.Run(test.name, func(t *testing.T) {
ma := analytics.NewMemoryAnalytics()
os := &optSetting{}
a := NewTiltAnalytics(test.opt, os, ma)
a.Incr("foo", testTags)
var expectedCounts []analytics.CountEvent
if test.expectRecord {
expectedCounts = append(expectedCounts, analytics.CountEvent{
Name: "foo",
Tags: testTags,
N: 1,
})
}
assert.Equal(t, expectedCounts, ma.Counts)
})
}
}
func TestTimer(t *testing.T) {
for _, test := range testCases(true, false, false) {
t.Run(test.name, func(t *testing.T) {
ma := analytics.NewMemoryAnalytics()
os := &optSetting{}
a := NewTiltAnalytics(test.opt, os, ma)
a.Timer("foo", time.Second, testTags)
var expectedTimes []analytics.TimeEvent
if test.expectRecord {
expectedTimes = append(expectedTimes, analytics.TimeEvent{
Name: "foo",
Tags: testTags,
Dur: time.Second,
})
}
assert.Equal(t, expectedTimes, ma.Timers)
})
}
}
func TestIncrIfUnopted(t *testing.T) {
for _, test := range testCases(false, false, true) {
t.Run(test.name, func(t *testing.T) {
ma := analytics.NewMemoryAnalytics()
os := &optSetting{}
a := NewTiltAnalytics(test.opt, os, ma)
a.IncrIfUnopted("foo")
var expectedCounts []analytics.CountEvent
if test.expectRecord {
expectedCounts = append(expectedCounts, analytics.CountEvent{
Name: "foo",
Tags: map[string]string{},
N: 1,
})
}
assert.Equal(t, expectedCounts, ma.Counts)
})
}
}
func analyticsViaTransition(t *testing.T, initialOpt, newOpt analytics.Opt) (*TiltAnalytics, *analytics.MemoryAnalytics) {
ma := analytics.NewMemoryAnalytics()
os := &optSetting{}
a := NewTiltAnalytics(initialOpt, os, ma)
err := a.SetOpt(newOpt)
if !assert.NoError(t, err) {
assert.FailNow(t, err.Error())
}
if !assert.Equal(t, newOpt, os.opt) {
t.FailNow()
}
// wipe out the reports of opt-in/out, since those are side effects of test setup, not the test itself
ma.Counts = nil
return a, ma
}
type transitionTestCase struct {
name string
initialOpt analytics.Opt
newOpt analytics.Opt
expectRecord bool
}
func TestOptTransitionIncr(t *testing.T) {
for _, test := range []transitionTestCase{
{"default -> out", analytics.OptDefault, analytics.OptOut, false},
{"default -> in", analytics.OptDefault, analytics.OptIn, true},
{"in -> out", analytics.OptIn, analytics.OptOut, false},
{"out -> in", analytics.OptOut, analytics.OptIn, true},
} {
t.Run(test.name, func(t *testing.T) {
a, ma := analyticsViaTransition(t, test.initialOpt, test.newOpt)
a.Incr("foo", testTags)
var expectedCounts []analytics.CountEvent
if test.expectRecord {
expectedCounts = append(expectedCounts, analytics.CountEvent{
Name: "foo",
Tags: testTags,
N: 1,
})
}
assert.Equal(t, expectedCounts, ma.Counts)
})
}
}
func TestOptTransitionIncrIfUnopted(t *testing.T) {
for _, test := range []transitionTestCase{
{"default -> out", analytics.OptDefault, analytics.OptOut, false},
{"default -> in", analytics.OptDefault, analytics.OptIn, false},
{"in -> out", analytics.OptIn, analytics.OptOut, false},
{"out -> in", analytics.OptOut, analytics.OptIn, false},
} {
t.Run(test.name, func(t *testing.T) {
a, ma := analyticsViaTransition(t, test.initialOpt, test.newOpt)
a.IncrIfUnopted("foo")
var expectedCounts []analytics.CountEvent
if test.expectRecord {
expectedCounts = append(expectedCounts, analytics.CountEvent{
Name: "foo",
Tags: map[string]string{},
N: 1,
})
}
assert.Equal(t, expectedCounts, ma.Counts)
})
}
}
func TestOptIn(t *testing.T) {
for _, test := range []struct {
name string
opt analytics.Opt
metricName string
}{
{"in", analytics.OptIn, "analytics.opt.in"},
{"out", analytics.OptOut, ""},
} {
t.Run(test.name, func(t *testing.T) {
ma := analytics.NewMemoryAnalytics()
os := &optSetting{}
a := NewTiltAnalytics(analytics.OptDefault, os, ma)
err := a.SetOpt(test.opt)
if !assert.NoError(t, err) {
t.FailNow()
}
var expectedCounts []analytics.CountEvent
if test.metricName != "" {
expectedCounts = append(expectedCounts, analytics.CountEvent{
Name: test.metricName,
Tags: map[string]string{},
N: 1,
})
}
assert.Equal(t, expectedCounts, ma.Counts)
})
}
}
type testOpter struct {
calls []analytics.Opt
}
func (t *testOpter) SetOpt(opt analytics.Opt) error {
t.calls = append(t.calls, opt)
return nil
}
var _ AnalyticsOpter = &testOpter{}