From 022744699fc0cfb0ff7231bad0ea71635ec7ca18 Mon Sep 17 00:00:00 2001 From: Daniel Nephin Date: Thu, 8 Oct 2020 19:43:49 -0400 Subject: [PATCH] grpc: fix data rate in stats handler test --- agent/grpc/stats_test.go | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/agent/grpc/stats_test.go b/agent/grpc/stats_test.go index e91318123..f98d0f3cb 100644 --- a/agent/grpc/stats_test.go +++ b/agent/grpc/stats_test.go @@ -3,15 +3,17 @@ package grpc import ( "context" "net" + "sync" "testing" "time" "github.com/armon/go-metrics" - "github.com/hashicorp/consul/agent/grpc/internal/testservice" - "github.com/hashicorp/consul/sdk/testutil/retry" "github.com/stretchr/testify/require" "golang.org/x/sync/errgroup" "google.golang.org/grpc" + + "github.com/hashicorp/consul/agent/grpc/internal/testservice" + "github.com/hashicorp/consul/sdk/testutil/retry" ) func noopRegister(*grpc.Server) {} @@ -57,15 +59,18 @@ func TestHandler_EmitsStats(t *testing.T) { require.NoError(t, err) cancel() + conn.Close() + handler.srv.GracefulStop() // Wait for the server to stop so that active_streams is predictable. - retry.RunWith(fastRetry, t, func(r *retry.R) { - expectedGauge := []metricCall{ - {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: 0}, - } - require.Equal(r, expectedGauge, sink.gaugeCalls) - }) + require.NoError(t, g.Wait()) + + expectedGauge := []metricCall{ + {key: []string{"testing", "grpc", "server", "active_conns"}, 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_streams"}, val: 0}, + } + require.Equal(t, expectedGauge, sink.gaugeCalls) expectedCounter := []metricCall{ {key: []string{"testing", "grpc", "server", "request"}, val: 1}, @@ -96,17 +101,22 @@ func patchGlobalMetrics(t *testing.T) *fakeMetricsSink { } type fakeMetricsSink struct { + lock sync.Mutex metrics.BlackholeSink gaugeCalls []metricCall incrCounterCalls []metricCall } func (f *fakeMetricsSink) SetGaugeWithLabels(key []string, val float32, labels []metrics.Label) { + f.lock.Lock() f.gaugeCalls = append(f.gaugeCalls, metricCall{key: key, val: val, labels: labels}) + f.lock.Unlock() } func (f *fakeMetricsSink) IncrCounterWithLabels(key []string, val float32, labels []metrics.Label) { + f.lock.Lock() f.incrCounterCalls = append(f.incrCounterCalls, metricCall{key: key, val: val, labels: labels}) + f.lock.Unlock() } type metricCall struct {