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

Function setupGRPCServer

test/xds/xds_server_integration_test.go:86–149  ·  view source on GitHub ↗

setupGRPCServer performs the following: - spin up an xDS-enabled gRPC server, configure it with xdsCredentials and register the test service on it - create a local TCP listener and start serving on it Returns the following: - local listener on which the xDS-enabled gRPC server is serving on - clean

(t *testing.T, bootstrapContents []byte, opts ...grpc.ServerOption)

Source from the content-addressed store, hash-verified

84// - local listener on which the xDS-enabled gRPC server is serving on
85// - cleanup function to be invoked by the tests when done
86func setupGRPCServer(t *testing.T, bootstrapContents []byte, opts ...grpc.ServerOption) (net.Listener, func()) {
87 t.Helper()
88
89 // Configure xDS credentials to be used on the server-side.
90 creds, err := xdscreds.NewServerCredentials(xdscreds.ServerOptions{
91 FallbackCreds: insecure.NewCredentials(),
92 })
93 if err != nil {
94 t.Fatal(err)
95 }
96
97 // Initialize a test gRPC server, assign it to the stub server, and start
98 // the test service.
99 stub := &stubserver.StubServer{
100 EmptyCallF: func(context.Context, *testpb.Empty) (*testpb.Empty, error) {
101 return &testpb.Empty{}, nil
102 },
103 UnaryCallF: func(context.Context, *testpb.SimpleRequest) (*testpb.SimpleResponse, error) {
104 return &testpb.SimpleResponse{}, nil
105 },
106 FullDuplexCallF: func(stream testgrpc.TestService_FullDuplexCallServer) error {
107 for {
108 _, err := stream.Recv() // hangs here forever if stream doesn't shut down...doesn't receive EOF without any errors
109 if err == io.EOF {
110 return nil
111 }
112 }
113 },
114 }
115
116 opts = append([]grpc.ServerOption{
117 grpc.Creds(creds),
118 testModeChangeServerOption(t),
119 xds.BootstrapContentsForTesting(bootstrapContents),
120 }, opts...)
121 if stub.S, err = xds.NewGRPCServer(opts...); err != nil {
122 t.Fatalf("Failed to create an xDS enabled gRPC server: %v", err)
123 }
124
125 // Create a local listener and pass it to Serve().
126 lis, err := testutils.LocalTCPListener()
127 if err != nil {
128 t.Fatalf("testutils.LocalTCPListener() failed: %v", err)
129 }
130
131 readyLis := &acceptNotifyingListener{
132 Listener: lis,
133 serverReady: *grpcsync.NewEvent(),
134 }
135
136 stub.Listener = readyLis
137 stubserver.StartTestService(t, stub)
138
139 // Wait for the server to start running.
140 select {
141 case <-readyLis.serverReady.Done():
142 case <-time.After(defaultTestTimeout):
143 t.Fatalf("Timed out while waiting for the backend server to start serving")

Calls 13

NewCredentialsFunction · 0.92
CredsFunction · 0.92
NewGRPCServerFunction · 0.92
LocalTCPListenerFunction · 0.92
NewEventFunction · 0.92
StartTestServiceFunction · 0.92
FatalMethod · 0.65
RecvMethod · 0.65
FatalfMethod · 0.65
StopMethod · 0.65

Tested by

no test coverage detected