Skip to content

Commit

Permalink
Merge 77f90e7 into 72a1c34
Browse files Browse the repository at this point in the history
  • Loading branch information
hieuk09 committed Apr 25, 2020
2 parents 72a1c34 + 77f90e7 commit 8a2296e
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lib/mock_redis/future.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def value

def store_result(result)
@result_set = true
@result = result
@result = @block ? @block.call(result) : result
end
end
end
4 changes: 2 additions & 2 deletions lib/mock_redis/transaction_wrapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def initialize(db)

def method_missing(method, *args, &block)
if in_multi?
future = MockRedis::Future.new([method, *args])
future = MockRedis::Future.new([method, *args], block)
@transaction_futures << future

if @multi_block_given
Expand Down Expand Up @@ -60,7 +60,7 @@ def exec
begin
result = send(*future.command)
future.store_result(result)
result
future.value
rescue StandardError => e
e
end
Expand Down
12 changes: 11 additions & 1 deletion spec/commands/future_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
describe MockRedis::Future do
let(:command) { [:get, 'foo'] }
let(:result) { 'bar' }
before { @future = MockRedis::Future.new(command) }
let(:block) { ->(value) { value.upcase } }

before do
@future = MockRedis::Future.new(command)
@future_2 = MockRedis::Future.new(command, block)
end

it 'remembers the command' do
@future.command.should eq(command)
Expand All @@ -17,4 +22,9 @@
@future.store_result(result)
@future.value.should eq(result)
end

it 'executes the block on the value if block is passed in' do
@future_2.store_result(result)
@future_2.value.should eq('BAR')
end
end
16 changes: 16 additions & 0 deletions spec/transactions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,22 @@
@redises.get('counter').should eq '6'
@redises.get('test').should eq '1'
end

it 'allows blocks within multi blocks' do
@redises.set('foo', 'bar')
@redises.set('fuu', 'baz')

result = nil

@redises.multi do |r|
result = r.mget('foo', 'fuu') { |reply| reply.map(&:upcase) }
r.del('foo', 'fuu')
end

result.value.should eq ['BAR', 'BAZ']
@redises.get('foo').should eq nil
@redises.get('fuu').should eq nil
end
end

context '#discard' do
Expand Down

0 comments on commit 8a2296e

Please sign in to comment.