mock_redis 0.8.2 → 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGELOG.md +3 -0
- data/README.md +1 -0
- data/lib/mock_redis/pipelined_wrapper.rb +31 -7
- data/lib/mock_redis/version.rb +1 -1
- data/spec/commands/future_spec.rb +20 -0
- data/spec/commands/pipelined_spec.rb +26 -7
- metadata +6 -4
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -1,4 +1,25 @@
|
|
1
1
|
class MockRedis
|
2
|
+
class FutureNotReady < RuntimeError; end
|
3
|
+
|
4
|
+
class Future
|
5
|
+
attr_reader :command
|
6
|
+
|
7
|
+
def initialize(command)
|
8
|
+
@command = command
|
9
|
+
@result_set = false
|
10
|
+
end
|
11
|
+
|
12
|
+
def value
|
13
|
+
raise FutureNotReady unless @result_set
|
14
|
+
@result
|
15
|
+
end
|
16
|
+
|
17
|
+
def set_result(result)
|
18
|
+
@result_set = true
|
19
|
+
@result = result
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
2
23
|
class PipelinedWrapper
|
3
24
|
include UndefRedisMethods
|
4
25
|
|
@@ -8,20 +29,21 @@ class MockRedis
|
|
8
29
|
|
9
30
|
def initialize(db)
|
10
31
|
@db = db
|
11
|
-
@
|
32
|
+
@pipelined_futures = []
|
12
33
|
@in_pipeline = false
|
13
34
|
end
|
14
35
|
|
15
36
|
def initialize_copy(source)
|
16
37
|
super
|
17
38
|
@db = @db.clone
|
18
|
-
@
|
39
|
+
@pipelined_futures = @pipelined_futures.clone
|
19
40
|
end
|
20
41
|
|
21
42
|
def method_missing(method, *args, &block)
|
22
43
|
if @in_pipeline
|
23
|
-
|
24
|
-
|
44
|
+
future = MockRedis::Future.new([method, *args])
|
45
|
+
@pipelined_futures << future
|
46
|
+
future
|
25
47
|
else
|
26
48
|
@db.send(method, *args, &block)
|
27
49
|
end
|
@@ -31,14 +53,16 @@ class MockRedis
|
|
31
53
|
@in_pipeline = true
|
32
54
|
yield self
|
33
55
|
@in_pipeline = false
|
34
|
-
responses = @
|
56
|
+
responses = @pipelined_futures.map do |future|
|
35
57
|
begin
|
36
|
-
send(*
|
58
|
+
result = send(*future.command)
|
59
|
+
future.set_result(result)
|
60
|
+
result
|
37
61
|
rescue => e
|
38
62
|
e
|
39
63
|
end
|
40
64
|
end
|
41
|
-
@
|
65
|
+
@pipelined_futures = []
|
42
66
|
responses
|
43
67
|
end
|
44
68
|
end
|
data/lib/mock_redis/version.rb
CHANGED
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe MockRedis::Future do
|
4
|
+
let(:command) { [:get, 'foo'] }
|
5
|
+
let(:result) { 'bar' }
|
6
|
+
before { @future = MockRedis::Future.new(command) }
|
7
|
+
|
8
|
+
it 'remembers the command' do
|
9
|
+
@future.command.should eq(command)
|
10
|
+
end
|
11
|
+
|
12
|
+
it 'raises an error if the value is requested before the result is set' do
|
13
|
+
expect{@future.value}.to raise_error(RuntimeError)
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'returns the value after the result has been set' do
|
17
|
+
@future.set_result(result)
|
18
|
+
@future.value.should eq(result)
|
19
|
+
end
|
20
|
+
end
|
@@ -9,15 +9,34 @@ describe '#pipelined' do
|
|
9
9
|
res.should == true
|
10
10
|
end
|
11
11
|
|
12
|
-
|
13
|
-
|
14
|
-
|
12
|
+
context 'with a few added data' do
|
13
|
+
let(:key1) { "hello" }
|
14
|
+
let(:key2) { "world" }
|
15
|
+
let(:value1) { "foo" }
|
16
|
+
let(:value2) { "bar" }
|
15
17
|
|
16
|
-
|
17
|
-
|
18
|
-
|
18
|
+
before do
|
19
|
+
@redises.set key1, value1
|
20
|
+
@redises.set key2, value2
|
19
21
|
end
|
20
22
|
|
21
|
-
results
|
23
|
+
it 'returns results of pipelined operations' do
|
24
|
+
results = @redises.pipelined do |redis|
|
25
|
+
redis.get key1
|
26
|
+
redis.get key2
|
27
|
+
end
|
28
|
+
|
29
|
+
results.should == [ value1, value2 ]
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'returns futures' do
|
33
|
+
future = nil
|
34
|
+
|
35
|
+
@redises.mock.pipelined do |redis|
|
36
|
+
future = redis.get key1
|
37
|
+
end
|
38
|
+
|
39
|
+
future.class.should be MockRedis::Future
|
40
|
+
end
|
22
41
|
end
|
23
42
|
end
|
metadata
CHANGED
@@ -5,9 +5,9 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
-
|
10
|
-
version: 0.
|
8
|
+
- 9
|
9
|
+
- 0
|
10
|
+
version: 0.9.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Causes Engineering
|
@@ -16,7 +16,7 @@ autorequire:
|
|
16
16
|
bindir: bin
|
17
17
|
cert_chain: []
|
18
18
|
|
19
|
-
date: 2013-
|
19
|
+
date: 2013-08-08 00:00:00 -07:00
|
20
20
|
default_executable:
|
21
21
|
dependencies:
|
22
22
|
- !ruby/object:Gem::Dependency
|
@@ -128,6 +128,7 @@ files:
|
|
128
128
|
- spec/commands/expireat_spec.rb
|
129
129
|
- spec/commands/flushall_spec.rb
|
130
130
|
- spec/commands/flushdb_spec.rb
|
131
|
+
- spec/commands/future_spec.rb
|
131
132
|
- spec/commands/get_spec.rb
|
132
133
|
- spec/commands/getbit_spec.rb
|
133
134
|
- spec/commands/getrange_spec.rb
|
@@ -294,6 +295,7 @@ test_files:
|
|
294
295
|
- spec/commands/expireat_spec.rb
|
295
296
|
- spec/commands/flushall_spec.rb
|
296
297
|
- spec/commands/flushdb_spec.rb
|
298
|
+
- spec/commands/future_spec.rb
|
297
299
|
- spec/commands/get_spec.rb
|
298
300
|
- spec/commands/getbit_spec.rb
|
299
301
|
- spec/commands/getrange_spec.rb
|