forked from grpc-ecosystem/go-grpc-middleware
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pingservice.go
77 lines (62 loc) · 1.88 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
72
73
74
75
76
77
// Copyright (c) The go-grpc-middleware Authors.
// Licensed under the Apache License 2.0.
/*
Package `grpc_testing` provides helper functions for testing validators in this package.
*/
package testpb
import (
"context"
"io"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
const (
// ListResponseCount is the expected number of responses to PingList
ListResponseCount = 100
)
var TestServiceFullName = _TestService_serviceDesc.ServiceName
// Interface implementation assert.
var _ TestServiceServer = &TestPingService{}
type TestPingService struct {
UnimplementedTestServiceServer
}
func (s *TestPingService) PingEmpty(_ context.Context, _ *PingEmptyRequest) (*PingEmptyResponse, error) {
return &PingEmptyResponse{}, nil
}
func (s *TestPingService) Ping(_ context.Context, ping *PingRequest) (*PingResponse, error) {
// Send user trailers and headers.
return &PingResponse{Value: ping.Value, Counter: 0}, nil
}
func (s *TestPingService) PingError(_ context.Context, ping *PingErrorRequest) (*PingErrorResponse, error) {
code := codes.Code(ping.ErrorCodeReturned)
return nil, status.Errorf(code, "Userspace error.")
}
func (s *TestPingService) PingList(ping *PingListRequest, stream TestService_PingListServer) error {
if ping.ErrorCodeReturned != 0 {
return status.Errorf(codes.Code(ping.ErrorCodeReturned), "foobar")
}
// Send user trailers and headers.
for i := 0; i < ListResponseCount; i++ {
if err := stream.Send(&PingListResponse{Value: ping.Value, Counter: int32(i)}); err != nil {
return err
}
}
return nil
}
func (s *TestPingService) PingStream(stream TestService_PingStreamServer) error {
count := 0
for {
ping, err := stream.Recv()
if err == io.EOF {
break
}
if err != nil {
return err
}
if err := stream.Send(&PingStreamResponse{Value: ping.Value, Counter: int32(count)}); err != nil {
return err
}
count += 1
}
return nil
}