-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathredis_test.go
150 lines (138 loc) · 3.48 KB
/
redis_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
package goredis
import (
"fmt"
"testing"
"time"
)
var (
network = "tcp"
address = "127.0.0.1:6379"
db = 1
password = ""
timeout = 5 * time.Second
maxidle = 1
r *Redis
format = "tcp://auth:%s@%s/%d?timeout=%s&maxidle=%d"
)
func init() {
client, err := Dial(&DialConfig{network, address, db, password, timeout, maxidle})
if err != nil {
panic(err)
}
r = client
}
func TestDial(t *testing.T) {
redis, err := Dial(&DialConfig{network, address, db, password, timeout, maxidle})
if err != nil {
t.Error(err)
} else if err := redis.Ping(); err != nil {
t.Error(err)
}
redis.pool.Close()
}
func TestDialTimeout(t *testing.T) {
redis, err := Dial(&DialConfig{network, address, db, password, timeout, maxidle})
if err != nil {
t.Error(err)
} else if err := redis.Ping(); err != nil {
t.Error(err)
}
redis.pool.Close()
}
func TestDialURL(t *testing.T) {
redis, err := DialURL(fmt.Sprintf(format, password, address, db, timeout.String(), maxidle))
if err != nil {
t.Fatal(err)
} else if err := redis.Ping(); err != nil {
t.Error(err)
}
redis.pool.Close()
}
func TestNewDialConfigFromURL(t *testing.T) {
defaultDialConfig := &DialConfig{
Address: DefaultAddress,
Database: 0,
Network: DefaultNetwork,
MaxIdle: DefaultMaxIdle,
Password: "",
Timeout: DefaultTimeout,
}
datasets := []struct {
errExpected bool
expectedDialConfig *DialConfig
urlString string
}{
{
false,
defaultDialConfig,
"",
}, {
true,
nil,
"://",
},
{
false,
defaultDialConfig,
"tcp://",
},
{
true,
nil,
"tcp:///invalid-db",
},
{
true,
nil,
"tcp://?maxidle=invalid-maxidle",
},
{
true,
nil,
"tcp:///?timeout=invalid-timeout",
},
{
false,
&DialConfig{
Address: address,
Database: db,
Network: "tcp",
MaxIdle: maxidle,
Password: password,
Timeout: timeout,
},
fmt.Sprintf(format, password, address, db, timeout.String(), maxidle),
},
}
for i, dataset := range datasets {
expectedDialConfig := dataset.expectedDialConfig
dialConfig, err := newDialConfigFromURLString(dataset.urlString)
if err == nil && dataset.errExpected {
t.Fatalf("Dataset %d: Expected error, but none was returned", i)
}
if err != nil {
if !dataset.errExpected {
t.Fatalf("Dataset %d: No error expected, but error was returned: %s", i, err.Error())
}
continue
}
if dialConfig.Address != expectedDialConfig.Address {
t.Errorf("Dataset %d: Addresses should match. Expected: %s, got: %s", i, expectedDialConfig.Address, dialConfig.Address)
}
if dialConfig.Database != expectedDialConfig.Database {
t.Errorf("Dataset %d: Databases should match. Expected: %d, got: %d", i, expectedDialConfig.Database, dialConfig.Database)
}
if dialConfig.MaxIdle != expectedDialConfig.MaxIdle {
t.Errorf("Dataset %d: MaxIdle should match. Expected: %d, got: %d", i, expectedDialConfig.MaxIdle, dialConfig.MaxIdle)
}
if dialConfig.Network != expectedDialConfig.Network {
t.Errorf("Dataset %d: Networks should match. Expected: %d, got: %d", i, expectedDialConfig.Network, dialConfig.Network)
}
if dialConfig.Password != expectedDialConfig.Password {
t.Errorf("Dataset %d: Passwords should match. Expected: %s, got: %s", i, expectedDialConfig.Password, dialConfig.Password)
}
if dialConfig.Timeout != expectedDialConfig.Timeout {
t.Errorf("Dataset %d: Timeouts should match. Expected: %d, got: %d", i, expectedDialConfig.Timeout, dialConfig.Timeout)
}
}
}