-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_test.go
176 lines (156 loc) · 4.13 KB
/
client_test.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package mrpc
import (
"bytes"
"context"
"reflect"
"testing"
"time"
"github.com/mprot/msgpack-go"
)
func TestClientCall(t *testing.T) {
request := Request{
Service: "service",
Method: 3,
Headers: RequestHeaders{},
Body: []byte("request body"),
}
response := Response{
ErrorCode: OK,
ErrorText: "error",
Body: []byte("response body"),
}
t.Run("no-timeout", func(t *testing.T) {
ctx := context.Background()
conn := newClientConn(func(req Request) Response {
if !reflect.DeepEqual(req, request) {
t.Fatalf("unexpected request: %#v", req)
}
return response
})
client := NewClient(conn)
resp, err := client.Call(ctx, request)
switch {
case err != nil:
t.Fatalf("unexpected error: %v", err)
case !reflect.DeepEqual(resp, response):
t.Fatalf("unexpected response: %#v", resp)
}
})
t.Run("header-timeout", func(t *testing.T) {
request.Headers.Timeout = 100
ctx := context.Background()
conn := newClientConn(func(req Request) Response {
if !reflect.DeepEqual(req, request) {
t.Fatalf("unexpected request: %#v", req)
}
return response
})
client := NewClient(conn)
resp, err := client.Call(ctx, request)
switch {
case err != nil:
t.Fatalf("unexpected error: %v", err)
case !reflect.DeepEqual(resp, response):
t.Fatalf("unexpected response: %#v", resp)
}
})
t.Run("context-timeout", func(t *testing.T) {
request.Headers.Timeout = 0
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
conn := newClientConn(func(req Request) Response {
if req.Headers.Timeout == 0 {
t.Fatal("unexpected timeout: 0")
}
req.Headers.Timeout = 0
if !reflect.DeepEqual(req, request) {
t.Fatalf("unexpected request: %#v", req)
}
return response
})
client := NewClient(conn)
resp, err := client.Call(ctx, request)
switch {
case err != nil:
t.Fatalf("unexpected error: %v", err)
case !reflect.DeepEqual(resp, response):
t.Fatalf("unexpected response: %#v", resp)
}
})
t.Run("header-and-context-timeout", func(t *testing.T) {
// prefer timeout header
request.Headers.Timeout = uint64(time.Second)
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
conn := newClientConn(func(req Request) Response {
if req.Headers.Timeout != uint64(time.Second) {
t.Fatalf("unexpected timeout: %v", time.Duration(req.Headers.Timeout))
}
if !reflect.DeepEqual(req, request) {
t.Fatalf("unexpected request: %#v", req)
}
return response
})
client := NewClient(conn)
resp, err := client.Call(ctx, request)
switch {
case err != nil:
t.Fatalf("unexpected error: %v", err)
case !reflect.DeepEqual(resp, response):
t.Fatalf("unexpected response: %#v", resp)
}
cancel()
// prefer context timeout
request.Headers.Timeout = uint64(time.Minute)
ctx, cancel = context.WithTimeout(context.Background(), time.Second)
conn = newClientConn(func(req Request) Response {
if req.Headers.Timeout == 0 || req.Headers.Timeout > uint64(time.Minute) {
t.Fatalf("unexpected timeout: %v", time.Duration(req.Headers.Timeout))
}
req.Headers.Timeout = request.Headers.Timeout
if !reflect.DeepEqual(req, request) {
t.Fatalf("unexpected request: %#v", req)
}
return response
})
client = NewClient(conn)
resp, err = client.Call(ctx, request)
switch {
case err != nil:
t.Fatalf("unexpected error: %v", err)
case !reflect.DeepEqual(resp, response):
t.Fatalf("unexpected response: %#v", resp)
}
cancel()
})
}
type clientConn struct {
f func(Request) Response
req []byte
resp []byte
pos int
}
func newClientConn(f func(Request) Response) *clientConn {
return &clientConn{f: f, pos: -1}
}
func (c *clientConn) Read(p []byte) (int, error) {
if c.pos < 0 {
var req Request
err := msgpack.Decode(bytes.NewReader(c.req), &req)
if err != nil {
return 0, err
}
resp := c.f(req)
var buf bytes.Buffer
if err := msgpack.Encode(&buf, &resp); err != nil {
return 0, err
}
c.resp = buf.Bytes()
}
n := copy(p, c.resp)
c.pos += n
return n, nil
}
func (c *clientConn) Write(p []byte) (int, error) {
c.req = append(c.req, p...)
return len(p), nil
}