-
Notifications
You must be signed in to change notification settings - Fork 18
/
migration_op_tracker_test.go
355 lines (265 loc) · 10.4 KB
/
migration_op_tracker_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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
package ldclient
import (
"math/rand"
"testing"
"time"
"github.com/launchdarkly/go-sdk-common/v3/ldcontext"
"github.com/launchdarkly/go-sdk-common/v3/ldmigration"
"github.com/launchdarkly/go-sdk-common/v3/ldreason"
"github.com/launchdarkly/go-sdk-common/v3/ldsampling"
"github.com/launchdarkly/go-sdk-common/v3/ldvalue"
"github.com/launchdarkly/go-server-sdk-evaluation/v3/ldbuilders"
"github.com/stretchr/testify/assert"
)
var allOrigins = []ldmigration.Origin{ldmigration.Old, ldmigration.New}
func minimalTracker(samplingRatio int) *MigrationOpTracker {
params := ldbuilders.NewMigrationFlagParametersBuilder().CheckRatio(samplingRatio).Build()
flag := ldbuilders.NewFlagBuilder("flag-key").MigrationFlagParameters(params).Build()
context := ldcontext.New("user-key")
detail := ldreason.NewEvaluationDetail(ldvalue.Bool(true), 1, ldreason.NewEvalReasonFallthrough())
tracker := NewMigrationOpTracker("flag-key", &flag, context, detail, ldmigration.Live)
tracker.Operation(ldmigration.Write)
tracker.TrackInvoked(ldmigration.Old)
tracker.TrackInvoked(ldmigration.New)
return tracker
}
func TestTrackerCanBuildSuccessfully(t *testing.T) {
tracker := minimalTracker(1)
event, err := tracker.Build()
assert.NotNil(t, event)
assert.NoError(t, err)
}
func TestTrackerCanTrackInvocations(t *testing.T) {
t.Run("for both origins", func(t *testing.T) {
tracker := minimalTracker(1)
// Reset since the minimal tracker already includes some values
tracker.invoked = make(map[ldmigration.Origin]struct{})
tracker.TrackInvoked(ldmigration.New)
tracker.TrackInvoked(ldmigration.Old)
event, err := tracker.Build()
assert.NoError(t, err)
assert.NotNil(t, event)
assert.Len(t, event.Invoked, 2)
if _, ok := event.Invoked[ldmigration.New]; !ok {
assert.Fail(t, "event is missing new origin invoked")
}
if _, ok := event.Invoked[ldmigration.Old]; !ok {
assert.Fail(t, "event is missing old origin invoked")
}
})
t.Run("for individual origins", func(t *testing.T) {
for _, origin := range allOrigins {
tracker := minimalTracker(1)
// Reset since the minimal tracker already includes some values
tracker.invoked = make(map[ldmigration.Origin]struct{})
tracker.TrackInvoked(origin)
event, err := tracker.Build()
assert.NoError(t, err)
assert.NotNil(t, event)
assert.Len(t, event.Invoked, 1)
if _, ok := event.Invoked[origin]; !ok {
assert.Failf(t, "event is missing %s origin invoked", string(origin))
}
}
})
}
func TestTrackerCanTrackErrors(t *testing.T) {
t.Run("for both origins", func(t *testing.T) {
tracker := minimalTracker(1)
tracker.TrackError(ldmigration.New)
tracker.TrackError(ldmigration.Old)
event, err := tracker.Build()
assert.NoError(t, err)
assert.NotNil(t, event)
assert.Len(t, event.Error, 2)
if _, ok := event.Error[ldmigration.New]; !ok {
assert.Fail(t, "event is missing new origin error")
}
if _, ok := event.Error[ldmigration.Old]; !ok {
assert.Fail(t, "event is missing old origin error")
}
})
t.Run("for individual origins", func(t *testing.T) {
for _, origin := range allOrigins {
tracker := minimalTracker(1)
tracker.TrackError(origin)
event, err := tracker.Build()
assert.NoError(t, err)
assert.NotNil(t, event)
assert.Len(t, event.Error, 1)
if _, ok := event.Error[origin]; !ok {
assert.Failf(t, "event is missing %s origin error", string(origin))
}
}
})
}
func TestTrackerCanTrackLatency(t *testing.T) {
t.Run("for both origins", func(t *testing.T) {
tracker := minimalTracker(1)
tracker.TrackLatency(ldmigration.New, 5*time.Second)
tracker.TrackLatency(ldmigration.Old, 10*time.Second)
event, err := tracker.Build()
assert.NoError(t, err)
assert.NotNil(t, event)
assert.Equal(t, event.Latency[ldmigration.New], 5_000)
assert.Equal(t, event.Latency[ldmigration.Old], 10_000)
assert.Len(t, event.Latency, 2)
})
t.Run("for individual origins", func(t *testing.T) {
for _, origin := range allOrigins {
tracker := minimalTracker(1)
tracker.TrackLatency(origin, 5*time.Second)
event, err := tracker.Build()
assert.NoError(t, err)
assert.NotNil(t, event)
assert.Equal(t, event.Latency[origin], 5_000)
assert.Len(t, event.Latency, 1)
}
})
}
func TestTrackerCanTrackConsistency(t *testing.T) {
consistent := func() bool { return true }
t.Run("defaults to sampling ratio of 1", func(t *testing.T) {
tracker := minimalTracker(1)
tracker.TrackConsistency(consistent)
event, err := tracker.Build()
assert.NoError(t, err)
assert.NotNil(t, event)
assert.Equal(t, event.ConsistencyCheck.Consistent(), true)
assert.Equal(t, event.ConsistencyCheck.SamplingRatio(), 1)
})
t.Run("can disable consistency check with 0 sampling ratio", func(t *testing.T) {
tracker := minimalTracker(0)
tracker.TrackConsistency(consistent)
event, err := tracker.Build()
assert.NoError(t, err)
assert.NotNil(t, event)
assert.Nil(t, event.ConsistencyCheck)
})
t.Run("honors sampling ratio", func(t *testing.T) {
source := rand.NewSource(1)
sampler := ldsampling.NewSamplerFromSource(source)
consistencyWasChecked := 0
for i := 0; i < 1_000; i++ {
tracker := minimalTracker(10)
tracker.sampler = sampler
tracker.TrackConsistency(consistent)
event, err := tracker.Build()
assert.NoError(t, err)
assert.NotNil(t, event)
if event.ConsistencyCheck != nil {
consistencyWasChecked += 1
}
}
// Randomization isn't perfect, but this is pretty close
assert.Equal(t, 117, consistencyWasChecked)
})
}
func TestTrackerCannotBuild(t *testing.T) {
t.Run("without calling invoked", func(t *testing.T) {
flag := ldbuilders.NewFlagBuilder("flag-key").Build()
context := ldcontext.New("user-key")
detail := ldreason.NewEvaluationDetail(ldvalue.Bool(true), 1, ldreason.NewEvalReasonFallthrough())
tracker := NewMigrationOpTracker("flag-key", &flag, context, detail, ldmigration.Live)
tracker.Operation(ldmigration.Write)
event, err := tracker.Build()
assert.Nil(t, event)
assert.Error(t, err)
assert.Equal(t, err.Error(), "no origins were recorded as being invoked")
})
t.Run("without operation", func(t *testing.T) {
flag := ldbuilders.NewFlagBuilder("flag-key").Build()
context := ldcontext.New("user-key")
detail := ldreason.NewEvaluationDetail(ldvalue.Bool(true), 1, ldreason.NewEvalReasonFallthrough())
tracker := NewMigrationOpTracker("flag-key", &flag, context, detail, ldmigration.Live)
tracker.TrackInvoked(ldmigration.Old)
event, err := tracker.Build()
assert.Nil(t, event)
assert.Error(t, err)
assert.Equal(t, err.Error(), "migration operation not specified")
})
t.Run("with invalid context", func(t *testing.T) {
flag := ldbuilders.NewFlagBuilder("flag-key").Build()
context := ldcontext.New("")
detail := ldreason.NewEvaluationDetail(ldvalue.Bool(true), 1, ldreason.NewEvalReasonFallthrough())
tracker := NewMigrationOpTracker("flag-key", &flag, context, detail, ldmigration.Live)
tracker.Operation(ldmigration.Write)
tracker.TrackInvoked(ldmigration.Old)
event, err := tracker.Build()
assert.Nil(t, event)
assert.Error(t, err)
assert.Contains(t, err.Error(), "invalid context given")
})
t.Run("with empty flag key", func(t *testing.T) {
flag := ldbuilders.NewFlagBuilder("flag-key").Build()
context := ldcontext.New("")
detail := ldreason.NewEvaluationDetail(ldvalue.Bool(true), 1, ldreason.NewEvalReasonFallthrough())
tracker := NewMigrationOpTracker("", &flag, context, detail, ldmigration.Live)
tracker.Operation(ldmigration.Write)
tracker.TrackInvoked(ldmigration.Old)
event, err := tracker.Build()
assert.Nil(t, event)
assert.Error(t, err)
assert.Contains(t, err.Error(), "migration operation cannot contain an empty key")
})
t.Run("detects when invoked doesn't align", func(t *testing.T) {
flag := ldbuilders.NewFlagBuilder("flag-key").Build()
context := ldcontext.New("user-key")
detail := ldreason.NewEvaluationDetail(ldvalue.Bool(true), 1, ldreason.NewEvalReasonFallthrough())
t.Run("with latency", func(t *testing.T) {
tracker := NewMigrationOpTracker("flag-key", &flag, context, detail, ldmigration.Live)
tracker.Operation(ldmigration.Write)
tracker.TrackInvoked(ldmigration.New)
tracker.TrackLatency(ldmigration.Old, 5*time.Second)
event, err := tracker.Build()
assert.Nil(t, event)
assert.Error(t, err)
assert.Contains(t, err.Error(), "provided latency for 'old' without recording invocation")
tracker = NewMigrationOpTracker("flag-key", &flag, context, detail, ldmigration.Live)
tracker.Operation(ldmigration.Write)
tracker.TrackInvoked(ldmigration.Old)
tracker.TrackLatency(ldmigration.New, 5*time.Second)
event, err = tracker.Build()
assert.Nil(t, event)
assert.Error(t, err)
assert.Contains(t, err.Error(), "provided latency for 'new' without recording invocation")
})
t.Run("with error", func(t *testing.T) {
tracker := NewMigrationOpTracker("flag-key", &flag, context, detail, ldmigration.Live)
tracker.Operation(ldmigration.Write)
tracker.TrackInvoked(ldmigration.New)
tracker.TrackError(ldmigration.Old)
event, err := tracker.Build()
assert.Nil(t, event)
assert.Error(t, err)
assert.Contains(t, err.Error(), "provided error for 'old' without recording invocation")
tracker = NewMigrationOpTracker("flag-key", &flag, context, detail, ldmigration.Live)
tracker.Operation(ldmigration.Write)
tracker.TrackInvoked(ldmigration.Old)
tracker.TrackError(ldmigration.New)
event, err = tracker.Build()
assert.Nil(t, event)
assert.Error(t, err)
assert.Contains(t, err.Error(), "provided error for 'new' without recording invocation")
})
t.Run("with consistent", func(t *testing.T) {
consistent := func() bool { return true }
tracker := NewMigrationOpTracker("flag-key", &flag, context, detail, ldmigration.Live)
tracker.Operation(ldmigration.Write)
tracker.TrackInvoked(ldmigration.New)
tracker.TrackConsistency(consistent)
event, err := tracker.Build()
assert.Nil(t, event)
assert.Error(t, err)
assert.Contains(t, err.Error(), "provided consistency without recording both invocations")
tracker = NewMigrationOpTracker("flag-key", &flag, context, detail, ldmigration.Live)
tracker.Operation(ldmigration.Write)
tracker.TrackInvoked(ldmigration.Old)
tracker.TrackConsistency(consistent)
event, err = tracker.Build()
assert.Nil(t, event)
assert.Error(t, err)
assert.Contains(t, err.Error(), "provided consistency without recording both invocations")
})
})
}