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