MCPcopy
hub / github.com/grpc/grpc-go / TestPickFirstMetrics

Method TestPickFirstMetrics

balancer/pickfirst/metrics_test.go:68–135  ·  view source on GitHub ↗

TestPickFirstMetrics tests pick first metrics. It configures a pick first balancer, causes it to connect and then disconnect, and expects the subsequent metrics to emit from that.

(t *testing.T)

Source from the content-addressed store, hash-verified

66// balancer, causes it to connect and then disconnect, and expects the
67// subsequent metrics to emit from that.
68func (s) TestPickFirstMetrics(t *testing.T) {
69 ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
70 defer cancel()
71
72 ss := &stubserver.StubServer{
73 EmptyCallF: func(context.Context, *testpb.Empty) (*testpb.Empty, error) {
74 return &testpb.Empty{}, nil
75 },
76 }
77 ss.StartServer()
78 defer ss.Stop()
79
80 sc := internal.ParseServiceConfig.(func(string) *serviceconfig.ParseResult)(pfConfig)
81
82 r := manual.NewBuilderWithScheme("whatever")
83 r.InitialState(resolver.State{
84 ServiceConfig: sc,
85 Addresses: []resolver.Address{{Addr: ss.Address}}},
86 )
87
88 tmr := stats.NewTestMetricsRecorder()
89 cc, err := grpc.NewClient(r.Scheme()+":///", grpc.WithStatsHandler(tmr), grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithResolvers(r))
90 if err != nil {
91 t.Fatalf("NewClient() failed with error: %v", err)
92 }
93 defer cc.Close()
94
95 tsc := testgrpc.NewTestServiceClient(cc)
96 if _, err := tsc.EmptyCall(ctx, &testpb.Empty{}); err != nil {
97 t.Fatalf("EmptyCall() failed: %v", err)
98 }
99
100 if got, _ := tmr.Metric("grpc.lb.pick_first.connection_attempts_succeeded"); got != 1 {
101 t.Errorf("Unexpected data for metric %v, got: %v, want: %v", "grpc.lb.pick_first.connection_attempts_succeeded", got, 1)
102 }
103 if got, _ := tmr.Metric("grpc.lb.pick_first.connection_attempts_failed"); got != 0 {
104 t.Errorf("Unexpected data for metric %v, got: %v, want: %v", "grpc.lb.pick_first.connection_attempts_failed", got, 0)
105 }
106 if got, _ := tmr.Metric("grpc.lb.pick_first.disconnections"); got != 0 {
107 t.Errorf("Unexpected data for metric %v, got: %v, want: %v", "grpc.lb.pick_first.disconnections", got, 0)
108 }
109
110 // Checking for subchannel metrics as well
111 if got, _ := tmr.Metric("grpc.subchannel.connection_attempts_succeeded"); got != 1 {
112 t.Errorf("Unexpected data for metric %v, got: %v, want: %v", "grpc.subchannel.connection_attempts_succeeded", got, 1)
113 }
114 if got, _ := tmr.Metric("grpc.subchannel.connection_attempts_failed"); got != 0 {
115 t.Errorf("Unexpected data for metric %v, got: %v, want: %v", "grpc.subchannel.connection_attempts_failed", got, 0)
116 }
117 if got, _ := tmr.Metric("grpc.subchannel.disconnections"); got != 0 {
118 t.Errorf("Unexpected data for metric %v, got: %v, want: %v", "grpc.subchannel.disconnections", got, 0)
119 }
120 if got, _ := tmr.Metric("grpc.subchannel.open_connections"); got != 1 {
121 t.Errorf("Unexpected data for metric %v, got: %v, want: %v", "grpc.subchannel.open_connections", got, 1)
122 }
123
124 ss.Stop()
125 testutils.AwaitState(ctx, t, cc, connectivity.Idle)

Callers

nothing calls this directly

Calls 15

StartServerMethod · 0.95
StopMethod · 0.95
EmptyCallMethod · 0.95
MetricMethod · 0.95
NewBuilderWithSchemeFunction · 0.92
NewTestMetricsRecorderFunction · 0.92
NewClientFunction · 0.92
WithStatsHandlerFunction · 0.92
WithTransportCredentialsFunction · 0.92
NewCredentialsFunction · 0.92
WithResolversFunction · 0.92
AwaitStateFunction · 0.92

Tested by

no test coverage detected