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

Method TestPickFirstMetricsFailure

balancer/pickfirst/metrics_test.go:140–173  ·  view source on GitHub ↗

TestPickFirstMetricsFailure tests the connection attempts failed metric. It configures a channel and scenario that causes a pick first connection attempt to fail, and then expects that metric to emit.

(t *testing.T)

Source from the content-addressed store, hash-verified

138// configures a channel and scenario that causes a pick first connection attempt
139// to fail, and then expects that metric to emit.
140func (s) TestPickFirstMetricsFailure(t *testing.T) {
141 ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
142 defer cancel()
143
144 sc := internal.ParseServiceConfig.(func(string) *serviceconfig.ParseResult)(pfConfig)
145
146 r := manual.NewBuilderWithScheme("whatever")
147 r.InitialState(resolver.State{
148 ServiceConfig: sc,
149 Addresses: []resolver.Address{{Addr: "bad address"}}},
150 )
151 grpcTarget := r.Scheme() + ":///"
152 tmr := stats.NewTestMetricsRecorder()
153 cc, err := grpc.NewClient(grpcTarget, grpc.WithStatsHandler(tmr), grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithResolvers(r))
154 if err != nil {
155 t.Fatalf("NewClient() failed with error: %v", err)
156 }
157 defer cc.Close()
158
159 tsc := testgrpc.NewTestServiceClient(cc)
160 if _, err := tsc.EmptyCall(ctx, &testpb.Empty{}); err == nil {
161 t.Fatalf("EmptyCall() passed when expected to fail")
162 }
163
164 if got, _ := tmr.Metric("grpc.lb.pick_first.connection_attempts_succeeded"); got != 0 {
165 t.Errorf("Unexpected data for metric %v, got: %v, want: %v", "grpc.lb.pick_first.connection_attempts_succeeded", got, 0)
166 }
167 if got, _ := tmr.Metric("grpc.lb.pick_first.connection_attempts_failed"); got != 1 {
168 t.Errorf("Unexpected data for metric %v, got: %v, want: %v", "grpc.lb.pick_first.connection_attempts_failed", got, 1)
169 }
170 if got, _ := tmr.Metric("grpc.lb.pick_first.disconnections"); got != 0 {
171 t.Errorf("Unexpected data for metric %v, got: %v, want: %v", "grpc.lb.pick_first.disconnections", got, 0)
172 }
173}
174
175// TestPickFirstMetricsE2E tests the pick first metrics end to end. It
176// configures a channel with an OpenTelemetry plugin, induces all 3 pick first

Callers

nothing calls this directly

Calls 14

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
InitialStateMethod · 0.80
SchemeMethod · 0.65
FatalfMethod · 0.65

Tested by

no test coverage detected