-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathredis_test.go
72 lines (53 loc) · 1.53 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
package redis
import (
"net"
"testing"
"time"
rd "github.com/redis/go-redis/v9"
)
const (
testKey = "foo"
testValue = "bar"
)
func TestRedis(t *testing.T) {
conn := rd.NewClient(&rd.Options{
Addr: ":6379",
})
if _, err := net.Dial("tcp", "localhost:6379"); err != nil {
t.Skip(err)
}
c := New(conn)
if err := c.Save(testKey, testValue, 10*time.Second); err != nil {
t.Errorf("save fail: expected nil, got %v", err)
}
if res, _ := c.Fetch(testKey); res != testValue {
t.Errorf("fetch fail, wrong value: expected %s, got %s", testValue, res)
}
if _, err := c.Fetch("bar"); err == nil {
t.Errorf("fetch fail: expected an error, got %v", err)
}
if !c.Contains(testKey) {
t.Errorf("contains failed: the key %s should be exist", testKey)
}
_ = c.Save("bar", testValue, 0)
if values := c.FetchMulti([]string{testKey, "bar"}); len(values) == 0 {
t.Errorf("fetch multi failed: expected %d, got %d", 2, len(values))
}
if err := c.Delete(testKey); err != nil {
t.Errorf("delete failed: expected nil, got %v", err)
}
if err := c.Flush(); err != nil {
t.Errorf("flush failed: expected nil, got %v", err)
}
if c.Contains(testKey) {
t.Errorf("contains failed: the key %s should not be exist", testKey)
}
conn = rd.NewClient(&rd.Options{Addr: ":6380"})
c = New(conn)
if c.Contains(testKey) {
t.Errorf("contains failed: the key %s should not be exist", testKey)
}
if values := c.FetchMulti([]string{testKey, "bar"}); len(values) != 0 {
t.Errorf("fetch multi failed: expected %d, got %d", 0, len(values))
}
}