forked from HDT3213/godis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmulti_utils_test.go
186 lines (171 loc) · 6.06 KB
/
multi_utils_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
181
182
183
184
185
186
package godis
import (
"github.com/hdt3213/godis/lib/utils"
"github.com/hdt3213/godis/redis/reply/asserts"
"testing"
"time"
)
func TestRollbackGivenKeys(t *testing.T) {
testDB.Flush()
// rollback to string
key := utils.RandString(10)
value := utils.RandString(10)
testDB.Exec(nil, utils.ToCmdLine("SET", key, value, "EX", "200"))
undoCmdLines := rollbackGivenKeys(testDB, key)
rawExpire, _ := testDB.ttlMap.Get(key)
expireTime, _ := rawExpire.(time.Time)
// override given key
value2 := value + utils.RandString(5)
testDB.Exec(nil, utils.ToCmdLine("SET", key, value2, "EX", "1000"))
for _, cmdLine := range undoCmdLines {
testDB.Exec(nil, cmdLine)
}
actual := testDB.Exec(nil, utils.ToCmdLine("GET", key))
asserts.AssertBulkReply(t, actual, value)
rawExpire2, _ := testDB.ttlMap.Get(key)
expireTime2, _ := rawExpire2.(time.Time)
timeDiff := expireTime.Sub(expireTime2)
if timeDiff < -time.Millisecond || timeDiff > time.Millisecond {
t.Error("rollback ttl failed")
}
}
func TestRollbackToList(t *testing.T) {
key := utils.RandString(10)
value := utils.RandString(10)
value2 := utils.RandString(10)
testDB.Remove(key)
testDB.Exec(nil, utils.ToCmdLine("RPUSH", key, value, value2))
undoCmdLines := rollbackGivenKeys(testDB, key)
testDB.Exec(nil, utils.ToCmdLine("LREM", key, value2))
for _, cmdLine := range undoCmdLines {
testDB.Exec(nil, cmdLine)
}
actual := testDB.Exec(nil, utils.ToCmdLine("LRANGE", key, "0", "-1"))
asserts.AssertMultiBulkReply(t, actual, []string{value, value2})
}
func TestRollbackToSet(t *testing.T) {
key := utils.RandString(10)
value := utils.RandString(10)
value2 := utils.RandString(10)
testDB.Remove(key)
cmdLine := utils.ToCmdLine("SADD", key, value)
testDB.Exec(nil, cmdLine)
undoCmdLines := rollbackFirstKey(testDB, cmdLine[1:])
testDB.Exec(nil, utils.ToCmdLine("SADD", key, value2))
for _, cmdLine := range undoCmdLines {
testDB.Exec(nil, cmdLine)
}
actual := testDB.Exec(nil, utils.ToCmdLine("SMembers", key))
asserts.AssertMultiBulkReply(t, actual, []string{value})
}
func TestRollbackSetMembers(t *testing.T) {
key := utils.RandString(10)
value := utils.RandString(10)
testDB.Remove(key)
// undo srem
cmdLine := utils.ToCmdLine("SADD", key, value)
testDB.Exec(nil, cmdLine)
undoCmdLines := undoSetChange(testDB, cmdLine[1:])
testDB.Exec(nil, utils.ToCmdLine("SREM", key, value))
for _, cmdLine := range undoCmdLines {
testDB.Exec(nil, cmdLine)
}
actual := testDB.Exec(nil, utils.ToCmdLine("SIsMember", key, value))
asserts.AssertIntReply(t, actual, 1)
// undo sadd
testDB.Remove(key)
value2 := utils.RandString(10)
testDB.Exec(nil, utils.ToCmdLine("SADD", key, value2))
cmdLine = utils.ToCmdLine("SADD", key, value)
undoCmdLines = undoSetChange(testDB, cmdLine[1:])
testDB.Exec(nil, cmdLine)
for _, cmdLine := range undoCmdLines {
testDB.Exec(nil, cmdLine)
}
actual = testDB.Exec(nil, utils.ToCmdLine("SIsMember", key, value))
asserts.AssertIntReply(t, actual, 0)
// undo sadd, only member
testDB.Remove(key)
undoCmdLines = rollbackSetMembers(testDB, key, value)
testDB.Exec(nil, utils.ToCmdLine("SAdd", key, value))
for _, cmdLine := range undoCmdLines {
testDB.Exec(nil, cmdLine)
}
actual = testDB.Exec(nil, utils.ToCmdLine("type", key))
asserts.AssertStatusReply(t, actual, "none")
}
func TestRollbackToHash(t *testing.T) {
key := utils.RandString(10)
value := utils.RandString(10)
value2 := utils.RandString(10)
testDB.Remove(key)
testDB.Exec(nil, utils.ToCmdLine("HSet", key, value, value))
undoCmdLines := rollbackGivenKeys(testDB, key)
testDB.Exec(nil, utils.ToCmdLine("HSet", key, value, value2))
for _, cmdLine := range undoCmdLines {
testDB.Exec(nil, cmdLine)
}
actual := testDB.Exec(nil, utils.ToCmdLine("HGET", key, value))
asserts.AssertBulkReply(t, actual, value)
}
func TestRollbackHashFields(t *testing.T) {
key := utils.RandString(10)
value := utils.RandString(10)
value2 := utils.RandString(10)
testDB.Remove(key)
testDB.Exec(nil, utils.ToCmdLine("HSet", key, value, value))
undoCmdLines := rollbackHashFields(testDB, key, value, value2)
testDB.Exec(nil, utils.ToCmdLine("HSet", key, value, value2, value2, value2))
for _, cmdLine := range undoCmdLines {
testDB.Exec(nil, cmdLine)
}
actual := testDB.Exec(nil, utils.ToCmdLine("HGET", key, value))
asserts.AssertBulkReply(t, actual, value)
actual = testDB.Exec(nil, utils.ToCmdLine("HGET", key, value2))
asserts.AssertNullBulk(t, actual)
testDB.Remove(key)
undoCmdLines = rollbackHashFields(testDB, key, value)
testDB.Exec(nil, utils.ToCmdLine("HSet", key, value, value))
for _, cmdLine := range undoCmdLines {
testDB.Exec(nil, cmdLine)
}
actual = testDB.Exec(nil, utils.ToCmdLine("type", key))
asserts.AssertStatusReply(t, actual, "none")
}
func TestRollbackToZSet(t *testing.T) {
key := utils.RandString(10)
value := utils.RandString(10)
testDB.Remove(key)
testDB.Exec(nil, utils.ToCmdLine("ZADD", key, "1", value))
undoCmdLines := rollbackGivenKeys(testDB, key)
testDB.Exec(nil, utils.ToCmdLine("ZADD", key, "2", value))
for _, cmdLine := range undoCmdLines {
testDB.Exec(nil, cmdLine)
}
actual := testDB.Exec(nil, utils.ToCmdLine("ZSCORE", key, value))
asserts.AssertBulkReply(t, actual, "1")
}
func TestRollbackZSetFields(t *testing.T) {
key := utils.RandString(10)
value := utils.RandString(10)
value2 := utils.RandString(10)
testDB.Remove(key)
testDB.Exec(nil, utils.ToCmdLine("ZADD", key, "1", value))
undoCmdLines := rollbackZSetFields(testDB, key, value, value2)
testDB.Exec(nil, utils.ToCmdLine("ZADD", key, "2", value, "3", value2))
for _, cmdLine := range undoCmdLines {
testDB.Exec(nil, cmdLine)
}
actual := testDB.Exec(nil, utils.ToCmdLine("ZSCORE", key, value))
asserts.AssertBulkReply(t, actual, "1")
actual = testDB.Exec(nil, utils.ToCmdLine("ZSCORE", key, value2))
asserts.AssertNullBulk(t, actual)
testDB.Remove(key)
undoCmdLines = rollbackZSetFields(testDB, key, value)
testDB.Exec(nil, utils.ToCmdLine("ZADD", key, "1", value))
for _, cmdLine := range undoCmdLines {
testDB.Exec(nil, cmdLine)
}
actual = testDB.Exec(nil, utils.ToCmdLine("type", key))
asserts.AssertStatusReply(t, actual, "none")
}