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