Skip to content

Commit

Permalink
fix: return empty array if the transaction is empty (#349)
Browse files Browse the repository at this point in the history
  • Loading branch information
supercaracal committed Apr 16, 2024
1 parent 60d7f68 commit 2e0170c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
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

0 comments on commit 2e0170c

Please sign in to comment.