forked from redis-go/redis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredis_test.go
95 lines (76 loc) · 2.01 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
package redis
import (
"github.com/go-redis/redis"
"github.com/stretchr/testify/assert"
"testing"
"time"
)
// redis server
var r = Default()
// redis client
var c = redis.NewClient(&redis.Options{
Addr: ":6379",
})
func init() {
go r.Run(":6379")
}
func TestPingCommand(t *testing.T) {
s, err := c.Ping().Result()
assert.Equal(t, "PONG", s)
assert.NoError(t, err)
pingCmd := redis.NewStringCmd("ping", "Hello,", "redis server!")
c.Process(pingCmd)
s, err = pingCmd.Result()
assert.Equal(t, "Hello, redis server!", s)
assert.NoError(t, err)
}
func TestSetCommand(t *testing.T) {
s, err := c.Set("k", "v", 0).Result()
assert.Equal(t, "OK", s)
assert.NoError(t, err)
s, err = c.Set("k2", nil, 0).Result()
assert.Equal(t, "OK", s)
assert.NoError(t, err)
s, err = c.Set("k3", "v", 1*time.Hour).Result()
assert.Equal(t, "OK", s)
assert.NoError(t, err)
}
func TestGetCommand(t *testing.T) {
s, err := c.Get("k").Result()
assert.Equal(t, "v", s)
assert.NoError(t, err)
}
func TestDelCommand(t *testing.T) {
i, err := c.Del("k", "k3").Result()
assert.Equal(t, i, int64(2))
assert.NoError(t, err)
i, err = c.Del("abc").Result()
assert.Zero(t, i)
assert.NoError(t, err)
}
func TestTtlCommand(t *testing.T) {
s, err := c.Set("aKey", "hey", 1*time.Minute).Result()
assert.Equal(t, "OK", s)
assert.NoError(t, err)
s, err = c.Set("bKey", "hallo", 0).Result()
assert.Equal(t, "OK", s)
assert.NoError(t, err)
ttl, err := c.TTL("aKey").Result()
assert.True(t, ttl.Seconds() > 55 && ttl.Seconds() < 61, "ttl: %d", ttl)
assert.NoError(t, err)
ttl, err = c.TTL("none").Result()
assert.Equal(t, time.Duration(-2000000000), ttl)
assert.NoError(t, err)
ttl, err = c.TTL("bKey").Result()
assert.NoError(t, err)
assert.Equal(t, time.Duration(-1000000000), ttl)
}
func TestExpiry(t *testing.T) {
s, err := c.Set("x", "val", 10*time.Millisecond).Result()
assert.NoError(t, err)
assert.Equal(t, "OK", s)
time.Sleep(10 * time.Millisecond)
s, err = c.Get("x").Result()
assert.Equal(t, "", s)
assert.Error(t, err)
}