gitee.com/ks-custle/core-gm@v0.0.0-20230922171213-b83bdd97b62c/go-grpc-middleware/testing/pingservice.go (about) 1 // Copyright 2016 Michal Witkowski. All Rights Reserved. 2 // See LICENSE for licensing terms. 3 4 /* 5 Package `grpc_testing` provides helper functions for testing validators in this package. 6 */ 7 8 package grpc_testing 9 10 import ( 11 "io" 12 "testing" 13 14 pb_testproto "gitee.com/ks-custle/core-gm/go-grpc-middleware/testing/testproto" 15 16 "gitee.com/ks-custle/core-gm/grpc" 17 "gitee.com/ks-custle/core-gm/grpc/codes" 18 "gitee.com/ks-custle/core-gm/net/context" 19 ) 20 21 const ( 22 // DefaultPongValue is the default value used. 23 DefaultResponseValue = "default_response_value" 24 // ListResponseCount is the expeted number of responses to PingList 25 ListResponseCount = 100 26 ) 27 28 type TestPingService struct { 29 T *testing.T 30 } 31 32 func (s *TestPingService) PingEmpty(ctx context.Context, _ *pb_testproto.Empty) (*pb_testproto.PingResponse, error) { 33 return &pb_testproto.PingResponse{Value: DefaultResponseValue, Counter: 42}, nil 34 } 35 36 func (s *TestPingService) Ping(ctx context.Context, ping *pb_testproto.PingRequest) (*pb_testproto.PingResponse, error) { 37 // Send user trailers and headers. 38 return &pb_testproto.PingResponse{Value: ping.Value, Counter: 42}, nil 39 } 40 41 func (s *TestPingService) PingError(ctx context.Context, ping *pb_testproto.PingRequest) (*pb_testproto.Empty, error) { 42 code := codes.Code(ping.ErrorCodeReturned) 43 return nil, grpc.Errorf(code, "Userspace error.") 44 } 45 46 func (s *TestPingService) PingList(ping *pb_testproto.PingRequest, stream pb_testproto.TestService_PingListServer) error { 47 if ping.ErrorCodeReturned != 0 { 48 return grpc.Errorf(codes.Code(ping.ErrorCodeReturned), "foobar") 49 } 50 // Send user trailers and headers. 51 for i := 0; i < ListResponseCount; i++ { 52 stream.Send(&pb_testproto.PingResponse{Value: ping.Value, Counter: int32(i)}) 53 } 54 return nil 55 } 56 57 func (s *TestPingService) PingStream(stream pb_testproto.TestService_PingStreamServer) error { 58 count := 0 59 for true { 60 ping, err := stream.Recv() 61 if err == io.EOF { 62 break 63 } 64 if err != nil { 65 return err 66 } 67 stream.Send(&pb_testproto.PingResponse{Value: ping.Value, Counter: int32(count)}) 68 count += 1 69 } 70 return nil 71 }