Repository: grpc/grpc-go
File: interop/test_utils.go (Go)

Code before:
	Latencies      *stats.Histogram
}

// ChannelFunc can be used to customize how the channel is handled (reuse or reset)
type ChannelFunc func(*grpc.ClientConn) (*grpc.ClientConn, testgrpc.TestServiceClient)

// createChannel Initialize the shared channel (once for all threads)
func createChannel(serverAddr string, dopts []grpc.DialOption) (*grpc.ClientConn, testgrpc.TestServiceClient) {
	conn, err := grpc.NewClient(serverAddr, dopts...)
	if err != nil {
		log.Fatalf("Failed to create shared channel: %v", err)
	}
	client := testgrpc.NewTestServiceClient(conn)
	return conn, client
}

func closeChannel(channel *grpc.ClientConn) {
	if channel != nil {
		err := channel.Close()
		if err != nil {
			log.Fatalf("Failed to close channel: %v", err)
		}
	}
}

// CreateNewChannel Create a new channel by shutting down the current one (for channel soak tests)
func CreateNewChannel(currentChannel *grpc.ClientConn, serverAddr string, dopts []grpc.DialOption) (*grpc.ClientConn, testgrpc.TestServiceClient) {
	closeChannel(currentChannel)
	conn, client := createChannel(serverAddr, dopts)
	return conn, client
}

// UseSharedChannel Reuses the provided currentChannel
func UseSharedChannel(currentChannel *grpc.ClientConn) (*grpc.ClientConn, testgrpc.TestServiceClient) {
	client := testgrpc.NewTestServiceClient(currentChannel)
	return currentChannel, client
}

func doOneSoakIteration(
	ctx context.Context,
	client testgrpc.TestServiceClient,
	soakRequestSize int,
	soakResponseSize int,
	copts []grpc.CallOption) (SoakIterationResult, error) {
	start := time.Now()
	var err error
	// Do a large-unary RPC
	// Create the request payload
	pl := ClientNewPayload(testpb.PayloadType_COMPRESSABLE, soakRequestSize)
	req := &testpb.SimpleRequest{
		ResponseType: testpb.PayloadType_COMPRESSABLE,

Review comment (suggestion):
Nit: The comments for functions (here and elsewhere) should be of the form: `// CreateFoo creates a new Foo by bar baz.`.

Code after:
// settings and does not need to be provided.  If multiple opts are provided,
// only the first one is used.
func NewTestServer(opts ...NewTestServerOptions) testgrpc.TestServiceServer {
	if len(opts) > 0 {
		return &testServer{metricsRecorder: opts[0].MetricsRecorder}
	}
	return &testServer{}
}

func (s *testServer) EmptyCall(context.Context, *testpb.Empty) (*testpb.Empty, error) {
	return new(testpb.Empty), nil
}

func serverNewPayload(t testpb.PayloadType, size int32) (*testpb.Payload, error) {
	if size < 0 {
		return nil, fmt.Errorf("requested a response with invalid length %d", size)
	}
	body := make([]byte, size)
	switch t {
	case testpb.PayloadType_COMPRESSABLE:
	default:
		return nil, fmt.Errorf("unsupported payload type: %d", t)
	}
	return &testpb.Payload{
		Type: t,
		Body: body,
	}, nil
}

func (s *testServer) UnaryCall(ctx context.Context, in *testpb.SimpleRequest) (*testpb.SimpleResponse, error) {
	st := in.GetResponseStatus()
	if md, ok := metadata.FromIncomingContext(ctx); ok {
		if initialMetadata, ok := md[initialMetadataKey]; ok {
			header := metadata.Pairs(initialMetadataKey, initialMetadata[0])
			grpc.SendHeader(ctx, header)
		}
		if trailingMetadata, ok := md[trailingMetadataKey]; ok {
			trailer := metadata.Pairs(trailingMetadataKey, trailingMetadata[0])
			grpc.SetTrailer(ctx, trailer)
		}
	}
	if st != nil && st.Code != 0 {
		return nil, status.Error(codes.Code(st.Code), st.Message)
	}
	pl, err := serverNewPayload(in.GetResponseType(), in.GetResponseSize())
	if err != nil {
		return nil, err
	}
	if r, orcaData := orca.CallMetricsRecorderFromContext(ctx), in.GetOrcaPerQueryReport(); r != nil && orcaData != nil {
		// Transfer the request's per-Call ORCA data to the call metrics
		// recorder in the context, if present.