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.
@@ -1,3 +1,6 @@
1
+ ### 0.9.0
2
+ * Added futures support
3
+
1
4
  ### 0.8.2
2
5
  * Added support for Ruby 2.0.0, dropped support for Ruby 1.8.7
3
6
  * Changes for compatibility with JRuby
data/README.md CHANGED
@@ -34,6 +34,7 @@ of supported methods:
34
34
  * Sorted set methods: `zadd`, `zrank`, `zunionstore`, etc.
35
35
  * Expirations: `expire`, `ttl`, etc.
36
36
  * Transactions: `multi`, `exec`, `discard`
37
+ * Futures
37
38
 
38
39
  ## Mostly-Supported Commands
39
40
 
@@ -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
- @pipelined_commands = []
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
- @pipelined_commands = @pipelined_commands.clone
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
- @pipelined_commands << [method, *args]
24
- nil
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 = @pipelined_commands.map do |cmd|
56
+ responses = @pipelined_futures.map do |future|
35
57
  begin
36
- send(*cmd)
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
- @pipelined_commands = []
65
+ @pipelined_futures = []
42
66
  responses
43
67
  end
44
68
  end
@@ -1,3 +1,3 @@
1
1
  class MockRedis
2
- VERSION = '0.8.2'
2
+ VERSION = '0.9.0'
3
3
  end
@@ -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
- it "returns results of pipelined operations" do
13
- @redises.set "hello", "world"
14
- @redises.set "foo", "bar"
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
- results = @redises.pipelined do |redis|
17
- redis.get "hello"
18
- redis.get "foo"
18
+ before do
19
+ @redises.set key1, value1
20
+ @redises.set key2, value2
19
21
  end
20
22
 
21
- results.should == ["world", "bar"]
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
- - 8
9
- - 2
10
- version: 0.8.2
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-07-29 00:00:00 -07:00
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