Skip to content

Commit

Permalink
daemon: Fix waiting on metrics in endpoint_test.go
Browse files Browse the repository at this point in the history
[ upstream commit 19030a7 ]

[ Backporter's notes: Minor import conflict ]

This commit fixes a flake where the metrics do not update as quickly as
we expect. It is fixed by waiting for up to 10 seconds to retrieve the
metric value we expect.

Fixes: 4a4a59b ("daemon: Add assertions for endpoint state metrics")

Signed-off-by: Chris Tarazi <chris@isovalent.com>
Signed-off-by: Joe Stringer <joe@cilium.io>
  • Loading branch information
christarazi authored and joestringer committed Jun 12, 2020
1 parent ad7a5c0 commit 24126c1
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions daemon/endpoint_test.go
Expand Up @@ -33,6 +33,7 @@ import (
"github.com/cilium/cilium/pkg/labels"
"github.com/cilium/cilium/pkg/metrics"
"github.com/cilium/cilium/pkg/option"
"github.com/cilium/cilium/pkg/testutils"

. "gopkg.in/check.v1"
)
Expand Down Expand Up @@ -168,11 +169,20 @@ func getMetricValue(state string) int64 {
}

func assertOnMetric(c *C, state string, expected int64) {
obtained := getMetricValue(state)
if obtained != expected {
_, _, line, _ := runtime.Caller(1)
c.Errorf("Metrics assertion failed on line %d for Endpoint state %s: obtained %d, expected %d",
line, state, obtained, expected)
_, _, line, _ := runtime.Caller(1)

obtainedValues := make(map[int64]struct{}, 0)
err := testutils.WaitUntil(func() bool {
obtained := getMetricValue(state)
obtainedValues[obtained] = struct{}{}
return obtained == expected
}, 10*time.Second)
if err != nil {
// We are printing the map here to show every unique obtained metrics
// value because these values change rapidly and it may be misleading
// to only show the last obtained value.
c.Errorf("Metrics assertion failed on line %d for Endpoint state %s: obtained %v, expected %d",
line, state, obtainedValues, expected)
}
}

Expand Down

0 comments on commit 24126c1

Please sign in to comment.