forked from alicebob/miniredis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tx_test.go
176 lines (156 loc) · 3.29 KB
/
tx_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
// +build int
package main
// Transaction things.
import (
"testing"
)
func TestTx(t *testing.T) {
testCommands(t,
succ("MULTI"),
succ("SET", "AAP", 1),
succ("GET", "AAP"),
succ("EXEC"),
succ("GET", "AAP"),
)
// err: Double MULTI
testCommands(t,
succ("MULTI"),
fail("MULTI"),
)
// err: No MULTI
testCommands(t,
fail("EXEC"),
)
// Errors in the MULTI sequence
testCommands(t,
succ("MULTI"),
succ("SET", "foo", "bar"),
fail("SET", "foo"),
succ("SET", "foo", "bar"),
fail("EXEC"),
)
// Simple WATCH
testCommands(t,
succ("SET", "foo", "bar"),
succ("WATCH", "foo"),
succ("MULTI"),
succ("GET", "foo"),
succ("EXEC"),
)
// Simple UNWATCH
testCommands(t,
succ("SET", "foo", "bar"),
succ("WATCH", "foo"),
succ("UNWATCH"),
succ("MULTI"),
succ("GET", "foo"),
succ("EXEC"),
)
// UNWATCH in a MULTI. Yep. Weird.
testCommands(t,
succ("WATCH", "foo"),
succ("MULTI"),
succ("UNWATCH"), // Valid. Somehow.
succ("EXEC"),
)
// Test whether all these commands support transactions.
testCommands(t,
succ("MULTI"),
succ("GET", "str"),
succ("SET", "str", "bar"),
succ("SETNX", "str", "bar"),
succ("GETSET", "str", "bar"),
succ("MGET", "str", "bar"),
succ("MSET", "str", "bar"),
succ("MSETNX", "str", "bar"),
succ("SETEX", "str", 12, "newv"),
succ("PSETEX", "str", 12, "newv"),
succ("STRLEN", "str"),
succ("APPEND", "str", "more"),
succ("GETRANGE", "str", 0, 2),
succ("SETRANGE", "str", 0, "B"),
succ("EXEC"),
succ("GET", "str"),
)
testCommands(t,
succ("MULTI"),
succ("SET", "bits", "\xff\x00"),
succ("BITCOUNT", "bits"),
succ("BITOP", "OR", "bits", "bits", "nosuch"),
succ("BITPOS", "bits", 1),
succ("GETBIT", "bits", 12),
succ("SETBIT", "bits", 12, 1),
succ("EXEC"),
succ("GET", "bits"),
)
testCommands(t,
succ("MULTI"),
succ("INCR", "number"),
succ("INCRBY", "number", 12),
succ("INCRBYFLOAT", "number", 12.2),
succ("DECR", "number"),
succ("GET", "number"),
succ("DECRBY", "number", 2),
succ("GET", "number"),
)
testCommands(t,
succ("MULTI"),
succ("HSET", "hash", "foo", "bar"),
succ("HDEL", "hash", "foo"),
succ("HEXISTS", "hash", "foo"),
succ("HSET", "hash", "foo", "bar22"),
succ("HSETNX", "hash", "foo", "bar22"),
succ("HGET", "hash", "foo"),
succ("HMGET", "hash", "foo", "baz"),
succ("HLEN", "hash"),
succ("HGETALL", "hash"),
succ("HKEYS", "hash"),
succ("HVALS", "hash"),
)
testCommands(t,
succ("MULTI"),
succ("SET", "key", "foo"),
succ("TYPE", "key"),
succ("EXPIRE", "key", 12),
succ("TTL", "key"),
succ("PEXPIRE", "key", 12),
succ("PTTL", "key"),
succ("PERSIST", "key"),
succ("DEL", "key"),
succ("TYPE", "key"),
succ("EXEC"),
)
// BITOP OPs are checked after the transaction.
testCommands(t,
succ("MULTI"),
succ("BITOP", "BROKEN", "str", ""),
succ("EXEC"),
)
// fail on invalid command
testCommands(t,
succ("MULTI"),
fail("GET"),
fail("EXEC"),
)
/* FIXME
// fail on unknown command
testCommands(t,
succ("MULTI"),
fail("NOSUCH"),
fail("EXEC"),
)
*/
// failed EXEC cleaned up the tx
testCommands(t,
succ("MULTI"),
fail("GET"),
fail("EXEC"),
succ("MULTI"),
)
testClients2(t, func(c1, c2 chan<- command) {
c1 <- succ("WATCH", "foo")
c1 <- succ("MULTI")
c2 <- succ("SET", "foo", "12")
c1 <- succ("EXEC") // nil
})
}