forked from peterlabuschagne/jettison
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
63 lines (51 loc) · 1.4 KB
/
server.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
package testgrpc
import (
"context"
"net"
"testing"
"google.golang.org/grpc"
"github.com/Zulzi/jettison/errors"
jetgrpc "github.com/Zulzi/jettison/grpc"
"github.com/Zulzi/jettison/grpc/test/testpb"
"github.com/Zulzi/jettison/j"
)
type Server struct{}
func NewServer(t *testing.T, l net.Listener) (*Server, func()) {
grpcSrv := grpc.NewServer(
grpc.UnaryInterceptor(jetgrpc.UnaryServerInterceptor),
grpc.StreamInterceptor(jetgrpc.StreamServerInterceptor))
srv := new(Server)
testpb.RegisterTestServer(grpcSrv, srv)
go func() {
err := grpcSrv.Serve(l)
if err != nil {
panic(err)
}
}()
return srv, func() {
grpcSrv.GracefulStop()
}
}
func (srv *Server) ErrorWithCode(ctx context.Context,
req *testpb.ErrorWithCodeRequest,
) (*testpb.Empty, error) {
return nil, errors.New("error with code", j.C(req.Code), j.KV("HELLO", "WORLD"))
}
func (srv *Server) WrapErrorWithCode(ctx context.Context,
req *testpb.WrapErrorWithCodeRequest,
) (*testpb.Empty, error) {
err := errors.New("wrap error with code", j.C(req.Code))
for i := int64(0); i < req.Wraps; i++ {
err = errors.Wrap(err, "wrap")
}
return nil, err
}
func (srv *Server) StreamThenError(req *testpb.StreamRequest, ss testpb.Test_StreamThenErrorServer) error {
for i := 0; i < int(req.ResponseCount); i++ {
err := ss.Send(&testpb.Empty{})
if err != nil {
return err
}
}
return errors.New("error with code", j.C(req.Code))
}