grpc: fix data rate in stats handler test

pull/8896/head
Daniel Nephin 2020-10-08 19:43:49 -04:00
parent 1eefd41882
commit af8a617797
1 changed files with 20 additions and 10 deletions

View File

@ -3,15 +3,17 @@ package grpc
import ( import (
"context" "context"
"net" "net"
"sync"
"testing" "testing"
"time" "time"
"github.com/armon/go-metrics" "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" "github.com/stretchr/testify/require"
"golang.org/x/sync/errgroup" "golang.org/x/sync/errgroup"
"google.golang.org/grpc" "google.golang.org/grpc"
"github.com/hashicorp/consul/agent/grpc/internal/testservice"
"github.com/hashicorp/consul/sdk/testutil/retry"
) )
func noopRegister(*grpc.Server) {} func noopRegister(*grpc.Server) {}
@ -57,15 +59,18 @@ func TestHandler_EmitsStats(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
cancel() cancel()
conn.Close()
handler.srv.GracefulStop()
// Wait for the server to stop so that active_streams is predictable. // Wait for the server to stop so that active_streams is predictable.
retry.RunWith(fastRetry, t, func(r *retry.R) { require.NoError(t, g.Wait())
expectedGauge := []metricCall{
{key: []string{"testing", "grpc", "server", "active_conns"}, val: 1}, expectedGauge := []metricCall{
{key: []string{"testing", "grpc", "server", "active_streams"}, val: 1}, {key: []string{"testing", "grpc", "server", "active_conns"}, val: 1},
{key: []string{"testing", "grpc", "server", "active_streams"}, val: 0}, {key: []string{"testing", "grpc", "server", "active_streams"}, val: 1},
} {key: []string{"testing", "grpc", "server", "active_conns"}, val: 0},
require.Equal(r, expectedGauge, sink.gaugeCalls) {key: []string{"testing", "grpc", "server", "active_streams"}, val: 0},
}) }
require.Equal(t, expectedGauge, sink.gaugeCalls)
expectedCounter := []metricCall{ expectedCounter := []metricCall{
{key: []string{"testing", "grpc", "server", "request"}, val: 1}, {key: []string{"testing", "grpc", "server", "request"}, val: 1},
@ -96,17 +101,22 @@ func patchGlobalMetrics(t *testing.T) *fakeMetricsSink {
} }
type fakeMetricsSink struct { type fakeMetricsSink struct {
lock sync.Mutex
metrics.BlackholeSink metrics.BlackholeSink
gaugeCalls []metricCall gaugeCalls []metricCall
incrCounterCalls []metricCall incrCounterCalls []metricCall
} }
func (f *fakeMetricsSink) SetGaugeWithLabels(key []string, val float32, labels []metrics.Label) { 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.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) { 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.incrCounterCalls = append(f.incrCounterCalls, metricCall{key: key, val: val, labels: labels})
f.lock.Unlock()
} }
type metricCall struct { type metricCall struct {