Skip to content

fix: return empty array if the transaction is empty #349

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

Merged
merged 1 commit into from
Apr 16, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions lib/redis_client/cluster/transaction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class Cluster
class Transaction
ConsistencyError = Class.new(::RedisClient::Error)
MAX_REDIRECTION = 2
EMPTY_ARRAY = [].freeze

def initialize(router, command_builder, node: nil, slot: nil, asking: false)
@router = router
Expand Down Expand Up @@ -62,10 +63,10 @@ def call_once_v(command, &block)
def execute
@pending_commands.each(&:call)

raise ArgumentError, 'empty transaction' if @pipeline._empty?
return EMPTY_ARRAY if @pipeline._empty?
raise ConsistencyError, "couldn't determine the node: #{@pipeline._commands}" if @node.nil?

settle
commit
end

private
Expand All @@ -92,8 +93,17 @@ def prepare_tx
@pending_commands.clear
end

def settle
def commit
@pipeline.call('EXEC')
settle
end

def cancel
@pipeline.call('DISCARD')
settle
end

def settle
# If we needed ASKING on the watch, we need ASKING on the multi as well.
@node.call('ASKING') if @asking
# Don't handle redirections at this level if we're in a watch (the watcher handles redirections
Expand Down
15 changes: 13 additions & 2 deletions test/redis_client/test_cluster.rb
Original file line number Diff line number Diff line change
Expand Up @@ -224,11 +224,22 @@ def test_transaction_with_multiple_key
end
end

def test_transaction_with_empty_block
assert_raises(ArgumentError) { @client.multi {} }
def test_transaction_without_block
assert_raises(LocalJumpError) { @client.multi }
end

def test_transaction_with_empty_block
@captured_commands.clear
assert_empty(@client.multi {})
assert_empty(@captured_commands.to_a.map(&:command).map(&:first))
end

def test_transaction_with_empty_block_and_watch
@captured_commands.clear
assert_empty(@client.multi(watch: %w[key]) {})
assert_equal(%w[WATCH MULTI EXEC], @captured_commands.to_a.map(&:command).map(&:first))
end

def test_transaction_with_only_keyless_commands
assert_raises(::RedisClient::Cluster::Transaction::ConsistencyError) do
@client.multi do |t|
Expand Down