Skip to content

Commit 092a3ed

Browse files
authored
Fix intermittent test failure (#91)
A number of Etre tests verify the server side metrics after performing some operation (create, update, delete, etc.). The existing assertions assume latency will be 0. Usually it is, but occasionaly on the GitHub servers running automated tests on the PR, the latency will be a few milliseconds. This causes the test to fail since the latency metric will have a value that doesn't match the 0 in the assertion. To fix this, I've added a method "fixLatencyMetric". This method finds any latency metrics in the "expect" list and updates the value to match the corresponding metric from the "actual" list. To make sure the latency metric value is not garbage, fixLatencyMetrics also takes a max latency and asserts that the actual latency is between 0 and max. This way we still check the latency metric has some reasonable value, but we won't fail the test if the latency is a few milliseconds instead of 0.
1 parent cb04dd0 commit 092a3ed

File tree

4 files changed

+62
-1
lines changed

4 files changed

+62
-1
lines changed

api/bulk_write_test.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ func TestPostEntitiesOK(t *testing.T) {
7878
{Method: "Inc", Metric: metrics.Created, IntVal: 2},
7979
{Method: "Val", Metric: metrics.LatencyMs, IntVal: 0},
8080
}
81+
fixLatencyMetric(t, 150, expectMetrics, server.metricsrec.Called)
8182
assert.Equal(t, expectMetrics, server.metricsrec.Called)
8283

8384
// -- Auth -----------------------------------------------------------
@@ -129,6 +130,7 @@ func TestPostEntitiesErrors(t *testing.T) {
129130
{Method: "Inc", Metric: metrics.ClientError, IntVal: 1},
130131
{Method: "Val", Metric: metrics.LatencyMs, IntVal: 0},
131132
}
133+
fixLatencyMetric(t, 150, expectMetrics, server.metricsrec.Called)
132134
assert.Equal(t, expectMetrics, server.metricsrec.Called)
133135

134136
// ----------------------------------------------------------------------
@@ -164,6 +166,7 @@ func TestPostEntitiesErrors(t *testing.T) {
164166
{Method: "Inc", Metric: metrics.ClientError, IntVal: 1},
165167
{Method: "Val", Metric: metrics.LatencyMs, IntVal: 0},
166168
}
169+
fixLatencyMetric(t, 150, expectMetrics, server.metricsrec.Called)
167170
assert.Equal(t, expectMetrics, server.metricsrec.Called)
168171

169172
// ----------------------------------------------------------------------
@@ -196,6 +199,7 @@ func TestPostEntitiesErrors(t *testing.T) {
196199
{Method: "Inc", Metric: metrics.ClientError, IntVal: 1},
197200
{Method: "Val", Metric: metrics.LatencyMs, IntVal: 0},
198201
}
202+
fixLatencyMetric(t, 150, expectMetrics, server.metricsrec.Called)
199203
assert.Equal(t, expectMetrics, server.metricsrec.Called)
200204

201205
// ----------------------------------------------------------------------
@@ -224,6 +228,7 @@ func TestPostEntitiesErrors(t *testing.T) {
224228
{Method: "Inc", Metric: metrics.ClientError, IntVal: 1},
225229
{Method: "Val", Metric: metrics.LatencyMs, IntVal: 0},
226230
}
231+
fixLatencyMetric(t, 150, expectMetrics, server.metricsrec.Called)
227232
assert.Equal(t, expectMetrics, server.metricsrec.Called)
228233
}
229234

@@ -309,6 +314,7 @@ func TestPutEntitiesOK(t *testing.T) {
309314
{Method: "Inc", Metric: metrics.Updated, IntVal: 1},
310315
{Method: "Val", Metric: metrics.LatencyMs, IntVal: 0},
311316
}
317+
fixLatencyMetric(t, 150, expectMetrics, server.metricsrec.Called)
312318
assert.Equal(t, expectMetrics, server.metricsrec.Called)
313319

314320
// -- Auth -----------------------------------------------------------
@@ -357,6 +363,7 @@ func TestPutEntitiesErrors(t *testing.T) {
357363
{Method: "Inc", Metric: metrics.ClientError, IntVal: 1}, // error
358364
{Method: "Val", Metric: metrics.LatencyMs, IntVal: 0},
359365
}
366+
fixLatencyMetric(t, 150, expectMetrics, server.metricsrec.Called)
360367
assert.Equal(t, expectMetrics, server.metricsrec.Called)
361368

362369
// ----------------------------------------------------------------------
@@ -381,6 +388,7 @@ func TestPutEntitiesErrors(t *testing.T) {
381388
{Method: "Inc", Metric: metrics.ClientError, IntVal: 1}, // error
382389
{Method: "Val", Metric: metrics.LatencyMs, IntVal: 0},
383390
}
391+
fixLatencyMetric(t, 150, expectMetrics, server.metricsrec.Called)
384392
assert.Equal(t, expectMetrics, server.metricsrec.Called)
385393

386394
// ----------------------------------------------------------------------
@@ -412,7 +420,8 @@ func TestPutEntitiesErrors(t *testing.T) {
412420
{Method: "Inc", Metric: metrics.ClientError, IntVal: 1}, // error
413421
{Method: "Val", Metric: metrics.LatencyMs, IntVal: 0},
414422
}
415-
assert.Equal(t, server.metricsrec.Called, expectMetrics)
423+
fixLatencyMetric(t, 150, expectMetrics, server.metricsrec.Called)
424+
assert.Equal(t, expectMetrics, server.metricsrec.Called)
416425

417426
// ----------------------------------------------------------------------
418427
// No patch (empty payload)
@@ -442,6 +451,7 @@ func TestPutEntitiesErrors(t *testing.T) {
442451
{Method: "Inc", Metric: metrics.ClientError, IntVal: 1}, // error
443452
{Method: "Val", Metric: metrics.LatencyMs, IntVal: 0},
444453
}
454+
fixLatencyMetric(t, 150, expectMetrics, server.metricsrec.Called)
445455
assert.Equal(t, expectMetrics, server.metricsrec.Called)
446456

447457
// ----------------------------------------------------------------------
@@ -471,6 +481,7 @@ func TestPutEntitiesErrors(t *testing.T) {
471481
{Method: "Inc", Metric: metrics.ClientError, IntVal: 1}, // error
472482
{Method: "Val", Metric: metrics.LatencyMs, IntVal: 0},
473483
}
484+
fixLatencyMetric(t, 150, expectMetrics, server.metricsrec.Called)
474485
assert.Equal(t, expectMetrics, server.metricsrec.Called)
475486
}
476487

@@ -548,6 +559,7 @@ func TestDeleteEntitiesOK(t *testing.T) {
548559
{Method: "Inc", Metric: metrics.Deleted, IntVal: 1},
549560
{Method: "Val", Metric: metrics.LatencyMs, IntVal: 0},
550561
}
562+
fixLatencyMetric(t, 150, expectMetrics, server.metricsrec.Called)
551563
assert.Equal(t, expectMetrics, server.metricsrec.Called)
552564

553565
// -- Auth -----------------------------------------------------------
@@ -596,6 +608,7 @@ func TestDeleteEntitiesErrors(t *testing.T) {
596608
{Method: "Inc", Metric: metrics.ClientError, IntVal: 1}, // error
597609
{Method: "Val", Metric: metrics.LatencyMs, IntVal: 0},
598610
}
611+
fixLatencyMetric(t, 150, expectMetrics, server.metricsrec.Called)
599612
assert.Equal(t, expectMetrics, server.metricsrec.Called)
600613

601614
// ----------------------------------------------------------------------
@@ -620,6 +633,7 @@ func TestDeleteEntitiesErrors(t *testing.T) {
620633
{Method: "Inc", Metric: metrics.ClientError, IntVal: 1}, // error
621634
{Method: "Val", Metric: metrics.LatencyMs, IntVal: 0},
622635
}
636+
fixLatencyMetric(t, 150, expectMetrics, server.metricsrec.Called)
623637
assert.Equal(t, server.metricsrec.Called, expectMetrics)
624638

625639
// ----------------------------------------------------------------------
@@ -651,5 +665,6 @@ func TestDeleteEntitiesErrors(t *testing.T) {
651665
{Method: "Inc", Metric: metrics.ClientError, IntVal: 1}, // error
652666
{Method: "Val", Metric: metrics.LatencyMs, IntVal: 0},
653667
}
668+
fixLatencyMetric(t, 150, expectMetrics, server.metricsrec.Called)
654669
assert.Equal(t, expectMetrics, server.metricsrec.Called)
655670
}

api/query_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ func TestQueryBasic(t *testing.T) {
6666
{Method: "Val", Metric: metrics.ReadMatch, IntVal: 3}, // len(testEntities)
6767
{Method: "Val", Metric: metrics.LatencyMs, IntVal: 0},
6868
}
69+
fixLatencyMetric(t, 150, expectMetrics, server.metricsrec.Called)
6970
assert.Equal(t, expectMetrics, server.metricsrec.Called)
7071

7172
// -- Auth -----------------------------------------------------------
@@ -110,6 +111,7 @@ func TestQueryBasic(t *testing.T) {
110111
{Method: "Val", Metric: metrics.ReadMatch, IntVal: 3}, // len(testEntities)
111112
{Method: "Val", Metric: metrics.LatencyMs, IntVal: 0},
112113
}
114+
fixLatencyMetric(t, 150, expectMetrics, server.metricsrec.Called)
113115
assert.Equal(t, expectMetrics, server.metricsrec.Called)
114116

115117
// -- Auth -----------------------------------------------------------
@@ -152,6 +154,7 @@ func TestQueryBasic(t *testing.T) {
152154
{Method: "Val", Metric: metrics.ReadMatch, IntVal: 3}, // len(testEntities)
153155
{Method: "Val", Metric: metrics.LatencyMs, IntVal: 0},
154156
}
157+
fixLatencyMetric(t, 150, expectMetrics, server.metricsrec.Called)
155158
assert.Equal(t, expectMetrics, server.metricsrec.Called)
156159

157160
// -- Auth -----------------------------------------------------------
@@ -205,6 +208,7 @@ func TestQueryNoMatches(t *testing.T) {
205208
{Method: "Val", Metric: metrics.ReadMatch, IntVal: 0}, // no matching queries
206209
{Method: "Val", Metric: metrics.LatencyMs, IntVal: 0},
207210
}
211+
fixLatencyMetric(t, 150, expectMetrics, server.metricsrec.Called)
208212
assert.Equal(t, expectMetrics, server.metricsrec.Called)
209213
}
210214

@@ -250,6 +254,7 @@ func TestQueryErrorsDatabaseError(t *testing.T) {
250254
{Method: "Inc", Metric: metrics.DbError, IntVal: 1}, // db error
251255
{Method: "Val", Metric: metrics.LatencyMs, IntVal: 0},
252256
}
257+
fixLatencyMetric(t, 150, expectMetrics, server.metricsrec.Called)
253258
assert.Equal(t, expectMetrics, server.metricsrec.Called)
254259
}
255260

@@ -345,6 +350,7 @@ func TestQueryErrorsTimeout(t *testing.T) {
345350
{Method: "Inc", Metric: metrics.QueryTimeout, IntVal: 1}, // query timeout
346351
{Method: "Val", Metric: metrics.LatencyMs, IntVal: 0},
347352
}
353+
fixLatencyMetric(t, 150, expectMetrics, server.metricsrec.Called)
348354
assert.Equal(t, expectMetrics, server.metricsrec.Called)
349355
}
350356

@@ -377,3 +383,21 @@ func TestResponseCompression(t *testing.T) {
377383
// Make sure content type is correct
378384
assert.Equal(t, "application/json", res.Header.Get("Content-Type"))
379385
}
386+
387+
// fixLatencyMetric is a helper function that fixes the non-deterministic latency to ensure actual==expected for assertions.
388+
// Since latency is non-deterministic, it can cause tests to fail intermittently. This function replaces the latency metric
389+
// in the "expect" metrics with the "actual" value, so that the test can pass.
390+
// It also asserts that the actual latency is between 0 and the provided max value, to ensure that the latency is within acceptable limits.
391+
func fixLatencyMetric(t *testing.T, max int, expect, actual []mock.MetricMethodArgs) {
392+
t.Helper()
393+
if len(actual) != len(expect) {
394+
// Something else is wrong, the test is going to fail anyway. Let it fail.
395+
return
396+
}
397+
for i, _ := range actual {
398+
if actual[i].Metric == metrics.LatencyMs && expect[i].Metric == metrics.LatencyMs && actual[i].Method == expect[i].Method {
399+
assert.True(t, actual[i].IntVal >= 0 && actual[i].IntVal <= int64(max), "Latency metric value %d must be between 0 and %d.", actual[i].IntVal, max)
400+
expect[i].IntVal = actual[i].IntVal
401+
}
402+
}
403+
}

api/single_entity_read_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ func TestGetEntityBasic(t *testing.T) {
6666
{Method: "Inc", Metric: metrics.ReadId, IntVal: 1},
6767
{Method: "Val", Metric: metrics.LatencyMs, IntVal: 0},
6868
}
69+
fixLatencyMetric(t, 150, expectMetrics, server.metricsrec.Called)
6970
assert.Equal(t, expectMetrics, server.metricsrec.Called)
7071

7172
// -- Auth -----------------------------------------------------------
@@ -112,6 +113,7 @@ func TestGetEntityReturnLabels(t *testing.T) {
112113
{Method: "Inc", Metric: metrics.ReadId, IntVal: 1},
113114
{Method: "Val", Metric: metrics.LatencyMs, IntVal: 0},
114115
}
116+
fixLatencyMetric(t, 150, expectMetrics, server.metricsrec.Called)
115117
assert.Equal(t, expectMetrics, server.metricsrec.Called)
116118

117119
// -- Auth -----------------------------------------------------------
@@ -152,6 +154,7 @@ func TestGetEntityNotFound(t *testing.T) {
152154
{Method: "Inc", Metric: metrics.ReadId, IntVal: 1},
153155
{Method: "Val", Metric: metrics.LatencyMs, IntVal: 0},
154156
}
157+
fixLatencyMetric(t, 150, expectMetrics, server.metricsrec.Called)
155158
assert.Equal(t, expectMetrics, server.metricsrec.Called)
156159
}
157160

@@ -193,6 +196,7 @@ func TestGetEntityErrors(t *testing.T) {
193196
{Method: "Inc", Metric: metrics.ClientError, IntVal: 1}, // error
194197
{Method: "Val", Metric: metrics.LatencyMs, IntVal: 0},
195198
}
199+
fixLatencyMetric(t, 150, expectMetrics, server.metricsrec.Called)
196200
assert.Equal(t, expectMetrics, server.metricsrec.Called)
197201

198202
// ----------------------------------------------------------------------
@@ -213,6 +217,7 @@ func TestGetEntityErrors(t *testing.T) {
213217

214218
// -- Metrics -----------------------------------------------------------
215219
expectMetrics = []mock.MetricMethodArgs{}
220+
fixLatencyMetric(t, 150, expectMetrics, server.metricsrec.Called)
216221
assert.Equal(t, expectMetrics, server.metricsrec.Called)
217222

218223
// ----------------------------------------------------------------------
@@ -239,6 +244,7 @@ func TestGetEntityErrors(t *testing.T) {
239244
{Method: "Inc", Metric: metrics.DbError, IntVal: 1}, // error
240245
{Method: "Val", Metric: metrics.LatencyMs, IntVal: 0},
241246
}
247+
fixLatencyMetric(t, 150, expectMetrics, server.metricsrec.Called)
242248
assert.Equal(t, expectMetrics, server.metricsrec.Called)
243249
}
244250

@@ -286,6 +292,7 @@ func TestGetEntityLabels(t *testing.T) {
286292
{Method: "Inc", Metric: metrics.ReadLabels, IntVal: 1},
287293
{Method: "Val", Metric: metrics.LatencyMs, IntVal: 0},
288294
}
295+
fixLatencyMetric(t, 150, expectMetrics, server.metricsrec.Called)
289296
assert.Equal(t, expectMetrics, server.metricsrec.Called)
290297

291298
// -- Auth -----------------------------------------------------------

api/single_entity_write_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ func TestPostEntityOK(t *testing.T) {
8181
{Method: "Inc", Metric: metrics.Created, IntVal: 1},
8282
{Method: "Val", Metric: metrics.LatencyMs, IntVal: 0},
8383
}
84+
fixLatencyMetric(t, 150, expectMetrics, server.metricsrec.Called)
8485
assert.Equal(t, expectMetrics, server.metricsrec.Called)
8586

8687
// -- Auth -----------------------------------------------------------
@@ -128,6 +129,7 @@ func TestPostEntityDuplicate(t *testing.T) {
128129
{Method: "Inc", Metric: metrics.DbError, IntVal: 1},
129130
{Method: "Val", Metric: metrics.LatencyMs, IntVal: 0},
130131
}
132+
fixLatencyMetric(t, 150, expectMetrics, server.metricsrec.Called)
131133
assert.Equal(t, expectMetrics, server.metricsrec.Called)
132134

133135
// -- Auth -----------------------------------------------------------
@@ -177,6 +179,7 @@ func TestPostEntityErrors(t *testing.T) {
177179
{Method: "Inc", Metric: metrics.ClientError, IntVal: 1},
178180
{Method: "Val", Metric: metrics.LatencyMs, IntVal: 0},
179181
}
182+
fixLatencyMetric(t, 150, expectMetrics, server.metricsrec.Called)
180183
assert.Equal(t, expectMetrics, server.metricsrec.Called)
181184

182185
// ----------------------------------------------------------------------
@@ -207,6 +210,7 @@ func TestPostEntityErrors(t *testing.T) {
207210
{Method: "Inc", Metric: metrics.ClientError, IntVal: 1},
208211
{Method: "Val", Metric: metrics.LatencyMs, IntVal: 0},
209212
}
213+
fixLatencyMetric(t, 150, expectMetrics, server.metricsrec.Called)
210214
assert.Equal(t, expectMetrics, server.metricsrec.Called)
211215

212216
// ----------------------------------------------------------------------
@@ -234,6 +238,7 @@ func TestPostEntityErrors(t *testing.T) {
234238
{Method: "Inc", Metric: metrics.ClientError, IntVal: 1},
235239
{Method: "Val", Metric: metrics.LatencyMs, IntVal: 0},
236240
}
241+
fixLatencyMetric(t, 150, expectMetrics, server.metricsrec.Called)
237242
assert.Equal(t, expectMetrics, server.metricsrec.Called)
238243
}
239244

@@ -309,6 +314,7 @@ func TestPutEntityOK(t *testing.T) {
309314
{Method: "Inc", Metric: metrics.Updated, IntVal: 1},
310315
{Method: "Val", Metric: metrics.LatencyMs, IntVal: 0},
311316
}
317+
fixLatencyMetric(t, 150, expectMetrics, server.metricsrec.Called)
312318
assert.Equal(t, expectMetrics, server.metricsrec.Called)
313319

314320
// -- Auth -----------------------------------------------------------
@@ -359,6 +365,7 @@ func TestPutEntityDuplicate(t *testing.T) {
359365
{Method: "Inc", Metric: metrics.DbError, IntVal: 1},
360366
{Method: "Val", Metric: metrics.LatencyMs, IntVal: 0},
361367
}
368+
fixLatencyMetric(t, 150, expectMetrics, server.metricsrec.Called)
362369
assert.Equal(t, expectMetrics, server.metricsrec.Called)
363370

364371
// -- Auth -----------------------------------------------------------
@@ -404,6 +411,7 @@ func TestPutEntityNotFound(t *testing.T) {
404411
//{Method: "Inc", Metric: metrics.Updated, IntVal: 1},
405412
{Method: "Val", Metric: metrics.LatencyMs, IntVal: 0},
406413
}
414+
fixLatencyMetric(t, 150, expectMetrics, server.metricsrec.Called)
407415
assert.Equal(t, expectMetrics, server.metricsrec.Called)
408416
}
409417

@@ -442,6 +450,7 @@ func TestPutEntityErrors(t *testing.T) {
442450

443451
// -- Metrics -----------------------------------------------------------
444452
expectMetrics := []mock.MetricMethodArgs{}
453+
fixLatencyMetric(t, 150, expectMetrics, server.metricsrec.Called)
445454
assert.Equal(t, expectMetrics, server.metricsrec.Called)
446455

447456
// ----------------------------------------------------------------------
@@ -470,6 +479,7 @@ func TestPutEntityErrors(t *testing.T) {
470479
{Method: "Inc", Metric: metrics.ClientError, IntVal: 1},
471480
{Method: "Val", Metric: metrics.LatencyMs, IntVal: 0},
472481
}
482+
fixLatencyMetric(t, 150, expectMetrics, server.metricsrec.Called)
473483
assert.Equal(t, expectMetrics, server.metricsrec.Called)
474484

475485
// ----------------------------------------------------------------------
@@ -503,6 +513,7 @@ func TestPutEntityErrors(t *testing.T) {
503513
{Method: "Inc", Metric: metrics.ClientError, IntVal: 1},
504514
{Method: "Val", Metric: metrics.LatencyMs, IntVal: 0},
505515
}
516+
fixLatencyMetric(t, 150, expectMetrics, server.metricsrec.Called)
506517
assert.Equal(t, expectMetrics, server.metricsrec.Called)
507518

508519
// ----------------------------------------------------------------------
@@ -535,6 +546,7 @@ func TestPutEntityErrors(t *testing.T) {
535546
{Method: "Inc", Metric: metrics.ClientError, IntVal: 1},
536547
{Method: "Val", Metric: metrics.LatencyMs, IntVal: 0},
537548
}
549+
fixLatencyMetric(t, 150, expectMetrics, server.metricsrec.Called)
538550
assert.Equal(t, expectMetrics, server.metricsrec.Called)
539551

540552
// ----------------------------------------------------------------------
@@ -564,6 +576,7 @@ func TestPutEntityErrors(t *testing.T) {
564576
{Method: "Inc", Metric: metrics.ClientError, IntVal: 1},
565577
{Method: "Val", Metric: metrics.LatencyMs, IntVal: 0},
566578
}
579+
fixLatencyMetric(t, 150, expectMetrics, server.metricsrec.Called)
567580
assert.Equal(t, expectMetrics, server.metricsrec.Called)
568581
}
569582

@@ -638,6 +651,7 @@ func TestDeleteEntityOK(t *testing.T) {
638651
{Method: "Inc", Metric: metrics.Deleted, IntVal: 1},
639652
{Method: "Val", Metric: metrics.LatencyMs, IntVal: 0},
640653
}
654+
fixLatencyMetric(t, 150, expectMetrics, server.metricsrec.Called)
641655
assert.Equal(t, expectMetrics, server.metricsrec.Called)
642656

643657
// -- Auth -----------------------------------------------------------
@@ -711,6 +725,7 @@ func TestDeleteLabel(t *testing.T) {
711725
{Method: "IncLabel", Metric: metrics.LabelDelete, StringVal: "foo"},
712726
{Method: "Val", Metric: metrics.LatencyMs, IntVal: 0},
713727
}
728+
fixLatencyMetric(t, 150, expectMetrics, server.metricsrec.Called)
714729
assert.Equal(t, expectMetrics, server.metricsrec.Called)
715730

716731
// -- Auth -----------------------------------------------------------

0 commit comments

Comments
 (0)