-
Notifications
You must be signed in to change notification settings - Fork 0
/
redis_test.go
99 lines (77 loc) · 1.89 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
96
97
98
99
package longredis
import (
"context"
"fmt"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
var (
ctx = context.Background()
singleAddr = []string{"127.0.0.1:6379"}
addrs = []string{"127.0.0.1:6379"}
)
func TestLongredis(t *testing.T) {
c, err := NewClient(Option{
Addrs: []string{"127.0.0.1:6379"},
})
assert.Nil(t, err)
err = c.Set(ctx, "longtest", "helloworld")
assert.Nil(t, err)
v, err := c.Get(ctx, "longtest")
assert.Nil(t, err)
assert.Equal(t, "helloworld", string(v))
}
func TestRequestTimeout(t *testing.T) {
_, err := NewClient(Option{
Addrs: []string{"127.0.0.1:6379"},
RequestTimeout: time.Nanosecond * 100,
})
assert.ErrorIs(t, err, context.DeadlineExceeded)
}
func TestPrefix(t *testing.T) {
c, err := NewClient(Option{
Addrs: []string{"127.0.0.1:6379"},
})
assert.Nil(t, err)
longtestClient := c.SubPrefix("longtest")
err = longtestClient.Set(ctx, "hello", "world")
assert.Nil(t, err)
v, err := longtestClient.Get(ctx, "hello")
assert.Nil(t, err)
assert.Equal(t, "world", string(v))
v, err = c.Get(ctx, "hello")
assert.NotNil(t, err)
assert.Nil(t, v)
}
func TestReidsSingle(t *testing.T) {
c, err := NewClient(Option{
Addrs: []string{"127.0.0.1:6379"}, // local redis
RedisType: RedisTypeSingle,
})
assert.Nil(t, err)
err = c.Ping(ctx)
checkErr(err)
}
func TestGetString(t *testing.T) {
c, err := NewClient(Option{
Addrs: []string{"127.0.0.1:6379"},
RedisType: RedisTypeSingle,
})
assert.Nil(t, err)
err = c.Set(ctx, "test_get_string", "hello world")
assert.Nil(t, err)
v, err := c.GetString(ctx, "test_get_string")
assert.Nil(t, err)
fmt.Printf("get string : %s\n", v)
assert.Equal(t, "hello world", v)
b, err := c.Get(ctx, "test_get_string")
assert.Nil(t, err)
fmt.Printf("get byte : %s\n", string(b))
assert.Equal(t, "hello world", string(b))
}
func checkErr(err error) {
if err != nil {
panic(err)
}
}