@@ -7,20 +7,20 @@ class TestClusterClientTransactions < Minitest::Test
7
7
include Helper ::Cluster
8
8
9
9
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' )
14
14
end
15
15
16
16
assert_equal ( [ 'OK' , 1 , 2 ] , actual )
17
17
assert_equal ( '2' , redis . get ( 'counter' ) )
18
18
end
19
19
20
20
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 )
24
24
end
25
25
26
26
assert_equal ( %w[ OK OK ] , actual )
@@ -29,18 +29,18 @@ def test_cluster_client_does_support_transaction_by_hashtag
29
29
30
30
def test_cluster_client_does_not_support_transaction_by_multiple_keys
31
31
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 )
37
37
end
38
38
end
39
39
40
40
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 )
44
44
end
45
45
end
46
46
@@ -63,10 +63,50 @@ def test_cluster_client_does_support_transaction_with_optimistic_locking
63
63
another . resume
64
64
v1 = redis . get ( '{key}1' )
65
65
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 )
68
68
end
69
69
70
70
assert_equal %w[ 3 4 ] , redis . mget ( '{key}1' , '{key}2' )
71
71
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
72
112
end
0 commit comments