-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Expand file tree
/
Copy pathoptions_test.go
More file actions
439 lines (403 loc) · 14.4 KB
/
options_test.go
File metadata and controls
439 lines (403 loc) · 14.4 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
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
//go:build go1.7
package redis
import (
"crypto/tls"
"errors"
"sync"
"testing"
"time"
"github.com/redis/go-redis/v9/maintnotifications"
)
func TestParseURL(t *testing.T) {
cases := []struct {
url string
o *Options // expected value
err error
}{
{
url: "redis://localhost:123/1",
o: &Options{Addr: "localhost:123", DB: 1},
}, {
url: "redis://localhost:123",
o: &Options{Addr: "localhost:123"},
}, {
url: "redis://localhost/1",
o: &Options{Addr: "localhost:6379", DB: 1},
}, {
url: "redis://12345",
o: &Options{Addr: "12345:6379"},
}, {
url: "rediss://localhost:123",
o: &Options{Addr: "localhost:123", TLSConfig: &tls.Config{ /* no deep comparison */ }},
}, {
url: "rediss://localhost:123/?skip_verify=true",
o: &Options{Addr: "localhost:123", TLSConfig: &tls.Config{InsecureSkipVerify: true}},
}, {
url: "redis://:bar@localhost:123",
o: &Options{Addr: "localhost:123", Password: "bar"},
}, {
url: "redis://foo@localhost:123",
o: &Options{Addr: "localhost:123", Username: "foo"},
}, {
url: "redis://foo:bar@localhost:123",
o: &Options{Addr: "localhost:123", Username: "foo", Password: "bar"},
}, {
// multiple params
url: "redis://localhost:123/?db=2&read_timeout=2&pool_fifo=true",
o: &Options{Addr: "localhost:123", DB: 2, ReadTimeout: 2 * time.Second, PoolFIFO: true},
}, {
// special case handling for disabled timeouts
url: "redis://localhost:123/?db=2&conn_max_idle_time=0",
o: &Options{Addr: "localhost:123", DB: 2, ConnMaxIdleTime: -1},
}, {
// negative values disable timeouts as well
url: "redis://localhost:123/?db=2&conn_max_idle_time=-1",
o: &Options{Addr: "localhost:123", DB: 2, ConnMaxIdleTime: -1},
}, {
// absent timeout values will use defaults
url: "redis://localhost:123/?db=2&conn_max_idle_time=",
o: &Options{Addr: "localhost:123", DB: 2, ConnMaxIdleTime: 0},
}, {
url: "redis://localhost:123/?db=2&conn_max_idle_time", // missing "=" at the end
o: &Options{Addr: "localhost:123", DB: 2, ConnMaxIdleTime: 0},
}, {
url: "redis://localhost:123/?db=2&client_name=hi", // client name
o: &Options{Addr: "localhost:123", DB: 2, ClientName: "hi"},
}, {
url: "redis://localhost:123/?db=2&protocol=2", // RESP Protocol
o: &Options{Addr: "localhost:123", DB: 2, Protocol: 2},
}, {
url: "redis://localhost:123/?max_concurrent_dials=5", // MaxConcurrentDials parameter
o: &Options{Addr: "localhost:123", MaxConcurrentDials: 5},
}, {
url: "redis://localhost:123/?max_concurrent_dials=0", // MaxConcurrentDials zero value
o: &Options{Addr: "localhost:123", MaxConcurrentDials: 0},
}, {
url: "redis://localhost:123/?conn_max_lifetime=1h&conn_max_lifetime_jitter=6m",
o: &Options{Addr: "localhost:123", ConnMaxLifetime: time.Hour, ConnMaxLifetimeJitter: 6 * time.Minute},
}, {
// jitter > lifetime should be capped
url: "redis://localhost:123/?conn_max_lifetime=30m&conn_max_lifetime_jitter=1h",
o: &Options{Addr: "localhost:123", ConnMaxLifetime: 30 * time.Minute, ConnMaxLifetimeJitter: 30 * time.Minute},
}, {
// jitter without lifetime should be capped to 0
url: "redis://localhost:123/?conn_max_lifetime_jitter=6m",
o: &Options{Addr: "localhost:123", ConnMaxLifetimeJitter: 0},
}, {
url: "unix:///tmp/redis.sock",
o: &Options{Addr: "/tmp/redis.sock"},
}, {
url: "unix://foo:bar@/tmp/redis.sock",
o: &Options{Addr: "/tmp/redis.sock", Username: "foo", Password: "bar"},
}, {
url: "unix://foo:bar@/tmp/redis.sock?db=3",
o: &Options{Addr: "/tmp/redis.sock", Username: "foo", Password: "bar", DB: 3},
}, {
// invalid db format
url: "unix://foo:bar@/tmp/redis.sock?db=test",
err: errors.New(`redis: invalid database number: strconv.Atoi: parsing "test": invalid syntax`),
}, {
// invalid int value
url: "redis://localhost/?pool_size=five",
err: errors.New(`redis: invalid pool_size number: strconv.Atoi: parsing "five": invalid syntax`),
}, {
// invalid bool value
url: "redis://localhost/?pool_fifo=yes",
err: errors.New(`redis: invalid pool_fifo boolean: expected true/false/1/0 or an empty string, got "yes"`),
}, {
// it returns first error
url: "redis://localhost/?db=foo&pool_size=five",
err: errors.New(`redis: invalid database number: strconv.Atoi: parsing "foo": invalid syntax`),
}, {
url: "redis://localhost/?abc=123",
err: errors.New("redis: unexpected option: abc"),
}, {
url: "redis://foo@localhost/?username=bar",
err: errors.New("redis: unexpected option: username"),
}, {
url: "redis://localhost/?wrte_timout=10s&abc=123",
err: errors.New("redis: unexpected option: abc, wrte_timout"),
}, {
url: "http://google.com",
err: errors.New("redis: invalid URL scheme: http"),
}, {
url: "redis://localhost/1/2/3/4",
err: errors.New("redis: invalid URL path: /1/2/3/4"),
}, {
url: "12345",
err: errors.New("redis: invalid URL scheme: "),
}, {
url: "redis://localhost/iamadatabase",
err: errors.New(`redis: invalid database number: "iamadatabase"`),
},
}
for i := range cases {
tc := cases[i]
t.Run(tc.url, func(t *testing.T) {
t.Parallel()
actual, err := ParseURL(tc.url)
if tc.err == nil && err != nil {
t.Fatalf("unexpected error: %q", err)
return
}
if tc.err != nil && err != nil {
if tc.err.Error() != err.Error() {
t.Fatalf("got %q, expected %q", err, tc.err)
}
return
}
comprareOptions(t, actual, tc.o)
})
}
}
func comprareOptions(t *testing.T, actual, expected *Options) {
t.Helper()
if actual.Addr != expected.Addr {
t.Errorf("got %q, want %q", actual.Addr, expected.Addr)
}
if actual.DB != expected.DB {
t.Errorf("DB: got %q, expected %q", actual.DB, expected.DB)
}
if actual.TLSConfig == nil && expected.TLSConfig != nil {
t.Errorf("got nil TLSConfig, expected a TLSConfig")
}
if actual.TLSConfig != nil && expected.TLSConfig == nil {
t.Errorf("got TLSConfig, expected no TLSConfig")
}
if actual.Username != expected.Username {
t.Errorf("Username: got %q, expected %q", actual.Username, expected.Username)
}
if actual.Password != expected.Password {
t.Errorf("Password: got %q, expected %q", actual.Password, expected.Password)
}
if actual.MaxRetries != expected.MaxRetries {
t.Errorf("MaxRetries: got %v, expected %v", actual.MaxRetries, expected.MaxRetries)
}
if actual.MinRetryBackoff != expected.MinRetryBackoff {
t.Errorf("MinRetryBackoff: got %v, expected %v", actual.MinRetryBackoff, expected.MinRetryBackoff)
}
if actual.MaxRetryBackoff != expected.MaxRetryBackoff {
t.Errorf("MaxRetryBackoff: got %v, expected %v", actual.MaxRetryBackoff, expected.MaxRetryBackoff)
}
if actual.DialTimeout != expected.DialTimeout {
t.Errorf("DialTimeout: got %v, expected %v", actual.DialTimeout, expected.DialTimeout)
}
if actual.ReadTimeout != expected.ReadTimeout {
t.Errorf("ReadTimeout: got %v, expected %v", actual.ReadTimeout, expected.ReadTimeout)
}
if actual.WriteTimeout != expected.WriteTimeout {
t.Errorf("WriteTimeout: got %v, expected %v", actual.WriteTimeout, expected.WriteTimeout)
}
if actual.PoolFIFO != expected.PoolFIFO {
t.Errorf("PoolFIFO: got %v, expected %v", actual.PoolFIFO, expected.PoolFIFO)
}
if actual.PoolSize != expected.PoolSize {
t.Errorf("PoolSize: got %v, expected %v", actual.PoolSize, expected.PoolSize)
}
if actual.PoolTimeout != expected.PoolTimeout {
t.Errorf("PoolTimeout: got %v, expected %v", actual.PoolTimeout, expected.PoolTimeout)
}
if actual.MinIdleConns != expected.MinIdleConns {
t.Errorf("MinIdleConns: got %v, expected %v", actual.MinIdleConns, expected.MinIdleConns)
}
if actual.MaxIdleConns != expected.MaxIdleConns {
t.Errorf("MaxIdleConns: got %v, expected %v", actual.MaxIdleConns, expected.MaxIdleConns)
}
if actual.ConnMaxIdleTime != expected.ConnMaxIdleTime {
t.Errorf("ConnMaxIdleTime: got %v, expected %v", actual.ConnMaxIdleTime, expected.ConnMaxIdleTime)
}
if actual.ConnMaxLifetime != expected.ConnMaxLifetime {
t.Errorf("ConnMaxLifetime: got %v, expected %v", actual.ConnMaxLifetime, expected.ConnMaxLifetime)
}
if actual.ConnMaxLifetimeJitter != expected.ConnMaxLifetimeJitter {
t.Errorf("ConnMaxLifetimeJitter: got %v, expected %v", actual.ConnMaxLifetimeJitter, expected.ConnMaxLifetimeJitter)
}
if actual.MaxConcurrentDials != expected.MaxConcurrentDials {
t.Errorf("MaxConcurrentDials: got %v, expected %v", actual.MaxConcurrentDials, expected.MaxConcurrentDials)
}
}
// Test ReadTimeout option initialization, including special values -1 and 0.
// And also test behaviour of WriteTimeout option, when it is not explicitly set and use
// ReadTimeout value.
func TestReadTimeoutOptions(t *testing.T) {
testDataInputOutputMap := map[time.Duration]time.Duration{
-1: 0 * time.Second,
0: 3 * time.Second,
1: 1 * time.Nanosecond,
3: 3 * time.Nanosecond,
}
for in, out := range testDataInputOutputMap {
o := &Options{ReadTimeout: in}
o.init()
if o.ReadTimeout != out {
t.Errorf("got %d instead of %d as ReadTimeout option", o.ReadTimeout, out)
}
if o.WriteTimeout != o.ReadTimeout {
t.Errorf("got %d instead of %d as WriteTimeout option", o.WriteTimeout, o.ReadTimeout)
}
}
}
func TestProtocolOptions(t *testing.T) {
testCasesMap := map[int]int{
0: 3,
1: 3,
2: 2,
3: 3,
}
o := &Options{}
o.init()
if o.Protocol != 3 {
t.Errorf("got %d instead of %d as protocol option", o.Protocol, 3)
}
for set, want := range testCasesMap {
o := &Options{Protocol: set}
o.init()
if o.Protocol != want {
t.Errorf("got %d instead of %d as protocol option", o.Protocol, want)
}
}
}
func TestMaxConcurrentDialsOptions(t *testing.T) {
// Test cases for MaxConcurrentDials initialization logic
testCases := []struct {
name string
poolSize int
maxConcurrentDials int
expectedConcurrentDials int
}{
// Edge cases and invalid values - negative/zero values set to PoolSize
{
name: "negative value gets set to pool size",
poolSize: 10,
maxConcurrentDials: -1,
expectedConcurrentDials: 10, // negative values are set to PoolSize
},
// Zero value tests - MaxConcurrentDials should be set to PoolSize
{
name: "zero value with positive pool size",
poolSize: 1,
maxConcurrentDials: 0,
expectedConcurrentDials: 1, // MaxConcurrentDials = PoolSize when 0
},
// Explicit positive value tests
{
name: "explicit value within limit",
poolSize: 10,
maxConcurrentDials: 3,
expectedConcurrentDials: 3, // should remain unchanged when < PoolSize
},
// Capping tests - values exceeding PoolSize should be capped
{
name: "value exceeding pool size",
poolSize: 5,
maxConcurrentDials: 10,
expectedConcurrentDials: 5, // should be capped at PoolSize
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
opts := &Options{
PoolSize: tc.poolSize,
MaxConcurrentDials: tc.maxConcurrentDials,
}
opts.init()
if opts.MaxConcurrentDials != tc.expectedConcurrentDials {
t.Errorf("MaxConcurrentDials: got %v, expected %v (PoolSize=%v)",
opts.MaxConcurrentDials, tc.expectedConcurrentDials, opts.PoolSize)
}
// Ensure MaxConcurrentDials never exceeds PoolSize (for all inputs)
if opts.MaxConcurrentDials > opts.PoolSize {
t.Errorf("MaxConcurrentDials (%v) should not exceed PoolSize (%v)",
opts.MaxConcurrentDials, opts.PoolSize)
}
// Ensure MaxConcurrentDials is always positive (for all inputs)
if opts.MaxConcurrentDials <= 0 {
t.Errorf("MaxConcurrentDials should be positive, got %v", opts.MaxConcurrentDials)
}
})
}
}
func TestClusterOptionsDialerRetries(t *testing.T) {
clusterOpt := &ClusterOptions{
DialerRetries: 10,
DialerRetryTimeout: 200 * time.Millisecond,
}
opt := clusterOpt.clientOptions()
if opt.DialerRetries != 10 {
t.Errorf("expected DialerRetries=10, got %d", opt.DialerRetries)
}
if opt.DialerRetryTimeout != 200*time.Millisecond {
t.Errorf("expected DialerRetryTimeout=200ms, got %v", opt.DialerRetryTimeout)
}
}
func TestRingOptionsDialerRetries(t *testing.T) {
ringOpt := &RingOptions{
DialerRetries: 10,
DialerRetryTimeout: 200 * time.Millisecond,
}
opt := ringOpt.clientOptions()
if opt.DialerRetries != 10 {
t.Errorf("expected DialerRetries=10, got %d", opt.DialerRetries)
}
if opt.DialerRetryTimeout != 200*time.Millisecond {
t.Errorf("expected DialerRetryTimeout=200ms, got %v", opt.DialerRetryTimeout)
}
}
func TestFailoverOptionsDialerRetries(t *testing.T) {
failoverOpt := &FailoverOptions{
DialerRetries: 10,
DialerRetryTimeout: 200 * time.Millisecond,
}
opt := failoverOpt.clientOptions()
if opt.DialerRetries != 10 {
t.Errorf("expected DialerRetries=10, got %d", opt.DialerRetries)
}
if opt.DialerRetryTimeout != 200*time.Millisecond {
t.Errorf("expected DialerRetryTimeout=200ms, got %v", opt.DialerRetryTimeout)
}
// Also verify sentinelOptions passes them through
sentinelOpt := failoverOpt.sentinelOptions("localhost:26379")
if sentinelOpt.DialerRetries != 10 {
t.Errorf("expected sentinel DialerRetries=10, got %d", sentinelOpt.DialerRetries)
}
if sentinelOpt.DialerRetryTimeout != 200*time.Millisecond {
t.Errorf("expected sentinel DialerRetryTimeout=200ms, got %v", sentinelOpt.DialerRetryTimeout)
}
}
// TestOptionsCloneMaintNotificationsRace verifies that cloning options via
// baseClient is safe when initConn concurrently writes MaintNotificationsConfig.Mode.
// Run with -race to detect the data race.
func TestOptionsCloneMaintNotificationsRace(t *testing.T) {
opt := &Options{
Addr: "localhost:6379",
MaintNotificationsConfig: &maintnotifications.Config{
Mode: maintnotifications.ModeAuto,
},
}
bc := baseClient{opt: opt}
var wg sync.WaitGroup
const iterations = 1000
// Writer: simulates initConn toggling MaintNotificationsConfig.Mode under optLock
wg.Add(1)
go func() {
defer wg.Done()
for i := 0; i < iterations; i++ {
bc.optLock.Lock()
bc.opt.MaintNotificationsConfig.Mode = maintnotifications.ModeDisabled
bc.optLock.Unlock()
bc.optLock.Lock()
bc.opt.MaintNotificationsConfig.Mode = maintnotifications.ModeAuto
bc.optLock.Unlock()
}
}()
// Reader: simulates newTx / withTimeout calling cloneOpt() (acquires RLock)
wg.Add(1)
go func() {
defer wg.Done()
for i := 0; i < iterations; i++ {
cloned := bc.cloneOpt()
_ = cloned
}
}()
wg.Wait()
}