-
Notifications
You must be signed in to change notification settings - Fork 16
/
net_test.go
174 lines (155 loc) · 3.83 KB
/
net_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
package dnscache
import (
"context"
"errors"
"fmt"
"math/rand"
"net"
"testing"
"time"
)
func TestDialFunc(t *testing.T) {
resolver := &Resolver{
cache: map[string][]net.IP{
"deeeet.com": {
net.IP("127.0.0.1"),
net.IP("127.0.0.2"),
net.IP("127.0.0.3"),
},
},
}
cases := []struct {
permF func(n int) []int
dialF dialFunc
}{
{
permF: func(n int) []int {
return []int{0}
},
dialF: func(ctx context.Context, network, addr string) (net.Conn, error) {
if got, want := addr, net.JoinHostPort(net.IP("127.0.0.1").String(), "443"); got != want {
t.Fatalf("got addr %q, want %q", got, want)
}
return nil, nil
},
},
{
permF: func(n int) []int {
return []int{1}
},
dialF: func(ctx context.Context, network, addr string) (net.Conn, error) {
if got, want := addr, net.JoinHostPort(net.IP("127.0.0.2").String(), "443"); got != want {
t.Fatalf("got addr %q, want %q", got, want)
}
return nil, nil
},
},
{
permF: func(n int) []int {
return []int{2}
},
dialF: func(ctx context.Context, network, addr string) (net.Conn, error) {
if got, want := addr, net.JoinHostPort(net.IP("127.0.0.3").String(), "443"); got != want {
t.Fatalf("got addr %q, want %q", got, want)
}
return nil, nil
},
},
}
origFunc := randPerm
defer func() {
randPerm = origFunc
}()
for n, tc := range cases {
t.Run(fmt.Sprintf("%d", n), func(t *testing.T) {
randPerm = tc.permF
if _, err := DialFunc(resolver, tc.dialF)(context.Background(), "tcp", "deeeet.com:443"); err != nil {
t.Fatalf("err: %s", err)
}
})
}
}
func TestDialFuncRand(t *testing.T) {
rand.Seed(time.Now().UTC().UnixNano())
defer func() {
rand.Seed(1)
}()
resolver := &Resolver{
cache: map[string][]net.IP{
"deeeet.com": {
net.IP("127.0.0.1"),
net.IP("127.0.0.2"),
net.IP("127.0.0.3"),
},
},
}
count := make(map[string]int)
dialF := func(ctx context.Context, network, addr string) (net.Conn, error) {
count[addr]++
return nil, nil
}
for i := 0; i < 100; i++ {
if _, err := DialFunc(resolver, dialF)(context.Background(), "tcp", "deeeet.com:443"); err != nil {
t.Fatalf("err: %s", err)
}
}
for _, c := range count {
got := float32(c) / float32(100)
if got < float32(0.2) {
t.Fatalf("expect rate more than 0.2, got %f", got)
}
}
}
func TestDialFuncError1(t *testing.T) {
resolver := testResolver(t)
if _, err := DialFunc(resolver, nil)(context.Background(), "tcp", "deeeet.jp"); err == nil {
t.Fatalf("expect to be failed") // need to specify port
}
}
func TestDialFuncError2(t *testing.T) {
originalFunc := lookupIP
defer func() {
lookupIP = originalFunc
}()
lookupIP = func(ctx context.Context, host string) ([]net.IP, error) {
return nil, fmt.Errorf("err")
}
if _, err := DialFunc(testResolver(t), nil)(context.Background(), "tcp", "tcnksm.io:443"); err == nil {
t.Fatalf("expect to be failed")
}
}
func TestDialFuncError3(t *testing.T) {
resolver := &Resolver{
cache: map[string][]net.IP{
"tcnksm.io": {
net.IP("1.1.1.1"),
net.IP("2.2.2.2"),
net.IP("3.3.3.3"),
},
},
}
origFunc := randPerm
randPerm = func(n int) []int {
return []int{0, 1, 2}
}
defer func() {
randPerm = origFunc
}()
want := errors.New("error1")
dialF := func(ctx context.Context, network, addr string) (net.Conn, error) {
if addr == net.JoinHostPort(net.IP("1.1.1.1").String(), "443") {
return nil, want // first error should be returned
}
if addr == net.JoinHostPort(net.IP("2.2.2.2").String(), "443") {
return nil, fmt.Errorf("error2")
}
if addr == net.JoinHostPort(net.IP("3.3.3.3").String(), "443") {
return nil, fmt.Errorf("error3")
}
return nil, nil
}
_, got := DialFunc(resolver, dialF)(context.Background(), "tcp", "tcnksm.io:443")
if got != want {
t.Fatalf("got error %v, want %v", got, want)
}
}