1
2
3 package redis
4
5 import (
6 "errors"
7 "testing"
8 "time"
9 )
10
11 func TestParseURL(t *testing.T) {
12 cases := []struct {
13 u string
14 addr string
15 db int
16 tls bool
17 err error
18 }{
19 {
20 "redis://localhost:123/1",
21 "localhost:123",
22 1, false, nil,
23 },
24 {
25 "redis://localhost:123",
26 "localhost:123",
27 0, false, nil,
28 },
29 {
30 "redis://localhost/1",
31 "localhost:6379",
32 1, false, nil,
33 },
34 {
35 "redis://12345",
36 "12345:6379",
37 0, false, nil,
38 },
39 {
40 "rediss://localhost:123",
41 "localhost:123",
42 0, true, nil,
43 },
44 {
45 "redis://localhost/?abc=123",
46 "",
47 0, false, errors.New("no options supported"),
48 },
49 {
50 "http://google.com",
51 "",
52 0, false, errors.New("invalid redis URL scheme: http"),
53 },
54 {
55 "redis://localhost/1/2/3/4",
56 "",
57 0, false, errors.New("invalid redis URL path: /1/2/3/4"),
58 },
59 {
60 "12345",
61 "",
62 0, false, errors.New("invalid redis URL scheme: "),
63 },
64 {
65 "redis://localhost/iamadatabase",
66 "",
67 0, false, errors.New(`invalid redis database number: "iamadatabase"`),
68 },
69 }
70
71 for _, c := range cases {
72 t.Run(c.u, func(t *testing.T) {
73 o, err := ParseURL(c.u)
74 if c.err == nil && err != nil {
75 t.Fatalf("unexpected error: %q", err)
76 return
77 }
78 if c.err != nil && err != nil {
79 if c.err.Error() != err.Error() {
80 t.Fatalf("got %q, expected %q", err, c.err)
81 }
82 return
83 }
84 if o.Addr != c.addr {
85 t.Errorf("got %q, want %q", o.Addr, c.addr)
86 }
87 if o.DB != c.db {
88 t.Errorf("got %q, expected %q", o.DB, c.db)
89 }
90 if c.tls && o.TLSConfig == nil {
91 t.Errorf("got nil TLSConfig, expected a TLSConfig")
92 }
93 })
94 }
95 }
96
97
98
99
100 func TestReadTimeoutOptions(t *testing.T) {
101 testDataInputOutputMap := map[time.Duration]time.Duration{
102 -1: 0 * time.Second,
103 0: 3 * time.Second,
104 1: 1 * time.Nanosecond,
105 3: 3 * time.Nanosecond,
106 }
107
108 for in, out := range testDataInputOutputMap {
109 o := &Options{ReadTimeout: in}
110 o.init()
111 if o.ReadTimeout != out {
112 t.Errorf("got %d instead of %d as ReadTimeout option", o.ReadTimeout, out)
113 }
114
115 if o.WriteTimeout != o.ReadTimeout {
116 t.Errorf("got %d instead of %d as WriteTimeout option", o.WriteTimeout, o.ReadTimeout)
117 }
118 }
119 }
120
View as plain text