-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlistener_test.go
173 lines (163 loc) · 4.49 KB
/
listener_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
package listener
import (
"errors"
"net"
"testing"
)
func TestNew(t *testing.T) {
t.Run("Nil", func(t *testing.T) {
l, err := New(nil)
if err != nil {
t.Fatalf("New error: %s", err)
}
defer l.Close()
if l.URL == nil {
t.Errorf("URL wants a URL but was nil")
}
if l.URL.Scheme != "http" {
t.Errorf("Scheme wants http but was %s", l.URL.Scheme)
}
if l.URL.Hostname() != "localhost" {
t.Errorf("Hostname wants localhost but was %s", l.URL.Hostname())
}
t.Logf("URL is %s", l.URL.String())
})
t.Run("Empty", func(t *testing.T) {
l, err := New([]string{})
if err != nil {
t.Fatalf("New error: %s", err)
}
defer l.Close()
if l.URL == nil {
t.Errorf("URL wants a URL but was nil")
}
if l.URL.Scheme != "http" {
t.Errorf("Scheme wants http but was %s", l.URL.Scheme)
}
if l.URL.Hostname() != "localhost" {
t.Errorf("Hostname wants localhost but was %s", l.URL.Hostname())
}
t.Logf("URL is %s", l.URL.String())
})
t.Run("SingleAddress", func(t *testing.T) {
l, err := New([]string{"localhost:9000"})
if err != nil {
t.Fatalf("New error: %s", err)
}
defer l.Close()
if l.URL == nil {
t.Errorf("URL wants a URL but was nil")
}
if l.URL.Scheme != "http" {
t.Errorf("Scheme wants http but was %s", l.URL.Scheme)
}
if l.URL.Hostname() != "localhost" {
t.Errorf("Hostname wants localhost but was %s", l.URL.Hostname())
}
if l.URL.Port() != "9000" {
t.Errorf("Port wants 9000 but was %s", l.URL.Port())
}
})
t.Run("MultipleAddressFallback", func(t *testing.T) {
l1, err := New([]string{"localhost:9000"})
if err != nil {
t.Fatalf("New error: %s", err)
}
defer l1.Close()
if l1.URL == nil {
t.Errorf("URL wants a URL but was nil")
}
if l1.URL.Scheme != "http" {
t.Errorf("Scheme wants http but was %s", l1.URL.Scheme)
}
if l1.URL.Hostname() != "localhost" {
t.Errorf("Hostname wants localhost but was %s", l1.URL.Hostname())
}
if l1.URL.Port() != "9000" {
t.Errorf("Port wants 9000 but was %s", l1.URL.Port())
}
l2, err := New([]string{"localhost:9000", "localhost:9001"})
if err != nil {
t.Fatalf("New error: %s", err)
}
defer l2.Close()
if l2.URL == nil {
t.Errorf("URL wants a URL but was nil")
}
if l2.URL.Scheme != "http" {
t.Errorf("Scheme wants http but was %s", l2.URL.Scheme)
}
if l2.URL.Hostname() != "localhost" {
t.Errorf("Hostname wants localhost but was %s", l2.URL.Hostname())
}
if l2.URL.Port() != "9001" {
t.Errorf("Port wants 9001 but was %s", l2.URL.Port())
}
})
t.Run("MultipleAddressFail", func(t *testing.T) {
l1, err := New([]string{"localhost:9000"})
if err != nil {
t.Fatalf("New error: %s", err)
}
defer l1.Close()
if l1.URL == nil {
t.Errorf("URL wants a URL but was nil")
}
if l1.URL.Scheme != "http" {
t.Errorf("Scheme wants http but was %s", l1.URL.Scheme)
}
if l1.URL.Hostname() != "localhost" {
t.Errorf("Hostname wants localhost but was %s", l1.URL.Hostname())
}
if l1.URL.Port() != "9000" {
t.Errorf("Port wants 9000 but was %s", l1.URL.Port())
}
l2, err := New([]string{"localhost:9001"})
if err != nil {
t.Fatalf("New error: %s", err)
}
defer l2.Close()
if l2.URL == nil {
t.Errorf("URL wants a URL but was nil")
}
if l2.URL.Scheme != "http" {
t.Errorf("Scheme wants http but was %s", l2.URL.Scheme)
}
if l2.URL.Hostname() != "localhost" {
t.Errorf("Hostname wants localhost but was %s", l2.URL.Hostname())
}
if l2.URL.Port() != "9001" {
t.Errorf("Port wants 9001 but was %s", l2.URL.Port())
}
l3, err := New([]string{"localhost:9000", "localhost:9001"})
if err == nil {
l3.Close()
t.Fatalf("New wants error but was nil")
}
t.Logf("expected error: %s", err)
var noAvailablePortErr NoAvailablePortError
if !errors.As(err, &noAvailablePortErr) {
t.Fatalf("error wants NoAvailablePortError but was %T", err)
}
causes := noAvailablePortErr.Unwrap()
if len(causes) != 2 {
t.Fatalf("len(causes) wants 3 but was %d", len(causes))
}
cause1 := causes[0]
var ne1 *net.OpError
if !errors.As(cause1, &ne1) {
t.Fatalf("cause1 wants net.OpError but was %T", errors.Unwrap(cause1))
}
if ne1.Addr.String() != "127.0.0.1:9000" {
t.Errorf("Addr wants 127.0.0.1:9000 but was %s", ne1.Addr)
}
cause2 := causes[1]
var ne2 *net.OpError
if !errors.As(cause2, &ne2) {
t.Fatalf("cause1 wants net.OpError but was %T", errors.Unwrap(cause2))
}
if ne2.Addr.String() != "127.0.0.1:9001" {
t.Errorf("Addr wants 127.0.0.1:9001 but was %s", ne1.Addr)
}
})
}