2021-09-08 16:00:52 +03:00
|
|
|
//go:build go1.7
|
2016-11-14 20:40:12 +03:00
|
|
|
// +build go1.7
|
|
|
|
|
2016-11-12 02:47:49 +03:00
|
|
|
package redis
|
|
|
|
|
|
|
|
import (
|
2021-09-10 20:15:16 +03:00
|
|
|
"crypto/tls"
|
2016-11-12 02:47:49 +03:00
|
|
|
"errors"
|
|
|
|
"testing"
|
2018-10-05 09:46:29 +03:00
|
|
|
"time"
|
2016-11-12 02:47:49 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestParseURL(t *testing.T) {
|
|
|
|
cases := []struct {
|
2021-09-10 20:15:16 +03:00
|
|
|
url string
|
|
|
|
o *Options // expected value
|
|
|
|
err error
|
2016-11-12 02:47:49 +03:00
|
|
|
}{
|
|
|
|
{
|
2021-09-10 20:15:16 +03:00
|
|
|
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: "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"},
|
2021-09-10 22:12:33 +03:00
|
|
|
}, {
|
|
|
|
// 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&idle_timeout=0",
|
2022-07-28 15:11:35 +03:00
|
|
|
o: &Options{Addr: "localhost:123", DB: 2, ConnMaxIdleTime: -1},
|
2021-09-10 22:12:33 +03:00
|
|
|
}, {
|
|
|
|
// negative values disable timeouts as well
|
|
|
|
url: "redis://localhost:123/?db=2&idle_timeout=-1",
|
2022-07-28 15:11:35 +03:00
|
|
|
o: &Options{Addr: "localhost:123", DB: 2, ConnMaxIdleTime: -1},
|
2021-09-10 22:12:33 +03:00
|
|
|
}, {
|
|
|
|
// absent timeout values will use defaults
|
|
|
|
url: "redis://localhost:123/?db=2&idle_timeout=",
|
2022-07-28 15:11:35 +03:00
|
|
|
o: &Options{Addr: "localhost:123", DB: 2, ConnMaxIdleTime: 0},
|
2021-09-10 22:12:33 +03:00
|
|
|
}, {
|
|
|
|
url: "redis://localhost:123/?db=2&idle_timeout", // missing "=" at the end
|
2022-07-28 15:11:35 +03:00
|
|
|
o: &Options{Addr: "localhost:123", DB: 2, ConnMaxIdleTime: 0},
|
2021-09-10 20:15:16 +03:00
|
|
|
}, {
|
|
|
|
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},
|
|
|
|
}, {
|
2021-09-10 22:12:33 +03:00
|
|
|
// invalid db format
|
2021-09-10 20:15:16 +03:00
|
|
|
url: "unix://foo:bar@/tmp/redis.sock?db=test",
|
|
|
|
err: errors.New(`redis: invalid database number: strconv.Atoi: parsing "test": invalid syntax`),
|
2021-09-10 22:12:33 +03:00
|
|
|
}, {
|
|
|
|
// 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`),
|
2021-09-10 20:15:16 +03:00
|
|
|
}, {
|
|
|
|
url: "redis://localhost/?abc=123",
|
2021-09-10 22:12:33 +03:00
|
|
|
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"),
|
2021-09-10 20:15:16 +03:00
|
|
|
}, {
|
|
|
|
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"`),
|
2016-11-12 02:47:49 +03:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-09-10 20:15:16 +03:00
|
|
|
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 {
|
2018-01-04 15:20:49 +03:00
|
|
|
t.Fatalf("unexpected error: %q", err)
|
2016-11-12 02:47:49 +03:00
|
|
|
return
|
|
|
|
}
|
2021-09-10 20:15:16 +03:00
|
|
|
if tc.err != nil && err != nil {
|
|
|
|
if tc.err.Error() != err.Error() {
|
|
|
|
t.Fatalf("got %q, expected %q", err, tc.err)
|
2016-11-12 02:47:49 +03:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2021-09-10 20:15:16 +03:00
|
|
|
comprareOptions(t, actual, tc.o)
|
2016-11-12 02:47:49 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2018-10-05 09:46:29 +03:00
|
|
|
|
2021-09-10 20:15:16 +03:00
|
|
|
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 {
|
2021-09-10 22:12:33 +03:00
|
|
|
t.Errorf("DB: got %q, expected %q", actual.DB, expected.DB)
|
2021-09-10 20:15:16 +03:00
|
|
|
}
|
|
|
|
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 {
|
2021-09-10 22:12:33 +03:00
|
|
|
t.Errorf("Username: got %q, expected %q", actual.Username, expected.Username)
|
2021-09-10 20:15:16 +03:00
|
|
|
}
|
|
|
|
if actual.Password != expected.Password {
|
2021-09-10 22:12:33 +03:00
|
|
|
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)
|
|
|
|
}
|
2022-07-28 15:11:35 +03:00
|
|
|
if actual.PoolTimeout != expected.PoolTimeout {
|
|
|
|
t.Errorf("PoolTimeout: got %v, expected %v", actual.PoolTimeout, expected.PoolTimeout)
|
|
|
|
}
|
2021-09-10 22:12:33 +03:00
|
|
|
if actual.MinIdleConns != expected.MinIdleConns {
|
|
|
|
t.Errorf("MinIdleConns: got %v, expected %v", actual.MinIdleConns, expected.MinIdleConns)
|
|
|
|
}
|
2022-07-28 15:11:35 +03:00
|
|
|
if actual.MaxIdleConns != expected.MaxIdleConns {
|
|
|
|
t.Errorf("MaxIdleConns: got %v, expected %v", actual.MaxIdleConns, expected.MaxIdleConns)
|
2021-09-10 22:12:33 +03:00
|
|
|
}
|
2022-07-28 15:11:35 +03:00
|
|
|
if actual.ConnMaxIdleTime != expected.ConnMaxIdleTime {
|
|
|
|
t.Errorf("ConnMaxIdleTime: got %v, expected %v", actual.ConnMaxIdleTime, expected.ConnMaxIdleTime)
|
2021-09-10 22:12:33 +03:00
|
|
|
}
|
2022-07-28 15:11:35 +03:00
|
|
|
if actual.ConnMaxLifetime != expected.ConnMaxLifetime {
|
|
|
|
t.Errorf("ConnMaxLifetime: got %v, expected %v", actual.ConnMaxLifetime, expected.ConnMaxLifetime)
|
2021-09-10 20:15:16 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-05 09:46:29 +03:00
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|