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

Function setupPickFirst

balancer/pickfirst/pickfirst_ext_test.go:106–150  ·  view source on GitHub ↗

setupPickFirst performs steps required for pick_first tests. It starts a bunch of backends exporting the TestService, creates a ClientConn to them with service config specifying the use of the pick_first LB policy.

(t *testing.T, backendCount int, opts ...grpc.DialOption)

Source from the content-addressed store, hash-verified

104// bunch of backends exporting the TestService, creates a ClientConn to them
105// with service config specifying the use of the pick_first LB policy.
106func setupPickFirst(t *testing.T, backendCount int, opts ...grpc.DialOption) (*grpc.ClientConn, *manual.Resolver, []*stubserver.StubServer) {
107 t.Helper()
108
109 r := manual.NewBuilderWithScheme("whatever")
110
111 backends := make([]*stubserver.StubServer, backendCount)
112 addrs := make([]resolver.Address, backendCount)
113 for i := 0; i < backendCount; i++ {
114 backend := &stubserver.StubServer{
115 EmptyCallF: func(context.Context, *testpb.Empty) (*testpb.Empty, error) {
116 return &testpb.Empty{}, nil
117 },
118 }
119 if err := backend.StartServer(); err != nil {
120 t.Fatalf("Failed to start backend: %v", err)
121 }
122 t.Logf("Started TestService backend at: %q", backend.Address)
123 t.Cleanup(func() { backend.Stop() })
124
125 backends[i] = backend
126 addrs[i] = resolver.Address{Addr: backend.Address}
127 }
128
129 dopts := []grpc.DialOption{
130 grpc.WithTransportCredentials(insecure.NewCredentials()),
131 grpc.WithResolvers(r),
132 grpc.WithDefaultServiceConfig(pickFirstServiceConfig),
133 }
134 dopts = append(dopts, opts...)
135 cc, err := grpc.NewClient(r.Scheme()+":///test.server", dopts...)
136 if err != nil {
137 t.Fatalf("grpc.NewClient() failed: %v", err)
138 }
139 t.Cleanup(func() { cc.Close() })
140
141 // At this point, the resolver has not returned any addresses to the channel.
142 // This RPC must block until the context expires.
143 sCtx, sCancel := context.WithTimeout(context.Background(), defaultTestShortTimeout)
144 defer sCancel()
145 client := testgrpc.NewTestServiceClient(cc)
146 if _, err := client.EmptyCall(sCtx, &testpb.Empty{}); status.Code(err) != codes.DeadlineExceeded {
147 t.Fatalf("EmptyCall() = %s, want %s", status.Code(err), codes.DeadlineExceeded)
148 }
149 return cc, r, backends
150}
151
152// stubBackendsToResolverAddrs converts from a set of stub server backends to
153// resolver addresses. Useful when pushing addresses to the manual resolver.

Calls 14

StartServerMethod · 0.95
StopMethod · 0.95
EmptyCallMethod · 0.95
NewBuilderWithSchemeFunction · 0.92
WithTransportCredentialsFunction · 0.92
NewCredentialsFunction · 0.92
WithResolversFunction · 0.92
WithDefaultServiceConfigFunction · 0.92
NewClientFunction · 0.92
CodeFunction · 0.92
FatalfMethod · 0.65
LogfMethod · 0.65

Tested by

no test coverage detected