Skip to content

Commit 2e0170c

Browse files
authored
fix: return empty array if the transaction is empty (#349)
1 parent 60d7f68 commit 2e0170c

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

lib/redis_client/cluster/transaction.rb

+13-3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ class Cluster
88
class Transaction
99
ConsistencyError = Class.new(::RedisClient::Error)
1010
MAX_REDIRECTION = 2
11+
EMPTY_ARRAY = [].freeze
1112

1213
def initialize(router, command_builder, node: nil, slot: nil, asking: false)
1314
@router = router
@@ -62,10 +63,10 @@ def call_once_v(command, &block)
6263
def execute
6364
@pending_commands.each(&:call)
6465

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

68-
settle
69+
commit
6970
end
7071

7172
private
@@ -92,8 +93,17 @@ def prepare_tx
9293
@pending_commands.clear
9394
end
9495

95-
def settle
96+
def commit
9697
@pipeline.call('EXEC')
98+
settle
99+
end
100+
101+
def cancel
102+
@pipeline.call('DISCARD')
103+
settle
104+
end
105+
106+
def settle
97107
# If we needed ASKING on the watch, we need ASKING on the multi as well.
98108
@node.call('ASKING') if @asking
99109
# Don't handle redirections at this level if we're in a watch (the watcher handles redirections

test/redis_client/test_cluster.rb

+13-2
Original file line numberDiff line numberDiff line change
@@ -224,11 +224,22 @@ def test_transaction_with_multiple_key
224224
end
225225
end
226226

227-
def test_transaction_with_empty_block
228-
assert_raises(ArgumentError) { @client.multi {} }
227+
def test_transaction_without_block
229228
assert_raises(LocalJumpError) { @client.multi }
230229
end
231230

231+
def test_transaction_with_empty_block
232+
@captured_commands.clear
233+
assert_empty(@client.multi {})
234+
assert_empty(@captured_commands.to_a.map(&:command).map(&:first))
235+
end
236+
237+
def test_transaction_with_empty_block_and_watch
238+
@captured_commands.clear
239+
assert_empty(@client.multi(watch: %w[key]) {})
240+
assert_equal(%w[WATCH MULTI EXEC], @captured_commands.to_a.map(&:command).map(&:first))
241+
end
242+
232243
def test_transaction_with_only_keyless_commands
233244
assert_raises(::RedisClient::Cluster::Transaction::ConsistencyError) do
234245
@client.multi do |t|

0 commit comments

Comments
 (0)