Repository: grpc/grpc-go
File: xds/internal/httpfilter/fault/fault_test.go (Go)

Code before:
// - spin up an xDS management server on a local port
// - spin up a gRPC server and register the test service on it
// - create a local TCP listener and start serving on it
//
// Returns the following:
//   - the management server: tests use this to configure resources
//   - nodeID expected by the management server: this is set in the Node proto
//     sent by the xdsClient for queries.
//   - the port the server is listening on
//   - cleanup function to be invoked by the tests when done
func clientSetup(t *testing.T) (*e2e.ManagementServer, string, uint32, func()) {
	// Spin up a xDS management server on a local port.
	nodeID := uuid.New().String()
	managementServer := e2e.StartManagementServer(t, e2e.ManagementServerOptions{})

	// Create a bootstrap file in a temporary directory.
	bootstrapContents := e2e.DefaultBootstrapContents(t, nodeID, managementServer.Address)
	testutils.CreateBootstrapFileForTesting(t, bootstrapContents)

	// Initialize a test gRPC server, assign it to the stub server, and start the test service.
	stub := &stubserver.StubServer{
		EmptyCallF: func(context.Context, *testpb.Empty) (*testpb.Empty, error) {
			return &testpb.Empty{}, nil
		},
		FullDuplexCallF: func(stream testgrpc.TestService_FullDuplexCallServer) error {
			for {
				if _, err := stream.Recv(); err == io.EOF {
					return nil
				} else if err != nil {
					return err
				}
			}
		},
	}

	server := grpc.NewServer()

	stub.S = server
	stubserver.StartTestService(t, stub)

	// Create a local listener and pass it to Serve().
	lis, err := testutils.LocalTCPListener()
	if err != nil {
		t.Fatalf("testutils.LocalTCPListener() failed: %v", err)
	}

	go func() {
		if err := server.Serve(lis); err != nil {
			t.Errorf("Serve() failed: %v", err)
		}
	}()

Review comment (suggestion):
```suggestion
	// Initialize a test gRPC server, assign it to the stub server, and start
	// the test service.
```

Code after:
// - spin up an xDS management server on a local port
// - spin up a gRPC server and register the test service on it
// - create a local TCP listener and start serving on it
//
// Returns the following:
//   - the management server: tests use this to configure resources
//   - nodeID expected by the management server: this is set in the Node proto
//     sent by the xdsClient for queries.
//   - the port the server is listening on
//   - cleanup function to be invoked by the tests when done
func clientSetup(t *testing.T) (*e2e.ManagementServer, string, uint32) {
	// Spin up a xDS management server on a local port.
	nodeID := uuid.New().String()
	managementServer := e2e.StartManagementServer(t, e2e.ManagementServerOptions{})

	// Create a bootstrap file in a temporary directory.
	bootstrapContents := e2e.DefaultBootstrapContents(t, nodeID, managementServer.Address)
	testutils.CreateBootstrapFileForTesting(t, bootstrapContents)

	// Create a local listener.
	lis, err := testutils.LocalTCPListener()
	if err != nil {
		t.Fatalf("testutils.LocalTCPListener() failed: %v", err)
	}

	// Initialize a test gRPC server, assign it to the stub server, and start the test service.
	stub := &stubserver.StubServer{
		Listener: lis,
		EmptyCallF: func(context.Context, *testpb.Empty) (*testpb.Empty, error) {
			return &testpb.Empty{}, nil
		},
		FullDuplexCallF: func(stream testgrpc.TestService_FullDuplexCallServer) error {
			// End RPC after client does a CloseSend.
			for {
				if _, err := stream.Recv(); err == io.EOF {
					return nil
				} else if err != nil {
					return err
				}
			}
		},
	}

	stubserver.StartTestService(t, stub)
	t.Cleanup(stub.S.Stop)
	return managementServer, nodeID, uint32(lis.Addr().(*net.TCPAddr).Port)
}

func (s) TestFaultInjection_Unary(t *testing.T) {
	type subcase struct {
		name   string