2020-08-28 23:23:40 +00:00
|
|
|
package grpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net"
|
2020-10-08 23:43:49 +00:00
|
|
|
"sync"
|
2020-08-28 23:23:40 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/armon/go-metrics"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"golang.org/x/sync/errgroup"
|
|
|
|
"google.golang.org/grpc"
|
2020-10-08 23:43:49 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/consul/agent/grpc/internal/testservice"
|
|
|
|
"github.com/hashicorp/consul/sdk/testutil/retry"
|
2020-08-28 23:23:40 +00:00
|
|
|
)
|
|
|
|
|
2020-09-08 21:31:47 +00:00
|
|
|
func noopRegister(*grpc.Server) {}
|
|
|
|
|
2020-08-28 23:23:40 +00:00
|
|
|
func TestHandler_EmitsStats(t *testing.T) {
|
|
|
|
sink := patchGlobalMetrics(t)
|
|
|
|
|
|
|
|
addr := &net.IPAddr{IP: net.ParseIP("127.0.0.1")}
|
2020-09-08 21:31:47 +00:00
|
|
|
handler := NewHandler(addr, noopRegister)
|
|
|
|
|
2020-08-28 23:23:40 +00:00
|
|
|
testservice.RegisterSimpleServer(handler.srv, &simple{})
|
|
|
|
|
|
|
|
lis, err := net.Listen("tcp", "127.0.0.1:0")
|
|
|
|
require.NoError(t, err)
|
2020-09-14 17:16:13 +00:00
|
|
|
t.Cleanup(logError(t, lis.Close))
|
2020-08-28 23:23:40 +00:00
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
t.Cleanup(cancel)
|
|
|
|
|
|
|
|
g, ctx := errgroup.WithContext(ctx)
|
|
|
|
g.Go(func() error {
|
|
|
|
return handler.srv.Serve(lis)
|
|
|
|
})
|
|
|
|
t.Cleanup(func() {
|
|
|
|
if err := handler.Shutdown(); err != nil {
|
|
|
|
t.Logf("grpc server shutdown: %v", err)
|
|
|
|
}
|
|
|
|
if err := g.Wait(); err != nil {
|
|
|
|
t.Logf("grpc server error: %v", err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
conn, err := grpc.DialContext(ctx, lis.Addr().String(), grpc.WithInsecure())
|
|
|
|
require.NoError(t, err)
|
2020-09-14 17:16:13 +00:00
|
|
|
t.Cleanup(logError(t, conn.Close))
|
2020-08-28 23:23:40 +00:00
|
|
|
|
|
|
|
client := testservice.NewSimpleClient(conn)
|
|
|
|
fClient, err := client.Flow(ctx, &testservice.Req{Datacenter: "mine"})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Wait for the first event so that we know the stream is sending.
|
|
|
|
_, err = fClient.Recv()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-09-14 17:16:13 +00:00
|
|
|
cancel()
|
2020-10-08 23:43:49 +00:00
|
|
|
conn.Close()
|
|
|
|
handler.srv.GracefulStop()
|
2020-09-14 17:16:13 +00:00
|
|
|
// Wait for the server to stop so that active_streams is predictable.
|
2020-10-08 23:43:49 +00:00
|
|
|
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)
|
2020-09-14 17:16:13 +00:00
|
|
|
|
2020-08-28 23:23:40 +00:00
|
|
|
expectedCounter := []metricCall{
|
|
|
|
{key: []string{"testing", "grpc", "server", "request"}, val: 1},
|
|
|
|
}
|
|
|
|
require.Equal(t, expectedCounter, sink.incrCounterCalls)
|
|
|
|
}
|
|
|
|
|
2020-09-14 17:16:13 +00:00
|
|
|
var fastRetry = &retry.Timer{Timeout: 7 * time.Second, Wait: 2 * time.Millisecond}
|
2020-08-28 23:23:40 +00:00
|
|
|
|
|
|
|
func patchGlobalMetrics(t *testing.T) *fakeMetricsSink {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
sink := &fakeMetricsSink{}
|
|
|
|
cfg := &metrics.Config{
|
|
|
|
ServiceName: "testing",
|
|
|
|
TimerGranularity: time.Millisecond, // Timers are in milliseconds
|
|
|
|
ProfileInterval: time.Second, // Poll runtime every second
|
|
|
|
FilterDefault: true,
|
|
|
|
}
|
2020-09-14 17:16:13 +00:00
|
|
|
var err error
|
|
|
|
defaultMetrics, err = metrics.New(cfg, sink)
|
2020-08-28 23:23:40 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
t.Cleanup(func() {
|
|
|
|
_, err = metrics.NewGlobal(cfg, &metrics.BlackholeSink{})
|
|
|
|
require.NoError(t, err, "failed to reset global metrics")
|
|
|
|
})
|
|
|
|
return sink
|
|
|
|
}
|
|
|
|
|
|
|
|
type fakeMetricsSink struct {
|
2020-10-08 23:43:49 +00:00
|
|
|
lock sync.Mutex
|
2020-08-28 23:23:40 +00:00
|
|
|
metrics.BlackholeSink
|
|
|
|
gaugeCalls []metricCall
|
|
|
|
incrCounterCalls []metricCall
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *fakeMetricsSink) SetGaugeWithLabels(key []string, val float32, labels []metrics.Label) {
|
2020-10-08 23:43:49 +00:00
|
|
|
f.lock.Lock()
|
2020-08-28 23:23:40 +00:00
|
|
|
f.gaugeCalls = append(f.gaugeCalls, metricCall{key: key, val: val, labels: labels})
|
2020-10-08 23:43:49 +00:00
|
|
|
f.lock.Unlock()
|
2020-08-28 23:23:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f *fakeMetricsSink) IncrCounterWithLabels(key []string, val float32, labels []metrics.Label) {
|
2020-10-08 23:43:49 +00:00
|
|
|
f.lock.Lock()
|
2020-08-28 23:23:40 +00:00
|
|
|
f.incrCounterCalls = append(f.incrCounterCalls, metricCall{key: key, val: val, labels: labels})
|
2020-10-08 23:43:49 +00:00
|
|
|
f.lock.Unlock()
|
2020-08-28 23:23:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type metricCall struct {
|
|
|
|
key []string
|
|
|
|
val float32
|
|
|
|
labels []metrics.Label
|
|
|
|
}
|
2020-09-14 17:16:13 +00:00
|
|
|
|
|
|
|
func logError(t *testing.T, f func() error) func() {
|
|
|
|
return func() {
|
|
|
|
if err := f(); err != nil {
|
|
|
|
t.Logf(err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|