From ad4f2cfd11aad200f46d2205db35a348b2a347ad Mon Sep 17 00:00:00 2001 From: Taishi Kasuga Date: Tue, 20 Feb 2024 13:18:50 +0900 Subject: [PATCH] fix: make an error message easy to understand for the watch command --- lib/redis_client/cluster/router.rb | 2 ++ test/redis_client/test_cluster.rb | 8 +++++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/redis_client/cluster/router.rb b/lib/redis_client/cluster/router.rb index 45fd13c..94963d4 100644 --- a/lib/redis_client/cluster/router.rb +++ b/lib/redis_client/cluster/router.rb @@ -313,6 +313,8 @@ def send_pubsub_command(method, command, args, &block) # rubocop:disable Metrics # for redis-rb def send_watch_command(command) + raise ::RedisClient::Cluster::Transaction::ConsistencyError, 'A block required. And you need to use the block argument as a client for the transaction.' unless block_given? + ::RedisClient::Cluster::OptimisticLocking.new(self).watch(command[1..]) do |c, slot| transaction = ::RedisClient::Cluster::Transaction.new( self, @command_builder, node: c, slot: slot diff --git a/test/redis_client/test_cluster.rb b/test/redis_client/test_cluster.rb index 84b77ca..ec2944b 100644 --- a/test/redis_client/test_cluster.rb +++ b/test/redis_client/test_cluster.rb @@ -364,7 +364,7 @@ def test_transaction_in_race_condition end # for redis-rb - def test_transaction_with_standalone_watch_command + def test_transaction_with_dedicated_watch_command @client.call('MSET', '{key}1', '0', '{key}2', '0') got = @client.call('WATCH', '{key}1', '{key}2') do |tx| @@ -378,6 +378,12 @@ def test_transaction_with_standalone_watch_command assert_equal(%w[1 2], @client.call('MGET', '{key}1', '{key}2')) end + def test_transaction_with_dedicated_watch_command_without_block + assert_raises(::RedisClient::Cluster::Transaction::ConsistencyError) do + @client.call('WATCH', '{key}1', '{key}2') + end + end + def test_pubsub_without_subscription pubsub = @client.pubsub assert_nil(pubsub.next_event(0.01))