Skip to content

Commit c05234a

Browse files
authored
test: ensure the transaction is legit durling the resharding (#319)
1 parent d1d5483 commit c05234a

File tree

3 files changed

+51
-5
lines changed

3 files changed

+51
-5
lines changed

lib/redis_client/cluster/transaction.rb

+21-3
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ def send_transaction(client, redirect:)
117117
end
118118

119119
def send_pipeline(client, redirect:)
120-
results = client.ensure_connected_cluster_scoped(retryable: @retryable) do |connection|
120+
replies = client.ensure_connected_cluster_scoped(retryable: @retryable) do |connection|
121121
commands = @pipeline._commands
122122
client.middlewares.call_pipelined(commands, client.config) do
123123
connection.call_pipelined(commands, nil)
@@ -128,8 +128,26 @@ def send_pipeline(client, redirect:)
128128
end
129129
end
130130

131-
@pipeline._coerce!(results)
132-
results[watch? ? -2 : -1]
131+
offset = watch? ? 2 : 1
132+
coerce_results!(replies[-offset], offset)
133+
end
134+
135+
def coerce_results!(results, offset)
136+
results.each_with_index do |result, index|
137+
if result.is_a?(::RedisClient::CommandError)
138+
result._set_command(@pipeline._commands[index + offset])
139+
raise result
140+
end
141+
142+
next if @pipeline._blocks.nil?
143+
144+
block = @pipeline._blocks[index + offset]
145+
next if block.nil?
146+
147+
results[index] = block.call(result)
148+
end
149+
150+
results
133151
end
134152

135153
def handle_command_error!(commands, err)

test/redis_client/test_cluster.rb

+25
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,31 @@ def test_transaction_with_meaningless_watch
295295
assert_equal(%w[1 2], @client.call('MGET', '{key}1', '{key}2'))
296296
end
297297

298+
def test_transaction_with_error
299+
@client.call('SET', 'key1', 'x')
300+
301+
assert_raises(::RedisClient::CommandError) do
302+
@client.multi do |tx|
303+
tx.call('SET', 'key1', 'aaa')
304+
tx.call('MYBAD', 'key1', 'bbb')
305+
end
306+
end
307+
308+
assert_equal('x', @client.call('GET', 'key1'))
309+
end
310+
311+
def test_transaction_with_block
312+
@client.call('MSET', '{key}1', 'a', '{key}2', 'b', '{key}3', 'c')
313+
314+
got = @client.multi do |tx|
315+
tx.call('GET', '{key}1') { |x| "#{x}aa" }
316+
tx.call('GET', '{key}2') { |x| "#{x}bb" }
317+
tx.call('GET', '{key}3') { |x| "#{x}cc" }
318+
end
319+
320+
assert_equal(%w[aaa bbb ccc], got)
321+
end
322+
298323
def test_pubsub_without_subscription
299324
pubsub = @client.pubsub
300325
assert_nil(pubsub.next_event(0.01))

test/test_against_cluster_state.rb

+5-2
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,14 @@ def test_the_state_of_cluster_resharding_with_pipelining
6262
def test_the_state_of_cluster_resharding_with_transaction
6363
do_resharding_test do |keys|
6464
@client.multi do |tx|
65-
keys.each { |key| tx.call('SET', key, key) }
65+
keys.each do |key|
66+
tx.call('SET', key, '0')
67+
tx.call('INCR', key)
68+
end
6669
end
6770

6871
keys.each do |key|
69-
want = key
72+
want = '1'
7073
got = @client.call('GET', key)
7174
assert_equal(want, got, "Case: GET: #{key}")
7275
end

0 commit comments

Comments
 (0)