agent/grpc: fix a flake in TestHandler_EmitsStats

pull/8961/head
Daniel Nephin 2020-10-13 19:38:13 -04:00
parent 19da9c3a9b
commit e101aa8a74
1 changed files with 25 additions and 2 deletions

View File

@ -3,11 +3,13 @@ package grpc
import ( import (
"context" "context"
"net" "net"
"sort"
"sync" "sync"
"testing" "testing"
"time" "time"
"github.com/armon/go-metrics" "github.com/armon/go-metrics"
"github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"golang.org/x/sync/errgroup" "golang.org/x/sync/errgroup"
"google.golang.org/grpc" "google.golang.org/grpc"
@ -64,22 +66,43 @@ func TestHandler_EmitsStats(t *testing.T) {
// Wait for the server to stop so that active_streams is predictable. // Wait for the server to stop so that active_streams is predictable.
require.NoError(t, g.Wait()) require.NoError(t, g.Wait())
// Occasionally the active_stream=0 metric may be emitted before the
// active_conns=0 metric. The order of those metrics is not really important
// so we sort the calls to match the expected.
sort.Slice(sink.gaugeCalls, func(i, j int) bool {
if i < 2 || j < 2 {
return i < j
}
if len(sink.gaugeCalls[i].key) < 4 || len(sink.gaugeCalls[j].key) < 4 {
return i < j
}
return sink.gaugeCalls[i].key[3] < sink.gaugeCalls[j].key[3]
})
cmpMetricCalls := cmp.AllowUnexported(metricCall{})
expectedGauge := []metricCall{ expectedGauge := []metricCall{
{key: []string{"testing", "grpc", "server", "active_conns"}, val: 1}, {key: []string{"testing", "grpc", "server", "active_conns"}, val: 1},
{key: []string{"testing", "grpc", "server", "active_streams"}, val: 1}, {key: []string{"testing", "grpc", "server", "active_streams"}, val: 1},
{key: []string{"testing", "grpc", "server", "active_conns"}, val: 0}, {key: []string{"testing", "grpc", "server", "active_conns"}, val: 0},
{key: []string{"testing", "grpc", "server", "active_streams"}, val: 0}, {key: []string{"testing", "grpc", "server", "active_streams"}, val: 0},
} }
require.Equal(t, expectedGauge, sink.gaugeCalls) assertDeepEqual(t, expectedGauge, sink.gaugeCalls, cmpMetricCalls)
expectedCounter := []metricCall{ expectedCounter := []metricCall{
{key: []string{"testing", "grpc", "server", "request"}, val: 1}, {key: []string{"testing", "grpc", "server", "request"}, val: 1},
} }
require.Equal(t, expectedCounter, sink.incrCounterCalls) assertDeepEqual(t, expectedCounter, sink.incrCounterCalls, cmpMetricCalls)
} }
var fastRetry = &retry.Timer{Timeout: 7 * time.Second, Wait: 2 * time.Millisecond} var fastRetry = &retry.Timer{Timeout: 7 * time.Second, Wait: 2 * time.Millisecond}
func assertDeepEqual(t *testing.T, x, y interface{}, opts ...cmp.Option) {
t.Helper()
if diff := cmp.Diff(x, y, opts...); diff != "" {
t.Fatalf("assertion failed: values are not equal\n--- expected\n+++ actual\n%v", diff)
}
}
func patchGlobalMetrics(t *testing.T) *fakeMetricsSink { func patchGlobalMetrics(t *testing.T) *fakeMetricsSink {
t.Helper() t.Helper()