github.com/google/martian/v3@v3.3.3/h2/testing/test_service.go (about)

     1  // Copyright 2021 Google Inc. All rights reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package testing
    16  
    17  import (
    18  	"context"
    19  	"io"
    20  
    21  	tspb "github.com/google/martian/v3/h2/testservice"
    22  )
    23  
    24  // Server is a testing gRPC server.
    25  type Server struct {
    26  	tspb.UnimplementedTestServiceServer
    27  }
    28  
    29  // Echo handles TestService.Echo RPCs.
    30  func (s *Server) Echo(ctx context.Context, in *tspb.EchoRequest) (*tspb.EchoResponse, error) {
    31  	return &tspb.EchoResponse{
    32  		Payload: in.GetPayload(),
    33  	}, nil
    34  }
    35  
    36  // Sum handles TestService.Sum RPCs.
    37  func (s *Server) Sum(_ context.Context, in *tspb.SumRequest) (*tspb.SumResponse, error) {
    38  	sum := int32(0)
    39  	for _, v := range in.GetValues() {
    40  		sum += v
    41  	}
    42  	return &tspb.SumResponse{
    43  		Value: sum,
    44  	}, nil
    45  }
    46  
    47  // DoubleEcho handles TestService.DoubleEcho RPCs.
    48  func (s *Server) DoubleEcho(stream tspb.TestService_DoubleEchoServer) error {
    49  	for {
    50  		req, err := stream.Recv()
    51  		if err == io.EOF {
    52  			return nil
    53  		}
    54  		if err != nil {
    55  			return err
    56  		}
    57  		resp := &tspb.EchoResponse{
    58  			Payload: req.GetPayload(),
    59  		}
    60  		if err := stream.Send(resp); err != nil {
    61  			return err
    62  		}
    63  		if err := stream.Send(resp); err != nil {
    64  			return err
    65  		}
    66  	}
    67  }