-
Notifications
You must be signed in to change notification settings - Fork 3
/
client_test.go
205 lines (184 loc) · 4.74 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package exposed
import (
"bufio"
"fmt"
"io"
"net"
"strings"
"testing"
"time"
"github.com/valyala/fasthttp/fasthttputil"
)
func TestClientNoServer(t *testing.T) {
c := NewClient("", ClientDialer(func(addr string) (net.Conn, error) {
return nil, fmt.Errorf("no server")
}))
c.NewResponse = newTestResponse
const iterations = 100
deadline := time.Now().Add(50 * time.Millisecond)
resultCh := make(chan error, iterations)
for i := 0; i < iterations; i++ {
go func() {
var req request
var resp response
req.SwapPayload([]byte("foobar"))
resultCh <- c.DoDeadline(&req, &resp, deadline)
}()
}
for i := 0; i < iterations; i++ {
var err error
select {
case err = <-resultCh:
case <-time.After(time.Second):
t.Fatalf("timeout")
}
if err == nil {
t.Fatalf("expecting error on iteration %d", i)
}
switch {
case err == ErrTimeout:
case strings.Contains(err.Error(), "no server"):
default:
t.Fatalf("unexpected error on iteration %d: %s", i, err)
}
}
}
func TestClientTimeout(t *testing.T) {
dialCh := make(chan struct{})
c := NewClient("addr",
ClientDialer(func(addr string) (net.Conn, error) {
<-dialCh
return nil, fmt.Errorf("no dial")
}),
)
c.NewResponse = newTestResponse
const iterations = 100
deadline := time.Now().Add(50 * time.Millisecond)
resultCh := make(chan error, iterations)
for i := 0; i < iterations; i++ {
go func() {
var req request
var resp response
req.SwapPayload([]byte("foobar"))
resultCh <- c.DoDeadline(&req, &resp, deadline)
}()
}
for i := 0; i < iterations; i++ {
var err error
select {
case err = <-resultCh:
case <-time.After(time.Second):
t.Fatalf("timeout")
}
if err == nil {
t.Fatalf("expecting error on iteration %d", i)
}
switch {
case err == ErrTimeout:
default:
t.Fatalf("unexpected error on iteration %d: %s", i, err)
}
}
close(dialCh)
}
func TestClientBrokenServerCloseConn(t *testing.T) {
testClientBrokenServer(t, func(conn net.Conn) error {
err := conn.Close()
if err != nil {
err = fmt.Errorf("cannot close client connection: %s", err)
}
return err
})
}
func TestClientBrokenServerGarbageResponse(t *testing.T) {
testClientBrokenServer(t, func(conn net.Conn) error {
_, err := conn.Write([]byte("garbage\naaaa"))
if err != nil {
err = fmt.Errorf("cannot send garbage to the client: %s", err)
}
return err
})
}
func TestClientBrokenServerCheckRequest(t *testing.T) {
testClientBrokenServer(t, func(conn net.Conn) error {
var reqID [4]byte
_, err := io.ReadFull(conn, reqID[:])
if err != nil {
return fmt.Errorf("cannot read reqID from the client: %s", err)
}
var req request
br := bufio.NewReader(conn)
if err = req.ReadRequest(br); err != nil {
return fmt.Errorf("cannot read request from the client: %s", err)
}
if string(req.Payload()) != "foobar" {
return fmt.Errorf("invalid request: %q. Expecting %q", req.Payload(), "foobar")
}
if _, err = conn.Write(reqID[:]); err != nil {
return fmt.Errorf("cannot send reqID to the client: %s", err)
}
if _, err = conn.Write([]byte("invalid\nhttp\nresponse")); err != nil {
return fmt.Errorf("cannot send invalid http response to the client: %s", err)
}
if err = conn.Close(); err != nil {
return fmt.Errorf("cannot close client connection: %s", err)
}
return nil
})
}
func testClientBrokenServer(t *testing.T, serverConnFunc func(net.Conn) error) {
ln := fasthttputil.NewInmemoryListener()
c := NewClient("addr",
ClientDialer(func(addr string) (net.Conn, error) {
return ln.Dial()
}),
ClientCompression(CompressNone),
)
c.ProtocolVersion = 123
c.SniffHeader = "xxxxsss"
c.NewResponse = newTestResponse
serverStopCh := make(chan error, 1)
go func() {
conn, err := ln.Accept()
if err != nil {
serverStopCh <- err
return
}
cfg := &handshakeConfig{
conn: conn,
writeCompressType: c.opts.CompressType,
protocolVersion: c.ProtocolVersion,
sniffHeader: []byte(c.SniffHeader),
}
readCompressType, _, realConn, err := handshakeServer(cfg)
if err != nil {
serverStopCh <- err
return
}
if readCompressType != c.opts.CompressType {
serverStopCh <- fmt.Errorf("unexpected read CompressType: %v. Expecting %v", readCompressType, c.opts.CompressType)
return
}
serverStopCh <- serverConnFunc(realConn)
}()
var req request
var resp response
req.SwapPayload([]byte("foobar"))
err := c.DoDeadline(&req, &resp, time.Now().Add(50*time.Millisecond))
if err == nil {
t.Fatalf("expecting error")
}
// wait for the server
ln.Close()
select {
case err := <-serverStopCh:
if err != nil {
t.Fatalf("error on the server: %s", err)
}
case <-time.After(time.Second):
t.Fatalf("timeout")
}
}
func newTestResponse() responseReader {
return &response{}
}