forked from alicebob/miniredis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
miniredis_test.go
258 lines (223 loc) · 5.25 KB
/
miniredis_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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
package miniredis
import (
"strings"
"testing"
"time"
"github.com/gomodule/redigo/redis"
)
// Test starting/stopping a server
func TestServer(t *testing.T) {
s, err := Run()
ok(t, err)
defer s.Close()
c, err := redis.Dial("tcp", s.Addr())
ok(t, err)
_, err = c.Do("PING")
ok(t, err)
// A single client
equals(t, 1, s.CurrentConnectionCount())
equals(t, 1, s.TotalConnectionCount())
equals(t, 1, s.CommandCount())
_, err = c.Do("PING")
ok(t, err)
equals(t, 2, s.CommandCount())
}
func TestMultipleServers(t *testing.T) {
s1, err := Run()
ok(t, err)
s2, err := Run()
ok(t, err)
if s1.Addr() == s2.Addr() {
t.Fatal("Non-unique addresses", s1.Addr(), s2.Addr())
}
s2.Close()
s1.Close()
// Closing multiple times is fine
go s1.Close()
go s1.Close()
s1.Close()
}
func TestRestart(t *testing.T) {
s, err := Run()
ok(t, err)
addr := s.Addr()
s.Set("color", "red")
s.Close()
err = s.Restart()
ok(t, err)
if have, want := s.Addr(), addr; have != want {
t.Fatalf("have: %s, want: %s", have, want)
}
c, err := redis.Dial("tcp", s.Addr())
ok(t, err)
_, err = c.Do("PING")
ok(t, err)
red, err := redis.String(c.Do("GET", "color"))
ok(t, err)
if have, want := red, "red"; have != want {
t.Errorf("have: %s, want: %s", have, want)
}
}
// Test a custom addr
func TestAddr(t *testing.T) {
m := NewMiniRedis()
err := m.StartAddr("127.0.0.1:7887")
ok(t, err)
defer m.Close()
c, err := redis.Dial("tcp", "127.0.0.1:7887")
ok(t, err)
_, err = c.Do("PING")
ok(t, err)
}
func TestDump(t *testing.T) {
s, err := Run()
ok(t, err)
s.Set("aap", "noot")
s.Set("vuur", "mies")
s.HSet("ahash", "aap", "noot")
s.HSet("ahash", "vuur", "mies")
if have, want := s.Dump(), `- aap
"noot"
- ahash
aap: "noot"
vuur: "mies"
- vuur
"mies"
`; have != want {
t.Errorf("have: %q, want: %q", have, want)
}
// Tricky whitespace
s.Select(1)
s.Set("whitespace", "foo\nbar\tbaz!")
if have, want := s.Dump(), `- whitespace
"foo\nbar\tbaz!"
`; have != want {
t.Errorf("have: %q, want: %q", have, want)
}
// Long key
s.Select(2)
s.Set("long", "This is a rather long key, with some fox jumping over a fence or something.")
s.Set("countonme", "0123456789012345678901234567890123456789012345678901234567890123456789")
s.HSet("hlong", "long", "This is another rather long key, with some fox jumping over a fence or something.")
if have, want := s.Dump(), `- countonme
"01234567890123456789012345678901234567890123456789012"...(70)
- hlong
long: "This is another rather long key, with some fox jumpin"...(81)
- long
"This is a rather long key, with some fox jumping over"...(75)
`; have != want {
t.Errorf("have: %q, want: %q", have, want)
}
}
func TestDumpList(t *testing.T) {
s, err := Run()
ok(t, err)
s.Push("elements", "earth")
s.Push("elements", "wind")
s.Push("elements", "fire")
if have, want := s.Dump(), `- elements
"earth"
"wind"
"fire"
`; have != want {
t.Errorf("have: %q, want: %q", have, want)
}
}
func TestDumpSet(t *testing.T) {
s, err := Run()
ok(t, err)
s.SetAdd("elements", "earth")
s.SetAdd("elements", "wind")
s.SetAdd("elements", "fire")
if have, want := s.Dump(), `- elements
"earth"
"fire"
"wind"
`; have != want {
t.Errorf("have: %q, want: %q", have, want)
}
}
func TestDumpSortedSet(t *testing.T) {
s, err := Run()
ok(t, err)
s.ZAdd("elements", 2.0, "wind")
s.ZAdd("elements", 3.0, "earth")
s.ZAdd("elements", 1.0, "fire")
if have, want := s.Dump(), `- elements
1.000000: "fire"
2.000000: "wind"
3.000000: "earth"
`; have != want {
t.Errorf("have: %q, want: %q", have, want)
}
}
func TestDumpStream(t *testing.T) {
s, err := Run()
ok(t, err)
s.XAdd("elements", "0-1", []string{"name", "earth"})
s.XAdd("elements", "123456789-0", []string{"name", "wind"})
s.XAdd("elements", "123456789-1", []string{"name", "fire"})
if have, want := s.Dump(), `- elements
0-1
"name": "earth"
123456789-0
"name": "wind"
123456789-1
"name": "fire"
`; have != want {
t.Errorf("have: %q, want: %q", have, want)
}
s.XAdd("elements", "*", []string{"name", "Leeloo"})
fullHave := s.Dump()
have := strings.Split(fullHave, "\n")[8]
want := ` "name": "Leeloo"`
if have != want {
t.Errorf("have: %q, want: %q", have, want)
}
}
func TestKeysAndFlush(t *testing.T) {
s, err := Run()
ok(t, err)
s.Set("aap", "noot")
s.Set("vuur", "mies")
s.Set("muur", "oom")
s.HSet("hash", "key", "value")
equals(t, []string{"aap", "hash", "muur", "vuur"}, s.Keys())
s.Select(1)
s.Set("1aap", "1noot")
equals(t, []string{"1aap"}, s.Keys())
s.Select(0)
s.FlushDB()
equals(t, []string{}, s.Keys())
s.Select(1)
equals(t, []string{"1aap"}, s.Keys())
s.Select(0)
s.FlushAll()
equals(t, []string{}, s.Keys())
s.Select(1)
equals(t, []string{}, s.Keys())
}
func TestExpireWithFastForward(t *testing.T) {
s, err := Run()
ok(t, err)
s.Set("aap", "noot")
s.Set("noot", "aap")
s.SetTTL("aap", 10*time.Second)
s.FastForward(5 * time.Second)
equals(t, 2, len(s.Keys()))
s.FastForward(5 * time.Second)
equals(t, 1, len(s.Keys()))
}
func TestRedigo(t *testing.T) {
s, err := Run()
ok(t, err)
r := s.redigo()
defer r.Close()
_, err = r.Do("SELECT", 2)
ok(t, err)
_, err = r.Do("SET", "foo", "bar")
ok(t, err)
v, err := redis.String(r.Do("GET", "foo"))
ok(t, err)
equals(t, "bar", v)
}