forked from peterlabuschagne/jettison
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
86 lines (72 loc) · 1.71 KB
/
client.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
78
79
80
81
82
83
84
85
86
package testgrpc
import (
"context"
"testing"
"google.golang.org/grpc"
"github.com/Zulzi/jettison/errors"
jetgrpc "github.com/Zulzi/jettison/grpc"
"github.com/Zulzi/jettison/grpc/test/testpb"
)
type Client struct {
cl testpb.TestClient
conn *grpc.ClientConn
}
func NewClient(t *testing.T, addr string) (*Client, error) {
conn, err := grpc.Dial(addr, grpc.WithInsecure(),
grpc.WithUnaryInterceptor(jetgrpc.UnaryClientInterceptor),
grpc.WithStreamInterceptor(jetgrpc.StreamClientInterceptor))
if err != nil {
return nil, err
}
return &Client{
cl: testpb.NewTestClient(conn),
conn: conn,
}, nil
}
func (cl *Client) Close() error {
return cl.conn.Close()
}
func (cl *Client) ClientPB() testpb.TestClient {
return cl.cl
}
func (cl *Client) ErrorWithCode(ctx context.Context, code string) error {
_, err := cl.cl.ErrorWithCode(ctx,
&testpb.ErrorWithCodeRequest{
Code: code,
})
return err
}
func (cl *Client) WrapErrorWithCode(code string, wraps int64) error {
_, err := cl.cl.WrapErrorWithCode(context.Background(),
&testpb.WrapErrorWithCodeRequest{
Code: code,
Wraps: wraps,
})
return err
}
func (cl *Client) StreamThenError(count int, code string) (int, error) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
sc, err := cl.cl.StreamThenError(ctx,
&testpb.StreamRequest{
Code: code,
ResponseCount: int64(count),
})
if err != nil {
return 0, err
}
var empties int
for i := 0; i < count; i++ {
_, err := sc.Recv()
if err != nil {
return i, errors.Wrap(err, "unexpected error")
}
empties++
}
// Expect the next call to error with code.
_, err = sc.Recv()
if err != nil {
return empties, err
}
return empties + 1, nil
}