Skip to content

Commit

Permalink
Merge e9eb3ce into 7535981
Browse files Browse the repository at this point in the history
  • Loading branch information
snmgian committed Sep 27, 2018
2 parents 7535981 + e9eb3ce commit b9b4a59
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 5 deletions.
24 changes: 19 additions & 5 deletions lib/mock_redis/pipelined_wrapper.rb
Expand Up @@ -9,7 +9,7 @@ def respond_to?(method, include_private = false)
def initialize(db)
@db = db
@pipelined_futures = []
@in_pipeline = false
@nesting_level = 0
end

def initialize_copy(source)
Expand All @@ -19,7 +19,7 @@ def initialize_copy(source)
end

def method_missing(method, *args, &block)
if @in_pipeline
if in_pipeline?
future = MockRedis::Future.new([method, *args], block)
@pipelined_futures << future
future
Expand All @@ -29,9 +29,17 @@ def method_missing(method, *args, &block)
end

def pipelined(_options = {})
@in_pipeline = true
yield self
@in_pipeline = false
begin
@nesting_level += 1
yield self
ensure
@nesting_level -= 1
end

if in_pipeline?
return
end

responses = @pipelined_futures.flat_map do |future|
begin
result = if future.block
Expand All @@ -48,5 +56,11 @@ def pipelined(_options = {})
@pipelined_futures = []
responses
end

private

def in_pipeline?
@nesting_level > 0
end
end
end
26 changes: 26 additions & 0 deletions spec/commands/pipelined_spec.rb
Expand Up @@ -39,4 +39,30 @@
future.class.should be MockRedis::Future
end
end

context 'with nested pipelines' do
let(:key1) { 'hello' }
let(:key2) { 'world' }
let(:value1) { 'foo' }
let(:value2) { 'bar' }

before do
@redises.set key1, value1
@redises.set key2, value2
end

it 'returns results of the nested pipelines' do
results = @redises.pipelined do |pr1|
pr1.pipelined do |pr2|
pr2.get key1
end

pr1.pipelined do |pr2|
pr2.get key2
end
end

results.should == [value1, value2]
end
end
end

0 comments on commit b9b4a59

Please sign in to comment.