github.com/jhump/protoreflect@v1.16.0/internal/testutil/testservice.go (about) 1 package testutil 2 3 import ( 4 "context" 5 "io" 6 7 "google.golang.org/protobuf/types/known/emptypb" 8 9 grpc_testing "github.com/jhump/protoreflect/internal/testprotos/grpc" 10 ) 11 12 // TestService is a very simple test service that just echos back request payloads 13 type TestService struct { 14 grpc_testing.UnimplementedTestServiceServer 15 } 16 17 // EmptyCall satisfies the grpc_testing.TestServiceServer interface. It always succeeds. 18 func (TestService) EmptyCall(context.Context, *emptypb.Empty) (*emptypb.Empty, error) { 19 return &emptypb.Empty{}, nil 20 } 21 22 // UnaryCall satisfies the grpc_testing.TestServiceServer interface. It always succeeds, echoing 23 // back the payload present in the request. 24 func (TestService) UnaryCall(_ context.Context, req *grpc_testing.SimpleRequest) (*grpc_testing.SimpleResponse, error) { 25 return &grpc_testing.SimpleResponse{ 26 Payload: req.Payload, 27 }, nil 28 } 29 30 // StreamingOutputCall satisfies the grpc_testing.TestServiceServer interface. It only fails if the 31 // client cancels or disconnects (thus causing ss.Send to return an error). It echoes a number of 32 // responses equal to the request's number of response parameters. The requested parameter details, 33 // however, ignored. The response payload is always an echo of the request payload. 34 func (TestService) StreamingOutputCall(req *grpc_testing.StreamingOutputCallRequest, ss grpc_testing.TestService_StreamingOutputCallServer) error { 35 for i := 0; i < len(req.GetResponseParameters()); i++ { 36 ss.Send(&grpc_testing.StreamingOutputCallResponse{ 37 Payload: req.Payload, 38 }) 39 } 40 return nil 41 } 42 43 // StreamingInputCall satisfies the grpc_testing.TestServiceServer interface. It always succeeds, 44 // sending back the total observed size of all request payloads. 45 func (TestService) StreamingInputCall(ss grpc_testing.TestService_StreamingInputCallServer) error { 46 sz := 0 47 for { 48 req, err := ss.Recv() 49 if err == io.EOF { 50 break 51 } 52 if err != nil { 53 return err 54 } 55 sz += len(req.Payload.GetBody()) 56 } 57 return ss.SendAndClose(&grpc_testing.StreamingInputCallResponse{ 58 AggregatedPayloadSize: int32(sz), 59 }) 60 } 61 62 // FullDuplexCall satisfies the grpc_testing.TestServiceServer interface. It only fails if the 63 // client cancels or disconnects (thus causing ss.Send to return an error). For each request 64 // message it receives, it sends back a response message with the same payload. 65 func (TestService) FullDuplexCall(ss grpc_testing.TestService_FullDuplexCallServer) error { 66 for { 67 req, err := ss.Recv() 68 if err == io.EOF { 69 return nil 70 } 71 if err != nil { 72 return err 73 } 74 75 err = ss.Send(&grpc_testing.StreamingOutputCallResponse{ 76 Payload: req.Payload, 77 }) 78 if err != nil { 79 return err 80 } 81 } 82 } 83 84 // HalfDuplexCall satisfies the grpc_testing.TestServiceServer interface. It only fails if the 85 // client cancels or disconnects (thus causing ss.Send to return an error). For each request 86 // message it receives, it sends back a response message with the same payload. But since it is 87 // half-duplex, all of the request payloads are buffered and responses will only be sent after 88 // the request stream is half-closed. 89 func (TestService) HalfDuplexCall(ss grpc_testing.TestService_HalfDuplexCallServer) error { 90 var data []*grpc_testing.Payload 91 for { 92 req, err := ss.Recv() 93 if err == io.EOF { 94 break 95 } 96 if err != nil { 97 return err 98 } 99 data = append(data, req.Payload) 100 } 101 102 for _, d := range data { 103 err := ss.Send(&grpc_testing.StreamingOutputCallResponse{ 104 Payload: d, 105 }) 106 if err != nil { 107 return err 108 } 109 } 110 return nil 111 }