-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdialer_test.go
More file actions
161 lines (136 loc) · 3.85 KB
/
dialer_test.go
File metadata and controls
161 lines (136 loc) · 3.85 KB
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
package contextual
import (
"context"
"fmt"
"io"
"net"
"testing"
"time"
)
const (
durShort = 100 * time.Millisecond
durLong = durShort * 2
)
var (
ctxBg = context.Background()
errPass = fmt.Errorf("test passed")
errFail = fmt.Errorf("test failed")
connPass, connFail = net.Pipe()
)
func TestCancelledContext(t *testing.T) {
d := Dialer{&testNeverDial{}}
ctx, cancel := context.WithCancel(ctxBg)
cancel()
_, err := d.DialContext(ctx, "tcp", "127.0.0.1:12345")
if err != context.Canceled {
t.Errorf("got: %#v want: %#v", err, context.Canceled)
}
}
type testNeverDial struct{}
func (d *testNeverDial) Dial(network, address string) (net.Conn, error) {
return nil, errFail
}
func TestDialContext(t *testing.T) {
d := Dialer{&testDialContext{}}
_, err := d.DialContext(ctxBg, "tcp", "127.0.0.1:12345")
if err != errPass {
t.Errorf("got: %#v want: %#v", err, errPass)
}
}
type testDialContext struct{}
func (d *testDialContext) Dial(network, address string) (net.Conn, error) {
return nil, errFail
}
func (d *testDialContext) DialContext(ctx context.Context, network, address string) (net.Conn, error) {
return nil, errPass
}
func TestDialTimeout(t *testing.T) {
d := Dialer{&testDialTimeout{}}
ctx, cancel := context.WithTimeout(ctxBg, durShort)
defer cancel()
_, err := d.DialContext(ctx, "tcp", "127.0.0.1:12345")
if err != errPass {
t.Errorf("got: %#v want: %#v", err, errPass)
}
}
type testDialTimeout struct{}
func (d *testDialTimeout) Dial(network, address string) (net.Conn, error) {
return nil, errFail
}
func (d *testDialTimeout) DialTimeout(network, address string, timeout time.Duration) (net.Conn, error) {
return nil, errPass
}
func TestDialTimeoutNoDeadline(t *testing.T) {
d := Dialer{&testDialTimeoutNoDeadline{}}
ctx, cancel := context.WithCancel(ctxBg)
defer cancel()
_, err := d.DialContext(ctx, "tcp", "127.0.0.1:12345")
if err != errPass {
t.Errorf("got: %#v want: %#v", err, errPass)
}
}
type testDialTimeoutNoDeadline struct{}
func (d *testDialTimeoutNoDeadline) Dial(network, address string) (net.Conn, error) {
return nil, errPass
}
func (d *testDialTimeoutNoDeadline) DialTimeout(network, address string, timeout time.Duration) (net.Conn, error) {
return nil, errFail
}
func TestDialSucceed(t *testing.T) {
d := Dialer{&testDialSucceed{}}
conn, err := d.DialContext(ctxBg, "tcp", "127.0.0.1:12345")
if conn != connPass {
t.Errorf("got: %#v want: %#v", conn, connPass)
}
if err != nil {
t.Errorf("got: %#v want: %#v", err, nil)
}
}
type testDialSucceed struct{}
func (d *testDialSucceed) Dial(network, address string) (net.Conn, error) {
return connPass, nil
}
func TestDialLate(t *testing.T) {
sentinel, _ := net.Pipe()
d := Dialer{&testDialLate{sentinel}}
ctx, cancel := context.WithTimeout(ctxBg, durShort)
defer cancel()
conn, err := d.DialContext(ctx, "tcp", "127.0.0.1:12345")
if conn != nil {
t.Errorf("got: %#v want: %#v", conn, nil)
}
if err != context.DeadlineExceeded {
t.Errorf("got: %#v want: %#v", err, context.DeadlineExceeded)
}
_, err = sentinel.Write([]byte("a"))
if err != io.ErrClosedPipe {
t.Errorf("got: %#v want: %#v", err, io.ErrClosedPipe)
}
}
func TestDialLateCancelled(t *testing.T) {
sentinel, _ := net.Pipe()
d := Dialer{&testDialLate{sentinel}}
ctx, cancel := context.WithCancel(ctxBg)
go func() {
time.Sleep(durShort)
cancel()
}()
conn, err := d.DialContext(ctx, "tcp", "127.0.0.1:12345")
if conn != nil {
t.Errorf("got: %#v want: %#v", conn, nil)
}
if err != context.Canceled {
t.Errorf("got: %#v want: %#v", err, context.Canceled)
}
_, err = sentinel.Write([]byte("a"))
if err != io.ErrClosedPipe {
t.Errorf("got: %#v want: %#v", err, io.ErrClosedPipe)
}
}
type testDialLate struct {
sentinel net.Conn
}
func (d *testDialLate) Dial(network, address string) (net.Conn, error) {
time.Sleep(durLong)
return d.sentinel, nil
}