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

Method TestWeightedTarget_NoTargets

balancer/weightedtarget/weightedtarget_test.go:177–205  ·  view source on GitHub ↗

Tests the behavior of the weighted_target LB policy when there are no targets configured. It verifies that the LB policy sets the overall channel state to TRANSIENT_FAILURE and fails RPCs with an expected status code and message.

(t *testing.T)

Source from the content-addressed store, hash-verified

175// configured. It verifies that the LB policy sets the overall channel state to
176// TRANSIENT_FAILURE and fails RPCs with an expected status code and message.
177func (s) TestWeightedTarget_NoTargets(t *testing.T) {
178 dopts := []grpc.DialOption{
179 grpc.WithTransportCredentials(insecure.NewCredentials()),
180 grpc.WithDefaultServiceConfig(`{"loadBalancingConfig": [{"weighted_target_experimental":{}}]}`),
181 }
182 cc, err := grpc.NewClient("passthrough:///test.server", dopts...)
183 if err != nil {
184 t.Fatalf("grpc.NewClient() failed: %v", err)
185 }
186 defer cc.Close()
187 cc.Connect()
188
189 ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
190 defer cancel()
191 client := testgrpc.NewTestServiceClient(cc)
192 _, err = client.EmptyCall(ctx, &testpb.Empty{})
193 if err == nil {
194 t.Error("EmptyCall() succeeded, want failure")
195 }
196 if gotCode, wantCode := status.Code(err), codes.Unavailable; gotCode != wantCode {
197 t.Errorf("EmptyCall() failed with code = %v, want %s", gotCode, wantCode)
198 }
199 if gotMsg, wantMsg := err.Error(), "no targets to pick from"; !strings.Contains(gotMsg, wantMsg) {
200 t.Errorf("EmptyCall() failed with message = %q, want to contain %q", gotMsg, wantMsg)
201 }
202 if gotState, wantState := cc.GetState(), connectivity.TransientFailure; gotState != wantState {
203 t.Errorf("cc.GetState() = %v, want %v", gotState, wantState)
204 }
205}
206
207// TestWeightedTarget covers the cases that a sub-balancer is added and a
208// sub-balancer is removed. It verifies that the addresses and balancer configs

Callers

nothing calls this directly

Calls 12

EmptyCallMethod · 0.95
WithTransportCredentialsFunction · 0.92
NewCredentialsFunction · 0.92
WithDefaultServiceConfigFunction · 0.92
NewClientFunction · 0.92
CodeFunction · 0.92
FatalfMethod · 0.65
CloseMethod · 0.65
ConnectMethod · 0.65
ErrorMethod · 0.65
ErrorfMethod · 0.65
GetStateMethod · 0.65

Tested by

no test coverage detected