Skip to content

Commit 6e54db6

Browse files
authored
refactor: tweak transaction method and test (#279)
1 parent 47529e7 commit 6e54db6

File tree

3 files changed

+25
-13
lines changed

3 files changed

+25
-13
lines changed

lib/redis_client/cluster.rb

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,7 @@ def pipelined
9090
end
9191

9292
def multi(watch: nil, &block)
93-
::RedisClient::Cluster::Transaction
94-
.new(@router, @command_builder)
95-
.find_node(&block)
96-
.multi(watch: watch, &block)
93+
::RedisClient::Cluster::Transaction.new(@router, @command_builder).execute(watch: watch, &block)
9794
end
9895

9996
def pubsub

lib/redis_client/cluster/transaction.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ def call_once_v(command, &_)
3333
ensure_node_key(command)
3434
end
3535

36-
def find_node
36+
def execute(watch: nil, &block)
3737
yield self
3838
raise ArgumentError, 'empty transaction' if @node_key.nil?
3939

40-
@router.find_node(@node_key)
40+
@router.find_node(@node_key).multi(watch: watch, &block)
4141
end
4242

4343
private

test/redis_client/test_cluster.rb

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -188,15 +188,14 @@ def test_pubsub_without_subscription
188188
end
189189

190190
def test_transaction_with_single_key
191-
want = ['OK', 1, 2, '2']
192191
got = @client.multi do |t|
193192
t.call('SET', 'counter', '0')
194193
t.call('INCR', 'counter')
195194
t.call('INCR', 'counter')
196-
t.call('GET', 'counter')
197195
end
198196

199-
assert_equal(want, got)
197+
assert_equal(['OK', 1, 2], got)
198+
assert_equal('2', @client.call('GET', 'counter'))
200199
end
201200

202201
def test_transaction_with_multiple_key
@@ -215,25 +214,41 @@ def test_transaction_with_multiple_key
215214

216215
def test_transaction_with_empty_block
217216
assert_raises(ArgumentError) { @client.multi {} }
217+
assert_raises(LocalJumpError) { @client.multi }
218+
end
219+
220+
def test_transaction_with_keyless_commands
221+
assert_raises(::RedisClient::Cluster::Transaction::ConsistencyError) do
222+
@client.multi do |t|
223+
t.call('ECHO', 'foo')
224+
t.call('ECHO', 'bar')
225+
end
226+
end
218227
end
219228

220229
def test_transaction_with_hashtag
221-
want = ['OK', 'OK', %w[1 2 3 4]]
222230
got = @client.multi do |t|
223231
t.call('MSET', '{key}1', '1', '{key}2', '2')
224232
t.call('MSET', '{key}3', '3', '{key}4', '4')
225-
t.call('MGET', '{key}1', '{key}2', '{key}3', '{key}4')
226233
end
227234

228-
assert_equal(want, got)
235+
assert_equal(%w[OK OK], got)
236+
assert_equal(%w[1 2 3 4], @client.call('MGET', '{key}1', '{key}2', '{key}3', '{key}4'))
229237
end
230238

231239
def test_transaction_without_hashtag
232240
assert_raises(::RedisClient::Cluster::Transaction::ConsistencyError) do
233241
@client.multi do |t|
234242
t.call('MSET', 'key1', '1', 'key2', '2')
235243
t.call('MSET', 'key3', '3', 'key4', '4')
236-
t.call('MGET', 'key1', 'key2', 'key3', 'key4')
244+
end
245+
end
246+
247+
assert_raises(::RedisClient::CommandError, 'CROSSSLOT keys') do
248+
@client.multi do |t|
249+
t.call('MSET', 'key1', '1', 'key2', '2')
250+
t.call('MSET', 'key1', '1', 'key3', '3')
251+
t.call('MSET', 'key1', '1', 'key4', '4')
237252
end
238253
end
239254

0 commit comments

Comments
 (0)