redis/options_test.go

181 lines
3.4 KiB
Go
Raw Normal View History

2021-09-08 16:00:52 +03:00
//go:build go1.7
// +build go1.7
package redis
import (
"errors"
"testing"
"time"
)
func TestParseURL(t *testing.T) {
cases := []struct {
2016-11-15 21:27:20 +03:00
u string
addr string
db int
tls bool
err error
2020-05-21 08:59:20 +03:00
user string
pass string
}{
{
"redis://localhost:123/1",
"localhost:123",
1, false, nil,
2020-05-21 08:59:20 +03:00
"", "",
},
{
"redis://localhost:123",
"localhost:123",
0, false, nil,
2020-05-21 08:59:20 +03:00
"", "",
},
{
"redis://localhost/1",
"localhost:6379",
1, false, nil,
2020-05-21 08:59:20 +03:00
"", "",
},
{
"redis://12345",
"12345:6379",
0, false, nil,
2020-05-21 08:59:20 +03:00
"", "",
},
{
"rediss://localhost:123",
"localhost:123",
0, true, nil,
2020-05-21 08:59:20 +03:00
"", "",
},
{
"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",
},
{
"unix:///tmp/redis.sock",
"/tmp/redis.sock",
0, false, nil,
"", "",
},
{
"unix://foo:bar@/tmp/redis.sock",
"/tmp/redis.sock",
0, false, nil,
"foo", "bar",
},
{
"unix://foo:bar@/tmp/redis.sock?db=3",
"/tmp/redis.sock",
3, false, nil,
"foo", "bar",
},
{
"unix://foo:bar@/tmp/redis.sock?db=test",
"/tmp/redis.sock",
2020-09-28 15:29:35 +03:00
0, false, errors.New("redis: invalid database number: strconv.Atoi: parsing \"test\": invalid syntax"),
"", "",
},
{
"redis://localhost/?abc=123",
"",
2020-09-28 15:29:35 +03:00
0, false, errors.New("redis: no options supported"),
2020-05-21 08:59:20 +03:00
"", "",
},
{
"http://google.com",
"",
2020-09-28 15:29:35 +03:00
0, false, errors.New("redis: invalid URL scheme: http"),
2020-05-21 08:59:20 +03:00
"", "",
},
{
"redis://localhost/1/2/3/4",
"",
2020-09-28 15:29:35 +03:00
0, false, errors.New("redis: invalid URL path: /1/2/3/4"),
2020-05-21 08:59:20 +03:00
"", "",
},
{
"12345",
"",
2020-09-28 15:29:35 +03:00
0, false, errors.New("redis: invalid URL scheme: "),
2020-05-21 08:59:20 +03:00
"", "",
},
{
"redis://localhost/iamadatabase",
"",
2020-09-28 15:29:35 +03:00
0, false, errors.New(`redis: invalid database number: "iamadatabase"`),
2020-05-21 08:59:20 +03:00
"", "",
},
}
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() {
2016-11-15 21:27:20 +03:00
t.Fatalf("got %q, expected %q", err, c.err)
}
return
}
if o.Addr != c.addr {
2016-11-15 21:27:20 +03:00
t.Errorf("got %q, want %q", o.Addr, c.addr)
}
if o.DB != c.db {
2016-11-15 21:27:20 +03:00
t.Errorf("got %q, expected %q", o.DB, c.db)
}
2016-11-15 21:27:20 +03:00
if c.tls && o.TLSConfig == nil {
t.Errorf("got nil TLSConfig, expected a TLSConfig")
}
2020-05-21 08:59:20 +03:00
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)
}
}
}