forked from redis/go-redis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
options_test.go
155 lines (148 loc) · 2.94 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
// +build go1.7
package redis
import (
"errors"
"testing"
"time"
)
func TestParseURL(t *testing.T) {
cases := []struct {
u string
addr string
db int
tls bool
err error
user string
pass string
}{
{
"redis://localhost:123/1",
"localhost:123",
1, false, nil,
"", "",
},
{
"redis://localhost:123",
"localhost:123",
0, false, nil,
"", "",
},
{
"redis://localhost/1",
"localhost:6379",
1, false, nil,
"", "",
},
{
"redis://12345",
"12345:6379",
0, false, nil,
"", "",
},
{
"rediss://localhost:123",
"localhost:123",
0, true, nil,
"", "",
},
{
"redis://:bar@localhost:123",
"localhost:123",
0, false, nil,
"", "bar",
},
{
"redis://foo@localhost:123",
"localhost:123",
0, false, nil,
"foo", "",
},
{
"redis://foo:bar@localhost:123",
"localhost:123",
0, false, nil,
"foo", "bar",
},
{
"redis://localhost/?abc=123",
"",
0, false, errors.New("no options supported"),
"", "",
},
{
"http://google.com",
"",
0, false, errors.New("invalid redis URL scheme: http"),
"", "",
},
{
"redis://localhost/1/2/3/4",
"",
0, false, errors.New("invalid redis URL path: /1/2/3/4"),
"", "",
},
{
"12345",
"",
0, false, errors.New("invalid redis URL scheme: "),
"", "",
},
{
"redis://localhost/iamadatabase",
"",
0, false, errors.New(`invalid redis database number: "iamadatabase"`),
"", "",
},
}
for _, c := range cases {
t.Run(c.u, func(t *testing.T) {
o, err := ParseURL(c.u)
if c.err == nil && err != nil {
t.Fatalf("unexpected error: %q", err)
return
}
if c.err != nil && err != nil {
if c.err.Error() != err.Error() {
t.Fatalf("got %q, expected %q", err, c.err)
}
return
}
if o.Addr != c.addr {
t.Errorf("got %q, want %q", o.Addr, c.addr)
}
if o.DB != c.db {
t.Errorf("got %q, expected %q", o.DB, c.db)
}
if c.tls && o.TLSConfig == nil {
t.Errorf("got nil TLSConfig, expected a TLSConfig")
}
if o.Username != c.user {
t.Errorf("got %q, expected %q", o.Username, c.user)
}
if o.Password != c.pass {
t.Errorf("got %q, expected %q", o.Password, c.pass)
}
})
}
}
// 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)
}
}
}