Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion api/bulk_write_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ func TestPostEntitiesOK(t *testing.T) {
{Method: "Inc", Metric: metrics.Created, IntVal: 2},
{Method: "Val", Metric: metrics.LatencyMs, IntVal: 0},
}
fixLatencyMetric(t, 150, expectMetrics, server.metricsrec.Called)
assert.Equal(t, expectMetrics, server.metricsrec.Called)

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

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

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

// ----------------------------------------------------------------------
Expand Down Expand Up @@ -224,6 +228,7 @@ func TestPostEntitiesErrors(t *testing.T) {
{Method: "Inc", Metric: metrics.ClientError, IntVal: 1},
{Method: "Val", Metric: metrics.LatencyMs, IntVal: 0},
}
fixLatencyMetric(t, 150, expectMetrics, server.metricsrec.Called)
assert.Equal(t, expectMetrics, server.metricsrec.Called)
}

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

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

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

// ----------------------------------------------------------------------
Expand Down Expand Up @@ -412,7 +420,8 @@ func TestPutEntitiesErrors(t *testing.T) {
{Method: "Inc", Metric: metrics.ClientError, IntVal: 1}, // error
{Method: "Val", Metric: metrics.LatencyMs, IntVal: 0},
}
assert.Equal(t, server.metricsrec.Called, expectMetrics)
fixLatencyMetric(t, 150, expectMetrics, server.metricsrec.Called)
assert.Equal(t, expectMetrics, server.metricsrec.Called)

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

// ----------------------------------------------------------------------
Expand Down Expand Up @@ -471,6 +481,7 @@ func TestPutEntitiesErrors(t *testing.T) {
{Method: "Inc", Metric: metrics.ClientError, IntVal: 1}, // error
{Method: "Val", Metric: metrics.LatencyMs, IntVal: 0},
}
fixLatencyMetric(t, 150, expectMetrics, server.metricsrec.Called)
assert.Equal(t, expectMetrics, server.metricsrec.Called)
}

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

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

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

// ----------------------------------------------------------------------
Expand Down Expand Up @@ -651,5 +665,6 @@ func TestDeleteEntitiesErrors(t *testing.T) {
{Method: "Inc", Metric: metrics.ClientError, IntVal: 1}, // error
{Method: "Val", Metric: metrics.LatencyMs, IntVal: 0},
}
fixLatencyMetric(t, 150, expectMetrics, server.metricsrec.Called)
assert.Equal(t, expectMetrics, server.metricsrec.Called)
}
24 changes: 24 additions & 0 deletions api/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ func TestQueryBasic(t *testing.T) {
{Method: "Val", Metric: metrics.ReadMatch, IntVal: 3}, // len(testEntities)
{Method: "Val", Metric: metrics.LatencyMs, IntVal: 0},
}
fixLatencyMetric(t, 150, expectMetrics, server.metricsrec.Called)
assert.Equal(t, expectMetrics, server.metricsrec.Called)

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

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

// -- Auth -----------------------------------------------------------
Expand Down Expand Up @@ -205,6 +208,7 @@ func TestQueryNoMatches(t *testing.T) {
{Method: "Val", Metric: metrics.ReadMatch, IntVal: 0}, // no matching queries
{Method: "Val", Metric: metrics.LatencyMs, IntVal: 0},
}
fixLatencyMetric(t, 150, expectMetrics, server.metricsrec.Called)
assert.Equal(t, expectMetrics, server.metricsrec.Called)
}

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

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

Expand Down Expand Up @@ -377,3 +383,21 @@ func TestResponseCompression(t *testing.T) {
// Make sure content type is correct
assert.Equal(t, "application/json", res.Header.Get("Content-Type"))
}

// fixLatencyMetric is a helper function that fixes the non-deterministic latency to ensure actual==expected for assertions.
// Since latency is non-deterministic, it can cause tests to fail intermittently. This function replaces the latency metric
// in the "expect" metrics with the "actual" value, so that the test can pass.
// It also asserts that the actual latency is between 0 and the provided max value, to ensure that the latency is within acceptable limits.
func fixLatencyMetric(t *testing.T, max int, expect, actual []mock.MetricMethodArgs) {
t.Helper()
if len(actual) != len(expect) {
// Something else is wrong, the test is going to fail anyway. Let it fail.
return
}
for i, _ := range actual {
if actual[i].Metric == metrics.LatencyMs && expect[i].Metric == metrics.LatencyMs && actual[i].Method == expect[i].Method {
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)
expect[i].IntVal = actual[i].IntVal
}
}
}
7 changes: 7 additions & 0 deletions api/single_entity_read_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ func TestGetEntityBasic(t *testing.T) {
{Method: "Inc", Metric: metrics.ReadId, IntVal: 1},
{Method: "Val", Metric: metrics.LatencyMs, IntVal: 0},
}
fixLatencyMetric(t, 150, expectMetrics, server.metricsrec.Called)
assert.Equal(t, expectMetrics, server.metricsrec.Called)

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

// -- Auth -----------------------------------------------------------
Expand Down Expand Up @@ -152,6 +154,7 @@ func TestGetEntityNotFound(t *testing.T) {
{Method: "Inc", Metric: metrics.ReadId, IntVal: 1},
{Method: "Val", Metric: metrics.LatencyMs, IntVal: 0},
}
fixLatencyMetric(t, 150, expectMetrics, server.metricsrec.Called)
assert.Equal(t, expectMetrics, server.metricsrec.Called)
}

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

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

// -- Metrics -----------------------------------------------------------
expectMetrics = []mock.MetricMethodArgs{}
fixLatencyMetric(t, 150, expectMetrics, server.metricsrec.Called)
assert.Equal(t, expectMetrics, server.metricsrec.Called)

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

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

// -- Auth -----------------------------------------------------------
Expand Down
15 changes: 15 additions & 0 deletions api/single_entity_write_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ func TestPostEntityOK(t *testing.T) {
{Method: "Inc", Metric: metrics.Created, IntVal: 1},
{Method: "Val", Metric: metrics.LatencyMs, IntVal: 0},
}
fixLatencyMetric(t, 150, expectMetrics, server.metricsrec.Called)
assert.Equal(t, expectMetrics, server.metricsrec.Called)

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

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

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

// ----------------------------------------------------------------------
Expand Down Expand Up @@ -234,6 +238,7 @@ func TestPostEntityErrors(t *testing.T) {
{Method: "Inc", Metric: metrics.ClientError, IntVal: 1},
{Method: "Val", Metric: metrics.LatencyMs, IntVal: 0},
}
fixLatencyMetric(t, 150, expectMetrics, server.metricsrec.Called)
assert.Equal(t, expectMetrics, server.metricsrec.Called)
}

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

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

// -- Auth -----------------------------------------------------------
Expand Down Expand Up @@ -404,6 +411,7 @@ func TestPutEntityNotFound(t *testing.T) {
//{Method: "Inc", Metric: metrics.Updated, IntVal: 1},
{Method: "Val", Metric: metrics.LatencyMs, IntVal: 0},
}
fixLatencyMetric(t, 150, expectMetrics, server.metricsrec.Called)
assert.Equal(t, expectMetrics, server.metricsrec.Called)
}

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

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

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

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

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

// ----------------------------------------------------------------------
Expand Down Expand Up @@ -564,6 +576,7 @@ func TestPutEntityErrors(t *testing.T) {
{Method: "Inc", Metric: metrics.ClientError, IntVal: 1},
{Method: "Val", Metric: metrics.LatencyMs, IntVal: 0},
}
fixLatencyMetric(t, 150, expectMetrics, server.metricsrec.Called)
assert.Equal(t, expectMetrics, server.metricsrec.Called)
}

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

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

// -- Auth -----------------------------------------------------------
Expand Down