Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle block within multi block #185

Merged
merged 2 commits into from Apr 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/mock_redis/future.rb
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
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
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)
@future2 = 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
@future2.store_result(result)
@future2.value.should eq('BAR')
end
end
16 changes: 16 additions & 0 deletions spec/transactions_spec.rb
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 %w[BAR BAZ]
@redises.get('foo').should eq nil
@redises.get('fuu').should eq nil
end
end

context '#discard' do
Expand Down