@@ -7,20 +7,20 @@ class TestClusterClientTransactions < Minitest::Test
77 include Helper ::Cluster
88
99 def test_cluster_client_does_support_transaction_by_single_key
10- actual = redis . multi do |r |
11- r . set ( 'counter' , '0' )
12- r . incr ( 'counter' )
13- r . incr ( 'counter' )
10+ actual = redis . multi do |tx |
11+ tx . set ( 'counter' , '0' )
12+ tx . incr ( 'counter' )
13+ tx . incr ( 'counter' )
1414 end
1515
1616 assert_equal ( [ 'OK' , 1 , 2 ] , actual )
1717 assert_equal ( '2' , redis . get ( 'counter' ) )
1818 end
1919
2020 def test_cluster_client_does_support_transaction_by_hashtag
21- actual = redis . multi do |r |
22- r . mset ( '{key}1' , 1 , '{key}2' , 2 )
23- r . mset ( '{key}3' , 3 , '{key}4' , 4 )
21+ actual = redis . multi do |tx |
22+ tx . mset ( '{key}1' , 1 , '{key}2' , 2 )
23+ tx . mset ( '{key}3' , 3 , '{key}4' , 4 )
2424 end
2525
2626 assert_equal ( %w[ OK OK ] , actual )
@@ -29,18 +29,18 @@ def test_cluster_client_does_support_transaction_by_hashtag
2929
3030 def test_cluster_client_does_not_support_transaction_by_multiple_keys
3131 assert_raises ( Redis ::Cluster ::TransactionConsistencyError ) do
32- redis . multi do |r |
33- r . set ( 'key1' , 1 )
34- r . set ( 'key2' , 2 )
35- r . set ( 'key3' , 3 )
36- r . set ( 'key4' , 4 )
32+ redis . multi do |tx |
33+ tx . set ( 'key1' , 1 )
34+ tx . set ( 'key2' , 2 )
35+ tx . set ( 'key3' , 3 )
36+ tx . set ( 'key4' , 4 )
3737 end
3838 end
3939
4040 assert_raises ( Redis ::Cluster ::TransactionConsistencyError ) do
41- redis . multi do |r |
42- r . mset ( 'key1' , 1 , 'key2' , 2 )
43- r . mset ( 'key3' , 3 , 'key4' , 4 )
41+ redis . multi do |tx |
42+ tx . mset ( 'key1' , 1 , 'key2' , 2 )
43+ tx . mset ( 'key3' , 3 , 'key4' , 4 )
4444 end
4545 end
4646
@@ -63,10 +63,50 @@ def test_cluster_client_does_support_transaction_with_optimistic_locking
6363 another . resume
6464 v1 = redis . get ( '{key}1' )
6565 v2 = redis . get ( '{key}2' )
66- tx . call ( 'SET' , '{key}1' , v2 )
67- tx . call ( 'SET' , '{key}2' , v1 )
66+ tx . set ( '{key}1' , v2 )
67+ tx . set ( '{key}2' , v1 )
6868 end
6969
7070 assert_equal %w[ 3 4 ] , redis . mget ( '{key}1' , '{key}2' )
7171 end
72+
73+ def test_cluster_client_can_be_used_compatible_with_standalone_client
74+ redis . set ( '{my}key' , 'value' )
75+ redis . set ( '{my}counter' , '0' )
76+ redis . watch ( '{my}key' , '{my}counter' ) do |client |
77+ if redis . get ( '{my}key' ) == 'value'
78+ client . multi do |tx |
79+ tx . set ( '{my}key' , 'updated value' )
80+ tx . incr ( '{my}counter' )
81+ end
82+ else
83+ client . unwatch
84+ end
85+ end
86+
87+ assert_equal ( 'updated value' , redis . get ( '{my}key' ) )
88+ assert_equal ( '1' , redis . get ( '{my}counter' ) )
89+
90+ another = Fiber . new do
91+ cli = build_another_client
92+ cli . set ( '{my}key' , 'another value' )
93+ cli . close
94+ Fiber . yield
95+ end
96+
97+ redis . watch ( '{my}key' , '{my}counter' ) do |client |
98+ another . resume
99+ if redis . get ( '{my}key' ) == 'value'
100+ client . multi do |tx |
101+ tx . set ( '{my}key' , 'latest value' )
102+ tx . incr ( '{my}counter' )
103+ end
104+ else
105+ client . unwatch
106+ end
107+ end
108+
109+ assert_equal ( 'another value' , redis . get ( '{my}key' ) )
110+ assert_equal ( '1' , redis . get ( '{my}counter' ) )
111+ end
72112end
0 commit comments