...
1
2
3
4
7
8 package grpc_testing
9
10 import (
11 "context"
12 "io"
13 "testing"
14
15 pb_testproto "github.com/grpc-ecosystem/go-grpc-middleware/testing/testproto"
16 "google.golang.org/grpc/codes"
17 "google.golang.org/grpc/status"
18 )
19
20 const (
21
22 DefaultResponseValue = "default_response_value"
23
24 ListResponseCount = 100
25 )
26
27 type TestPingService struct {
28 T *testing.T
29 }
30
31 func (s *TestPingService) PingEmpty(ctx context.Context, _ *pb_testproto.Empty) (*pb_testproto.PingResponse, error) {
32 return &pb_testproto.PingResponse{Value: DefaultResponseValue, Counter: 42}, nil
33 }
34
35 func (s *TestPingService) Ping(ctx context.Context, ping *pb_testproto.PingRequest) (*pb_testproto.PingResponse, error) {
36
37 return &pb_testproto.PingResponse{Value: ping.Value, Counter: 42}, nil
38 }
39
40 func (s *TestPingService) PingError(ctx context.Context, ping *pb_testproto.PingRequest) (*pb_testproto.Empty, error) {
41 code := codes.Code(ping.ErrorCodeReturned)
42 return nil, status.Errorf(code, "Userspace error.")
43 }
44
45 func (s *TestPingService) PingList(ping *pb_testproto.PingRequest, stream pb_testproto.TestService_PingListServer) error {
46 if ping.ErrorCodeReturned != 0 {
47 return status.Errorf(codes.Code(ping.ErrorCodeReturned), "foobar")
48 }
49
50 for i := 0; i < ListResponseCount; i++ {
51 stream.Send(&pb_testproto.PingResponse{Value: ping.Value, Counter: int32(i)})
52 }
53 return nil
54 }
55
56 func (s *TestPingService) PingStream(stream pb_testproto.TestService_PingStreamServer) error {
57 count := 0
58 for true {
59 ping, err := stream.Recv()
60 if err == io.EOF {
61 break
62 }
63 if err != nil {
64 return err
65 }
66 stream.Send(&pb_testproto.PingResponse{Value: ping.Value, Counter: int32(count)})
67 count += 1
68 }
69 return nil
70 }
71
View as plain text