Skip to content

Commit

Permalink
Support transactions for cluster client
Browse files Browse the repository at this point in the history
  • Loading branch information
supercaracal committed Sep 30, 2023
1 parent c888c74 commit dda95f8
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 6 deletions.
3 changes: 3 additions & 0 deletions cluster/lib/redis/cluster.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ def initialize(errors, error_message = 'Command errors were replied on any node'
class AmbiguousNodeError < BaseError
end

class TransactionConsistencyError < BaseError
end

def connection
raise NotImplementedError, "Redis::Cluster doesn't implement #connection"
end
Expand Down
3 changes: 2 additions & 1 deletion cluster/lib/redis/cluster/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ class Client < RedisClient::Cluster
RedisClient::Cluster::InitialSetupError => Redis::Cluster::InitialSetupError,
RedisClient::Cluster::OrchestrationCommandNotSupported => Redis::Cluster::OrchestrationCommandNotSupported,
RedisClient::Cluster::AmbiguousNodeError => Redis::Cluster::AmbiguousNodeError,
RedisClient::Cluster::ErrorCollection => Redis::Cluster::CommandErrorCollection
RedisClient::Cluster::ErrorCollection => Redis::Cluster::CommandErrorCollection,
RedisClient::Cluster::Transaction::ConsistencyError => Redis::Cluster::TransactionConsistencyError
).freeze

class << self
Expand Down
2 changes: 1 addition & 1 deletion cluster/redis-clustering.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,5 @@ Gem::Specification.new do |s|
s.required_ruby_version = '>= 2.7.0'

s.add_runtime_dependency('redis', s.version)
s.add_runtime_dependency('redis-cluster-client', '>= 0.6.1')
s.add_runtime_dependency('redis-cluster-client', '>= 0.7.0')
end
43 changes: 40 additions & 3 deletions cluster/test/client_transactions_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,46 @@
class TestClusterClientTransactions < Minitest::Test
include Helper::Cluster

def test_cluster_client_does_not_support_transaction
assert_raises(Redis::Cluster::AmbiguousNodeError) do
redis.multi { |r| r.set('key', 'foo') }
def test_cluster_client_does_support_transaction_by_single_key
actual = redis.multi do |r|
r.set('counter', '0')
r.incr('counter')
r.incr('counter')
end

assert_equal(['OK', 1, 2], actual)
assert_equal('2', redis.get('counter'))
end

def test_cluster_client_does_support_transaction_by_hashtag
actual = redis.multi do |r|
r.mset('{key}1', 1, '{key}2', 2)
r.mset('{key}3', 3, '{key}4', 4)
end

assert_equal(%w[OK OK], actual)
assert_equal(%w[1 2 3 4], redis.mget('{key}1', '{key}2', '{key}3', '{key}4'))
end

def test_cluster_client_does_not_support_transaction_by_multiple_keys
assert_raises(Redis::Cluster::TransactionConsistencyError) do
redis.multi do |r|
r.set('key1', 1)
r.set('key2', 2)
r.set('key3', 3)
r.set('key4', 4)
end
end

assert_raises(Redis::Cluster::TransactionConsistencyError) do
redis.multi do |r|
r.mset('key1', 1, 'key2', 2)
r.mset('key3', 3, 'key4', 4)
end
end

(1..4).each do |i|
assert_nil(redis.get("key#{i}"))
end
end
end
8 changes: 7 additions & 1 deletion cluster/test/commands_on_transactions_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,15 @@ def test_exec
end

def test_multi
assert_raises(Redis::Cluster::AmbiguousNodeError) do
assert_raises(LocalJumpError) do
redis.multi
end

assert_raises(ArgumentError) do
redis.multi {}
end

assert_equal([1], redis.multi { |r| r.incr('counter') })
end

def test_unwatch
Expand Down

0 comments on commit dda95f8

Please sign in to comment.