forked from HDT3213/godis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaof_test.go
180 lines (177 loc) · 4.74 KB
/
aof_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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package godis
import (
"github.com/hdt3213/godis/config"
"github.com/hdt3213/godis/lib/utils"
"github.com/hdt3213/godis/redis/reply"
"io/ioutil"
"os"
"path"
"strconv"
"testing"
"time"
)
func TestAof(t *testing.T) {
tmpDir, err := ioutil.TempDir("", "godis")
if err != nil {
t.Error(err)
return
}
aofFilename := path.Join(tmpDir, "a.aof")
defer func() {
_ = os.Remove(aofFilename)
}()
config.Properties = &config.ServerProperties{
AppendOnly: true,
AppendFilename: aofFilename,
}
aofWriteDB := MakeDB()
size := 10
keys := make([]string, 0)
cursor := 0
for i := 0; i < size; i++ {
key := strconv.Itoa(cursor)
cursor++
execSet(aofWriteDB, utils.ToCmdLine(key, utils.RandString(8), "EX", "10000"))
keys = append(keys, key)
}
for i := 0; i < size; i++ {
key := strconv.Itoa(cursor)
cursor++
execRPush(aofWriteDB, utils.ToCmdLine(key, utils.RandString(8)))
keys = append(keys, key)
}
for i := 0; i < size; i++ {
key := strconv.Itoa(cursor)
cursor++
execHSet(aofWriteDB, utils.ToCmdLine(key, utils.RandString(8), utils.RandString(8)))
keys = append(keys, key)
}
for i := 0; i < size; i++ {
key := strconv.Itoa(cursor)
cursor++
execSAdd(aofWriteDB, utils.ToCmdLine(key, utils.RandString(8)))
keys = append(keys, key)
}
for i := 0; i < size; i++ {
key := strconv.Itoa(cursor)
cursor++
execZAdd(aofWriteDB, utils.ToCmdLine(key, "10", utils.RandString(8)))
keys = append(keys, key)
}
aofWriteDB.Close() // wait for aof finished
aofReadDB := MakeDB() // start new db and read aof file
for _, key := range keys {
expect, ok := aofWriteDB.GetEntity(key)
if !ok {
t.Errorf("key not found in origin: %s", key)
continue
}
actual, ok := aofReadDB.GetEntity(key)
if !ok {
t.Errorf("key not found: %s", key)
continue
}
expectData := EntityToCmd(key, expect).ToBytes()
actualData := EntityToCmd(key, actual).ToBytes()
if !utils.BytesEquals(expectData, actualData) {
t.Errorf("wrong value of key: %s", key)
}
}
aofReadDB.Close()
}
func TestRewriteAOF(t *testing.T) {
tmpDir, err := ioutil.TempDir("", "godis")
if err != nil {
t.Error(err)
return
}
aofFilename := path.Join(tmpDir, "a.aof")
defer func() {
_ = os.Remove(aofFilename)
}()
config.Properties = &config.ServerProperties{
AppendOnly: true,
AppendFilename: aofFilename,
}
aofWriteDB := MakeDB()
size := 1
keys := make([]string, 0)
ttlKeys := make([]string, 0)
cursor := 0
for i := 0; i < size; i++ {
key := "str" + strconv.Itoa(cursor)
cursor++
execSet(aofWriteDB, utils.ToCmdLine(key, utils.RandString(8)))
execSet(aofWriteDB, utils.ToCmdLine(key, utils.RandString(8)))
keys = append(keys, key)
}
// test ttl
for i := 0; i < size; i++ {
key := "str" + strconv.Itoa(cursor)
cursor++
execSet(aofWriteDB, utils.ToCmdLine(key, utils.RandString(8), "EX", "1000"))
ttlKeys = append(ttlKeys, key)
}
for i := 0; i < size; i++ {
key := "list" + strconv.Itoa(cursor)
cursor++
execRPush(aofWriteDB, utils.ToCmdLine(key, utils.RandString(8)))
execRPush(aofWriteDB, utils.ToCmdLine(key, utils.RandString(8)))
keys = append(keys, key)
}
for i := 0; i < size; i++ {
key := "hash" + strconv.Itoa(cursor)
cursor++
field := utils.RandString(8)
execHSet(aofWriteDB, utils.ToCmdLine(key, field, utils.RandString(8)))
execHSet(aofWriteDB, utils.ToCmdLine(key, field, utils.RandString(8)))
keys = append(keys, key)
}
for i := 0; i < size; i++ {
key := "set" + strconv.Itoa(cursor)
cursor++
member := utils.RandString(8)
execSAdd(aofWriteDB, utils.ToCmdLine(key, member))
execSAdd(aofWriteDB, utils.ToCmdLine(key, member))
keys = append(keys, key)
}
for i := 0; i < size; i++ {
key := "zset" + strconv.Itoa(cursor)
cursor++
execZAdd(aofWriteDB, utils.ToCmdLine(key, "10", utils.RandString(8)))
keys = append(keys, key)
}
time.Sleep(time.Second) // wait for async goroutine finish its job
aofWriteDB.aofRewrite()
aofWriteDB.Close() // wait for aof finished
aofReadDB := MakeDB() // start new db and read aof file
for _, key := range keys {
expect, ok := aofWriteDB.GetEntity(key)
if !ok {
t.Errorf("key not found in origin: %s", key)
continue
}
actual, ok := aofReadDB.GetEntity(key)
if !ok {
t.Errorf("key not found: %s", key)
continue
}
expectData := EntityToCmd(key, expect).ToBytes()
actualData := EntityToCmd(key, actual).ToBytes()
if !utils.BytesEquals(expectData, actualData) {
t.Errorf("wrong value of key: %s", key)
}
}
for _, key := range ttlKeys {
ret := execTTL(aofReadDB, utils.ToCmdLine(key))
intResult, ok := ret.(*reply.IntReply)
if !ok {
t.Errorf("expected int reply, actually %s", ret.ToBytes())
return
}
if intResult.Code <= 0 {
t.Errorf("expect a positive integer, actual: %d", intResult.Code)
}
}
aofReadDB.Close()
}