superfeedr-em-redis 0.2.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +2 -0
- data/History.txt +22 -0
- data/README.rdoc +85 -0
- data/Rakefile +41 -0
- data/lib/em-redis/redis_protocol.rb +446 -0
- data/lib/em-redis.rb +47 -0
- data/spec/live_redis_protocol_spec.rb +446 -0
- data/spec/redis_commands_spec.rb +647 -0
- data/spec/redis_protocol_spec.rb +149 -0
- data/spec/test_helper.rb +21 -0
- metadata +106 -0
data/lib/em-redis.rb
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
|
2
|
+
module EMRedis
|
3
|
+
|
4
|
+
# :stopdoc:
|
5
|
+
VERSION = '0.2.2'
|
6
|
+
LIBPATH = ::File.expand_path(::File.dirname(__FILE__)) + ::File::SEPARATOR
|
7
|
+
PATH = ::File.dirname(LIBPATH) + ::File::SEPARATOR
|
8
|
+
# :startdoc:
|
9
|
+
|
10
|
+
# Returns the version string for the library.
|
11
|
+
#
|
12
|
+
def self.version
|
13
|
+
VERSION
|
14
|
+
end
|
15
|
+
|
16
|
+
# Returns the library path for the module. If any arguments are given,
|
17
|
+
# they will be joined to the end of the libray path using
|
18
|
+
# <tt>File.join</tt>.
|
19
|
+
#
|
20
|
+
def self.libpath( *args )
|
21
|
+
args.empty? ? LIBPATH : ::File.join(LIBPATH, args.flatten)
|
22
|
+
end
|
23
|
+
|
24
|
+
# Returns the lpath for the module. If any arguments are given,
|
25
|
+
# they will be joined to the end of the path using
|
26
|
+
# <tt>File.join</tt>.
|
27
|
+
#
|
28
|
+
def self.path( *args )
|
29
|
+
args.empty? ? PATH : ::File.join(PATH, args.flatten)
|
30
|
+
end
|
31
|
+
|
32
|
+
# Utility method used to require all files ending in .rb that lie in the
|
33
|
+
# directory below this file that has the same name as the filename passed
|
34
|
+
# in. Optionally, a specific _directory_ name can be passed in such that
|
35
|
+
# the _filename_ does not have to be equivalent to the directory.
|
36
|
+
#
|
37
|
+
def self.require_all_libs_relative_to( fname, dir = nil )
|
38
|
+
dir ||= ::File.basename(fname, '.*')
|
39
|
+
search_me = ::File.expand_path(
|
40
|
+
::File.join(::File.dirname(fname), dir, '**', '*.rb'))
|
41
|
+
|
42
|
+
Dir.glob(search_me).sort.each {|rb| require rb}
|
43
|
+
end
|
44
|
+
|
45
|
+
end # module EMRedis
|
46
|
+
|
47
|
+
EMRedis.require_all_libs_relative_to(__FILE__)
|
@@ -0,0 +1,446 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + "/test_helper.rb")
|
2
|
+
|
3
|
+
EM.describe EM::Protocols::Redis, "connected to an empty db" do
|
4
|
+
|
5
|
+
before do
|
6
|
+
@c = EM::Protocols::Redis.connect :db => 14
|
7
|
+
@c.flushdb
|
8
|
+
end
|
9
|
+
|
10
|
+
should "be able to set a string value" do
|
11
|
+
@c.set("foo", "bar") do |r|
|
12
|
+
r.should == "OK"
|
13
|
+
done
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
should "be able to increment the value of a string" do
|
18
|
+
@c.incr "foo" do |r|
|
19
|
+
r.should == 1
|
20
|
+
@c.incr "foo" do |r|
|
21
|
+
r.should == 2
|
22
|
+
done
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
should "be able to increment the value of a string by an amount" do
|
28
|
+
@c.incrby "foo", 10 do |r|
|
29
|
+
r.should == 10
|
30
|
+
done
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
should "be able to decrement the value of a string" do
|
35
|
+
@c.incr "foo" do |r|
|
36
|
+
r.should == 1
|
37
|
+
@c.decr "foo" do |r|
|
38
|
+
r.should == 0
|
39
|
+
done
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
should "be able to decrement the value of a string by an amount" do
|
45
|
+
@c.incrby "foo", 20 do |r|
|
46
|
+
r.should == 20
|
47
|
+
@c.decrby "foo", 10 do |r|
|
48
|
+
r.should == 10
|
49
|
+
done
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
should "be able to 'lpush' to a nonexistent list" do
|
55
|
+
@c.lpush("foo", "bar") do |r|
|
56
|
+
r.should == "OK"
|
57
|
+
done
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
should "be able to 'rpush' to a nonexistent list" do
|
62
|
+
@c.rpush("foo", "bar") do |r|
|
63
|
+
r.should == "OK"
|
64
|
+
done
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
|
69
|
+
should "be able to get the size of the database" do
|
70
|
+
@c.dbsize do |r|
|
71
|
+
r.should == 0
|
72
|
+
done
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
should "be able to add a member to a nonexistent set" do
|
77
|
+
@c.sadd("set_foo", "bar") do |r|
|
78
|
+
r.should == true
|
79
|
+
done
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
should "be able to get info about the db as a hash" do
|
84
|
+
@c.info do |r|
|
85
|
+
r.should.key? :redis_version
|
86
|
+
done
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
should "be able to save db" do
|
91
|
+
@c.save do |r|
|
92
|
+
r.should == "OK"
|
93
|
+
done
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
should "be able to save db in the background" do
|
98
|
+
@c.bgsave do |r|
|
99
|
+
r.should == "Background saving started"
|
100
|
+
done
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
end
|
105
|
+
|
106
|
+
EM.describe EM::Protocols::Redis, "connected to a db containing some simple string-valued keys" do
|
107
|
+
|
108
|
+
before do
|
109
|
+
@c = EM::Protocols::Redis.connect :db => 14
|
110
|
+
@c.flushdb
|
111
|
+
@c.set "a", "b"
|
112
|
+
@c.set "x", "y"
|
113
|
+
end
|
114
|
+
|
115
|
+
should "be able to fetch the values of multiple keys" do
|
116
|
+
@c.mget "a", "x" do |r|
|
117
|
+
r.should == ["b", "y"]
|
118
|
+
done
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
should "be able to fetch the values of multiple keys in a hash" do
|
123
|
+
@c.mapped_mget "a", "x" do |r|
|
124
|
+
r.should == {"a" => "b", "x" => "y"}
|
125
|
+
done
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
should "be able to fetch all the keys" do
|
130
|
+
@c.keys "*" do |r|
|
131
|
+
r.sort.should == ["a", "x"]
|
132
|
+
done
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
should "be able to set a value if a key doesn't exist" do
|
137
|
+
@c.setnx "a", "foo" do |r|
|
138
|
+
r.should == false
|
139
|
+
@c.setnx "zzz", "foo" do |r|
|
140
|
+
r.should == true
|
141
|
+
done
|
142
|
+
end
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
146
|
+
should "be able to test for the existence of a key" do
|
147
|
+
@c.exists "a" do |r|
|
148
|
+
r.should == true
|
149
|
+
@c.exists "zzz" do |r|
|
150
|
+
r.should == false
|
151
|
+
done
|
152
|
+
end
|
153
|
+
end
|
154
|
+
end
|
155
|
+
|
156
|
+
should "be able to delete a key" do
|
157
|
+
@c.del "a" do |r|
|
158
|
+
r.should == true
|
159
|
+
@c.exists "a" do |r|
|
160
|
+
r.should == false
|
161
|
+
@c.del "a" do |r|
|
162
|
+
r.should == false
|
163
|
+
done
|
164
|
+
end
|
165
|
+
end
|
166
|
+
end
|
167
|
+
end
|
168
|
+
|
169
|
+
should "be able to detect the type of a key, existing or not" do
|
170
|
+
@c.type "a" do |r|
|
171
|
+
r.should == "string"
|
172
|
+
@c.type "zzz" do |r|
|
173
|
+
r.should == "none"
|
174
|
+
done
|
175
|
+
end
|
176
|
+
end
|
177
|
+
end
|
178
|
+
|
179
|
+
should "be able to rename a key" do
|
180
|
+
@c.rename "a", "x" do |r|
|
181
|
+
@c.get "x" do |r|
|
182
|
+
r.should == "b"
|
183
|
+
done
|
184
|
+
end
|
185
|
+
end
|
186
|
+
end
|
187
|
+
|
188
|
+
should "be able to rename a key unless it exists" do
|
189
|
+
@c.renamenx "a", "x" do |r|
|
190
|
+
r.should == false
|
191
|
+
@c.renamenx "a", "zzz" do |r|
|
192
|
+
r.should == true
|
193
|
+
@c.get "zzz" do |r|
|
194
|
+
r.should == "b"
|
195
|
+
done
|
196
|
+
end
|
197
|
+
end
|
198
|
+
end
|
199
|
+
end
|
200
|
+
|
201
|
+
|
202
|
+
end
|
203
|
+
|
204
|
+
EM.describe EM::Protocols::Redis, "connected to a db containing a list" do
|
205
|
+
|
206
|
+
before do
|
207
|
+
@c = EM::Protocols::Redis.connect :db => 14
|
208
|
+
@c.flushdb
|
209
|
+
@c.lpush "foo", "c"
|
210
|
+
@c.lpush "foo", "b"
|
211
|
+
@c.lpush "foo", "a"
|
212
|
+
end
|
213
|
+
|
214
|
+
should "be able to 'lset' a list member and 'lindex' to retrieve it" do
|
215
|
+
@c.lset("foo", 1, "bar") do |r|
|
216
|
+
@c.lindex("foo", 1) do |r|
|
217
|
+
r.should == "bar"
|
218
|
+
done
|
219
|
+
end
|
220
|
+
end
|
221
|
+
end
|
222
|
+
|
223
|
+
should "be able to 'rpush' onto the tail of the list" do
|
224
|
+
@c.rpush "foo", "d" do |r|
|
225
|
+
r.should == "OK"
|
226
|
+
@c.rpop "foo" do |r|
|
227
|
+
r.should == "d"
|
228
|
+
done
|
229
|
+
end
|
230
|
+
end
|
231
|
+
end
|
232
|
+
|
233
|
+
should "be able to 'lpush' onto the head of the list" do
|
234
|
+
@c.lpush "foo", "d" do |r|
|
235
|
+
r.should == "OK"
|
236
|
+
@c.lpop "foo" do |r|
|
237
|
+
r.should == "d"
|
238
|
+
done
|
239
|
+
end
|
240
|
+
end
|
241
|
+
end
|
242
|
+
|
243
|
+
should "be able to 'rpop' off the tail of the list" do
|
244
|
+
@c.rpop("foo") do |r|
|
245
|
+
r.should == "c"
|
246
|
+
done
|
247
|
+
end
|
248
|
+
end
|
249
|
+
|
250
|
+
should "be able to 'lpop' off the tail of the list" do
|
251
|
+
@c.lpop("foo") do |r|
|
252
|
+
r.should == "a"
|
253
|
+
done
|
254
|
+
end
|
255
|
+
end
|
256
|
+
|
257
|
+
should "be able to get a range of values from a list" do
|
258
|
+
@c.lrange("foo", 0, 1) do |r|
|
259
|
+
r.should == ["a", "b"]
|
260
|
+
done
|
261
|
+
end
|
262
|
+
end
|
263
|
+
|
264
|
+
should "be able to 'ltrim' a list" do
|
265
|
+
@c.ltrim("foo", 0, 1) do |r|
|
266
|
+
r.should == "OK"
|
267
|
+
@c.llen("foo") do |r|
|
268
|
+
r.should == 2
|
269
|
+
done
|
270
|
+
end
|
271
|
+
end
|
272
|
+
end
|
273
|
+
|
274
|
+
should "be able to 'rem' a list element" do
|
275
|
+
@c.lrem("foo", 0, "a") do |r|
|
276
|
+
r.should == 1
|
277
|
+
@c.llen("foo") do |r|
|
278
|
+
r.should == 2
|
279
|
+
done
|
280
|
+
end
|
281
|
+
end
|
282
|
+
end
|
283
|
+
|
284
|
+
should "be able to detect the type of a list" do
|
285
|
+
@c.type "foo" do |r|
|
286
|
+
r.should == "list"
|
287
|
+
done
|
288
|
+
end
|
289
|
+
end
|
290
|
+
|
291
|
+
end
|
292
|
+
|
293
|
+
EM.describe EM::Protocols::Redis, "connected to a db containing two sets" do
|
294
|
+
before do
|
295
|
+
@c = EM::Protocols::Redis.connect :db => 14
|
296
|
+
@c.flushdb
|
297
|
+
@c.sadd "foo", "a"
|
298
|
+
@c.sadd "foo", "b"
|
299
|
+
@c.sadd "foo", "c"
|
300
|
+
@c.sadd "bar", "c"
|
301
|
+
@c.sadd "bar", "d"
|
302
|
+
@c.sadd "bar", "e"
|
303
|
+
end
|
304
|
+
|
305
|
+
should "be able to find a set's cardinality" do
|
306
|
+
@c.scard("foo") do |r|
|
307
|
+
r.should == 3
|
308
|
+
done
|
309
|
+
end
|
310
|
+
end
|
311
|
+
|
312
|
+
should "be able to add a new member to a set unless it is a duplicate" do
|
313
|
+
@c.sadd("foo", "d") do |r|
|
314
|
+
r.should == true # success
|
315
|
+
@c.sadd("foo", "a") do |r|
|
316
|
+
r.should == false # failure
|
317
|
+
@c.scard("foo") do |r|
|
318
|
+
r.should == 4
|
319
|
+
done
|
320
|
+
end
|
321
|
+
end
|
322
|
+
end
|
323
|
+
end
|
324
|
+
|
325
|
+
should "be able to remove a set member if it exists" do
|
326
|
+
@c.srem("foo", "a") do |r|
|
327
|
+
r.should == true
|
328
|
+
@c.srem("foo", "z") do |r|
|
329
|
+
r.should == false
|
330
|
+
@c.scard("foo") do |r|
|
331
|
+
r.should == 2
|
332
|
+
done
|
333
|
+
end
|
334
|
+
end
|
335
|
+
end
|
336
|
+
end
|
337
|
+
|
338
|
+
should "be able to retrieve a set's members" do
|
339
|
+
@c.smembers("foo") do |r|
|
340
|
+
r.sort.should == ["a", "b", "c"]
|
341
|
+
done
|
342
|
+
end
|
343
|
+
end
|
344
|
+
|
345
|
+
should "be able to detect set membership" do
|
346
|
+
@c.sismember("foo", "a") do |r|
|
347
|
+
r.should == true
|
348
|
+
@c.sismember("foo", "z") do |r|
|
349
|
+
r.should == false
|
350
|
+
done
|
351
|
+
end
|
352
|
+
end
|
353
|
+
end
|
354
|
+
|
355
|
+
should "be able to find the sets' intersection" do
|
356
|
+
@c.sinter("foo", "bar") do |r|
|
357
|
+
r.should == ["c"]
|
358
|
+
done
|
359
|
+
end
|
360
|
+
end
|
361
|
+
|
362
|
+
should "be able to find and store the sets' intersection" do
|
363
|
+
@c.sinterstore("baz", "foo", "bar") do |r|
|
364
|
+
r.should == 1
|
365
|
+
@c.smembers("baz") do |r|
|
366
|
+
r.should == ["c"]
|
367
|
+
done
|
368
|
+
end
|
369
|
+
end
|
370
|
+
end
|
371
|
+
|
372
|
+
should "be able to find the sets' union" do
|
373
|
+
@c.sunion("foo", "bar") do |r|
|
374
|
+
r.sort.should == ["a","b","c","d","e"]
|
375
|
+
done
|
376
|
+
end
|
377
|
+
end
|
378
|
+
|
379
|
+
should "be able to find and store the sets' union" do
|
380
|
+
@c.sunionstore("baz", "foo", "bar") do |r|
|
381
|
+
r.should == 5
|
382
|
+
@c.smembers("baz") do |r|
|
383
|
+
r.sort.should == ["a","b","c","d","e"]
|
384
|
+
done
|
385
|
+
end
|
386
|
+
end
|
387
|
+
end
|
388
|
+
|
389
|
+
should "be able to detect the type of a set" do
|
390
|
+
@c.type "foo" do |r|
|
391
|
+
r.should == "set"
|
392
|
+
done
|
393
|
+
end
|
394
|
+
end
|
395
|
+
|
396
|
+
end
|
397
|
+
|
398
|
+
|
399
|
+
EM.describe EM::Protocols::Redis, "connected to a db containing three linked lists" do
|
400
|
+
before do
|
401
|
+
@c = EM::Protocols::Redis.connect :db => 14
|
402
|
+
@c.flushdb
|
403
|
+
@c.rpush "foo", "a"
|
404
|
+
@c.rpush "foo", "b"
|
405
|
+
@c.set "a_sort", "2"
|
406
|
+
@c.set "b_sort", "1"
|
407
|
+
@c.set "a_data", "foo"
|
408
|
+
@c.set "b_data", "bar"
|
409
|
+
end
|
410
|
+
|
411
|
+
should "be able to collate a sorted set of data" do
|
412
|
+
@c.sort("foo", :by => "*_sort", :get => "*_data") do |r|
|
413
|
+
r.should == ["bar", "foo"]
|
414
|
+
done
|
415
|
+
end
|
416
|
+
end
|
417
|
+
|
418
|
+
should "be able to get keys selectively" do
|
419
|
+
@c.keys "a_*" do |r|
|
420
|
+
r.should == ["a_sort", "a_data"]
|
421
|
+
done
|
422
|
+
end
|
423
|
+
end
|
424
|
+
end
|
425
|
+
|
426
|
+
EM.describe EM::Protocols::Redis, "when reconnecting" do
|
427
|
+
before do
|
428
|
+
@c = EM::Protocols::Redis.connect :db => 14
|
429
|
+
@c.flushdb
|
430
|
+
end
|
431
|
+
|
432
|
+
should "select previously selected datase" do
|
433
|
+
#simulate disconnect
|
434
|
+
@c.set('foo', 'a') { @c.close_connection_after_writing }
|
435
|
+
|
436
|
+
EM.add_timer(2) do
|
437
|
+
@c.get('foo') do |r|
|
438
|
+
r.should == 'a'
|
439
|
+
@c.get('non_existing') do |r|
|
440
|
+
r.should == nil
|
441
|
+
done
|
442
|
+
end
|
443
|
+
end
|
444
|
+
end
|
445
|
+
end
|
446
|
+
end
|