generated from devnw/oss-template
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
options_test.go
313 lines (272 loc) · 5.17 KB
/
options_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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
package plex
import (
"context"
"errors"
"fmt"
"net"
"testing"
"time"
)
type conntest struct {
conns []net.Conn
err bool
expected int
}
func connectionTests() map[string]conntest {
return map[string]conntest{
"single": {
[]net.Conn{
&testconn{},
},
false,
1,
},
"single nil": {
[]net.Conn{
nil,
},
false,
0,
},
"single valid, single nil (first)": {
[]net.Conn{
nil,
&testconn{},
},
false,
1,
},
"single valid, single nil (last)": {
[]net.Conn{
&testconn{},
nil,
},
false,
1,
},
"multi": {
[]net.Conn{
&testconn{},
&testconn{},
&testconn{},
&testconn{},
&testconn{},
},
false,
5,
},
"multi nil": {
[]net.Conn{
&testconn{},
nil,
&testconn{},
nil,
&testconn{},
},
false,
3,
},
"multi valid, single nil": {
[]net.Conn{
&testconn{},
&testconn{},
nil,
&testconn{},
&testconn{},
},
false,
4,
},
"multi different remote": {
[]net.Conn{
&testconn{
addr: "127.0.0.1",
},
&testconn{},
},
true,
0,
},
}
}
func Test_WithConnections(t *testing.T) {
for name, test := range connectionTests() {
t.Run(name, func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
m, err := New(ctx, WithConnections(test.conns...))
if err != nil {
if !test.err {
t.Fatal(err)
}
return
}
defer func() {
err := m.Close()
if err != nil {
t.Errorf("Close() failed: %v", err)
}
}()
if len(m.readers) != test.expected {
t.Fatalf("expected %d readers, got %d", test.expected, len(m.readers))
}
if len(m.writers) != test.expected {
t.Fatalf("expected %d writers, got %d", test.expected, len(m.writers))
}
})
}
}
func Test_WithConnector(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
fn := func(ctx context.Context, addr net.Addr) (net.Conn, error) {
return &testconn{
addr: "0.0.0.0",
tcp: true,
}, nil
}
m, err := New(
ctx,
WithConnector(fn),
)
if err != nil {
t.Fatal(err)
}
if m.connector == nil {
t.Fatal("expected connector to be set")
}
}
func Test_WithConnector_nil(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
_, err := New(
ctx,
WithConnector(nil),
)
if err != errConnectorNil {
t.Fatalf("expected ErrConnectorNil; got %v", err)
}
}
func WithErrOption() Option {
return func(m *Multiplexer) error {
return errors.New("err")
}
}
func Test_OptionError(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
_, err := New(
ctx,
WithErrOption(),
)
if err == nil {
t.Fatal("expected error")
}
}
func Test_WithMaxCapacity(t *testing.T) {
testdata := map[int]error{
1: nil,
5: nil,
10: nil,
15: nil,
-1: errInvalidMaxCapacity,
}
for capacity, expErr := range testdata {
t.Run(fmt.Sprintf("cap-%v", capacity), func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
m, err := New(
ctx,
WithMaxCapacity(capacity),
)
if err != expErr {
t.Fatalf("expected %v; got %v", expErr, err)
}
if err != nil {
return
}
if m.capacity != capacity {
t.Fatalf("expected capacity %v; got %v", capacity, m.capacity)
}
if cap(m.readers) != capacity {
t.Fatalf(
"expected reader capacity %v; got %v",
capacity,
cap(m.readers),
)
}
if cap(m.writers) != capacity {
t.Fatalf(
"expected writer capacity %v; got %v",
capacity,
cap(m.writers),
)
}
})
}
}
func Test_WithMaxCapacity_MoreConnections(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
_, err := New(
ctx,
WithMaxCapacity(1),
WithConnections(
&testconn{},
&testconn{},
),
)
if err != errTooManyConns {
t.Fatalf("expected %v; got %v", errTooManyConns, err)
}
}
func Test_WithAutoScale_NoConnect(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
_, err := New(
ctx,
WithAutoScaling(time.Millisecond),
)
if err != errImproperAutoScalingNilConnector {
t.Fatalf(
"expected %v; got %v",
errImproperAutoScalingNilConnector,
err,
)
}
}
func Test_WithAutoScaling(t *testing.T) {
testdata := map[time.Duration]error{
time.Nanosecond: nil,
time.Millisecond: nil,
time.Second: nil,
time.Minute: nil,
time.Duration(-1): errInvalidTimeout,
}
for duration, expErr := range testdata {
t.Run(fmt.Sprintf("scale_timeout-%s", duration), func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
m, err := New(
ctx,
WithAutoScaling(duration),
WithConnector(
func(context.Context, net.Addr) (net.Conn, error) {
return &testconn{}, nil
}),
)
if err != expErr {
t.Fatalf("expected %v; got %v", expErr, err)
}
if err != nil {
return
}
if m.scaleTimeout == nil {
t.Fatal("expected scaleTimeout to be set")
}
if *m.scaleTimeout != duration {
t.Fatalf("expected capacity %v; got %v", duration, *m.scaleTimeout)
}
})
}
}