-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathclient_test.go
108 lines (98 loc) · 2.22 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
package httpteleport
import (
"bytes"
"fmt"
"github.com/valyala/fasthttp"
"net"
"strings"
"testing"
"time"
)
func TestClientBodyStream(t *testing.T) {
c := &Client{
Dial: func(addr string) (net.Conn, error) {
return nil, fmt.Errorf("no server")
},
}
var req fasthttp.Request
var resp fasthttp.Response
req.SetRequestURI("http://foobar/baz")
req.SetBodyStream(bytes.NewBufferString("foobarbaz"), -1)
err := c.DoTimeout(&req, &resp, time.Second)
if err == nil {
t.Fatalf("expecting error")
}
if err != errNoBodyStream {
t.Fatalf("unexpected error: %s. Expecting %s", err, errNoBodyStream)
}
}
func TestClientNoServer(t *testing.T) {
c := &Client{
Dial: func(addr string) (net.Conn, error) {
return nil, fmt.Errorf("no server")
},
}
const iterations = 100
resultCh := make(chan error, iterations)
for i := 0; i < iterations; i++ {
go func() {
var req fasthttp.Request
var resp fasthttp.Response
req.SetRequestURI("http://foobar/baz")
resultCh <- c.DoTimeout(&req, &resp, 50*time.Millisecond)
}()
}
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 := &Client{
Dial: func(addr string) (net.Conn, error) {
<-dialCh
return nil, fmt.Errorf("no dial")
},
}
const iterations = 100
resultCh := make(chan error, iterations)
for i := 0; i < iterations; i++ {
go func() {
var req fasthttp.Request
var resp fasthttp.Response
req.SetRequestURI("http://foobar/baz")
resultCh <- c.DoTimeout(&req, &resp, 50*time.Millisecond)
}()
}
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)
}