@@ -35,4 +35,82 @@ def test_sentinel_connection
35
35
assert_equal commands [ :s1 ] , [ %w[ get-master-addr-by-name master1 ] ]
36
36
assert_equal commands [ :s2 ] , [ ]
37
37
end
38
+
39
+ def test_sentinel_failover
40
+ sentinels = [ { :host => "127.0.0.1" , :port => 26381 } ,
41
+ { :host => "127.0.0.1" , :port => 26382 } ]
42
+
43
+ commands = {
44
+ :s1 => [ ] ,
45
+ :s2 => [ ] ,
46
+ }
47
+
48
+ s1 = {
49
+ :sentinel => lambda do |command , *args |
50
+ commands [ :s1 ] << [ command , *args ]
51
+ "$-1" # Nil
52
+ end
53
+ }
54
+
55
+ s2 = {
56
+ :sentinel => lambda do |command , *args |
57
+ commands [ :s2 ] << [ command , *args ]
58
+ [ "127.0.0.1" , "6381" ]
59
+ end
60
+ }
61
+
62
+ Thread . abort_on_exception = true
63
+
64
+ RedisMock . start ( s1 , { } , 26381 ) do
65
+ RedisMock . start ( s2 , { } , 26382 ) do
66
+ redis = Redis . new ( :url => "redis://master1" , :sentinels => sentinels , :role => :master )
67
+
68
+ assert redis . ping
69
+ end
70
+ end
71
+
72
+ assert_equal commands [ :s1 ] , [ %w[ get-master-addr-by-name master1 ] ]
73
+ assert_equal commands [ :s2 ] , [ %w[ get-master-addr-by-name master1 ] ]
74
+ end
75
+
76
+ def test_sentinel_failover_prioritize_healthy_sentinel
77
+ sentinels = [ { :host => "127.0.0.1" , :port => 26381 } ,
78
+ { :host => "127.0.0.1" , :port => 26382 } ]
79
+
80
+ commands = {
81
+ :s1 => [ ] ,
82
+ :s2 => [ ] ,
83
+ }
84
+
85
+ s1 = {
86
+ :sentinel => lambda do |command , *args |
87
+ commands [ :s1 ] << [ command , *args ]
88
+ "$-1" # Nil
89
+ end
90
+ }
91
+
92
+ s2 = {
93
+ :sentinel => lambda do |command , *args |
94
+ commands [ :s2 ] << [ command , *args ]
95
+ [ "127.0.0.1" , "6381" ]
96
+ end
97
+ }
98
+
99
+ Thread . abort_on_exception = true
100
+
101
+ RedisMock . start ( s1 , { } , 26381 ) do
102
+ RedisMock . start ( s2 , { } , 26382 ) do
103
+ redis = Redis . new ( :url => "redis://master1" , :sentinels => sentinels , :role => :master )
104
+
105
+ assert redis . ping
106
+
107
+ redis . quit
108
+
109
+ assert redis . ping
110
+ end
111
+ end
112
+
113
+ assert_equal commands [ :s1 ] , [ %w[ get-master-addr-by-name master1 ] ]
114
+ assert_equal commands [ :s2 ] , [ %w[ get-master-addr-by-name master1 ] , %w[ get-master-addr-by-name master1 ] ]
115
+ end
38
116
end
0 commit comments