-
Notifications
You must be signed in to change notification settings - Fork 0
/
redis_server_test.go
127 lines (109 loc) · 2.63 KB
/
redis_server_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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package lantern_cache
import (
"fmt"
"testing"
"time"
"github.com/go-redis/redis/v7"
"github.com/stretchr/testify/assert"
)
func TestRedisServer(t *testing.T) {
ca := NewLanternCache(&Config{
BucketCount: 256,
MaxCapacity: 1024 * 1024 * 100,
InitCapacity: 1024 * 1024 * 50,
})
server := NewRedisServer(":6379", ca)
go func() {
err := server.ListenAndServe()
if err != nil {
panic(err)
}
}()
time.Sleep(time.Millisecond * 300)
client := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
})
{
ca.Reset()
key := "key"
val := "val"
err := client.Set(key, val, 0).Err()
assert.Nil(t, err)
actual, err := client.Get(key).Result()
assert.Nil(t, err)
assert.Equal(t, val, actual)
}
{
ca.Reset()
key := "key"
val := "val"
err := client.Set(key, val, time.Second).Err()
assert.Nil(t, err)
actual, err := client.Get(key).Result()
assert.Nil(t, err)
assert.Equal(t, val, actual)
time.Sleep(2 * time.Second)
// 超时返回redis.nil
actual, err = client.Get(key).Result()
assert.Equal(t, redis.Nil, err)
assert.Equal(t, "", actual)
}
{
ca.Reset()
kvs := make([]string, 0, 20)
keys := make([]string, 0, 10)
vals := make([]interface{}, 0, 10)
for i := 0; i < 10; i++ {
keys = append(keys, fmt.Sprintf("key-%d", i))
vals = append(vals, fmt.Sprintf("val-%d", i))
kvs = append(kvs, fmt.Sprintf("key-%d", i), fmt.Sprintf("val-%d", i))
}
err := client.MSet(kvs).Err()
assert.Nil(t, err)
actual, err := client.MGet(keys...).Result()
assert.Equal(t, nil, err)
assert.Equal(t, vals, actual)
}
{
ca.Reset()
bucket := "bucket"
key := "key"
val := "val"
err := client.HSet(bucket, key, val).Err()
assert.Nil(t, err)
actual, err := client.HGet(bucket, key).Result()
assert.Nil(t, err)
assert.Equal(t, val, actual)
}
{
ca.Reset()
bucket := "bucket"
kvs := make([]string, 0, 20)
keys := make([]string, 0, 10)
vals := make([]interface{}, 0, 10)
for i := 0; i < 10; i++ {
keys = append(keys, fmt.Sprintf("key-%d", i))
vals = append(vals, fmt.Sprintf("val-%d", i))
kvs = append(kvs, fmt.Sprintf("key-%d", i), fmt.Sprintf("val-%d", i))
}
err := client.HMSet(bucket, kvs).Err()
assert.Nil(t, err)
actual, err := client.HMGet(bucket, keys...).Result()
assert.Equal(t, nil, err)
assert.Equal(t, vals, actual)
}
{
ca.Reset()
key := "key"
val := "val"
err := client.Set(key, val, 0).Err()
assert.Nil(t, err)
actual, err := client.Get(key).Result()
assert.Nil(t, err)
assert.Equal(t, val, actual)
client.Del(key)
actual, err = client.Get(key).Result()
assert.Equal(t, redis.Nil, err)
assert.Equal(t, "", actual)
}
}