5
5
class TestPublishSubscribe < Minitest ::Test
6
6
include Helper ::Client
7
7
8
+ def setup
9
+ @threads = { }
10
+ super
11
+ end
12
+
13
+ def teardown
14
+ super
15
+ @threads . each do |thread , redis |
16
+ if redis . subscribed?
17
+ redis . unsubscribe
18
+ redis . punsubscribe
19
+ end
20
+ redis . close
21
+ begin
22
+ thread . join ( 2 ) or warn ( "leaked thread" )
23
+ rescue RedisClient ::ConnectionError
24
+ end
25
+ end
26
+ end
27
+
8
28
class TestError < StandardError
9
29
end
10
30
11
31
def test_subscribe_and_unsubscribe
12
32
@subscribed = false
13
33
@unsubscribed = false
14
34
15
- thread = Thread . new do
16
- r . subscribe ( "foo" ) do |on |
35
+ thread = new_thread do | r |
36
+ r . subscribe ( channel_name ) do |on |
17
37
on . subscribe do |_channel , total |
18
38
@subscribed = true
19
39
@t1 = total
@@ -36,8 +56,7 @@ def test_subscribe_and_unsubscribe
36
56
# Wait until the subscription is active before publishing
37
57
Thread . pass until @subscribed
38
58
39
- Redis . new ( OPTIONS ) . publish ( "foo" , "s1" )
40
-
59
+ redis . publish ( channel_name , "s1" )
41
60
thread . join
42
61
43
62
assert @subscribed
@@ -51,8 +70,8 @@ def test_psubscribe_and_punsubscribe
51
70
@subscribed = false
52
71
@unsubscribed = false
53
72
54
- thread = Thread . new do
55
- r . psubscribe ( "f *" ) do |on |
73
+ thread = new_thread do | r |
74
+ r . psubscribe ( "channel: *" ) do |on |
56
75
on . psubscribe do |_pattern , total |
57
76
@subscribed = true
58
77
@t1 = total
@@ -74,9 +93,7 @@ def test_psubscribe_and_punsubscribe
74
93
75
94
# Wait until the subscription is active before publishing
76
95
Thread . pass until @subscribed
77
-
78
- Redis . new ( OPTIONS ) . publish ( "foo" , "s1" )
79
-
96
+ redis . publish ( channel_name , "s1" )
80
97
thread . join
81
98
82
99
assert @subscribed
@@ -86,56 +103,37 @@ def test_psubscribe_and_punsubscribe
86
103
assert_equal "s1" , @message
87
104
end
88
105
89
- def test_pubsub_with_numpat_subcommand
90
- @subscribed = false
91
- thread = Thread . new do
92
- r . psubscribe ( "f*" ) do |on |
93
- on . psubscribe { |_channel , _total | @subscribed = true }
94
- on . pmessage { |_pattern , _channel , _message | r . punsubscribe }
95
- end
96
- end
97
- Thread . pass until @subscribed
98
- redis = Redis . new ( OPTIONS )
99
- numpat_result = redis . pubsub ( :numpat )
100
-
101
- redis . publish ( "foo" , "s1" )
102
- thread . join
103
-
104
- assert_equal redis . pubsub ( :numpat ) , 0
105
- assert_equal numpat_result , 1
106
- end
107
-
108
106
def test_pubsub_with_channels_and_numsub_subcommnads
109
107
@subscribed = false
110
- thread = Thread . new do
111
- r . subscribe ( "foo" ) do |on |
108
+ thread = new_thread do | r |
109
+ r . subscribe ( channel_name ) do |on |
112
110
on . subscribe { |_channel , _total | @subscribed = true }
113
111
on . message { |_channel , _message | r . unsubscribe }
114
112
end
115
113
end
116
114
Thread . pass until @subscribed
117
- redis = Redis . new ( OPTIONS )
118
115
channels_result = redis . pubsub ( :channels )
119
116
channels_result . delete ( '__sentinel__:hello' )
120
- numsub_result = redis . pubsub ( :numsub , 'foo' , 'boo' )
117
+ numsub_result = redis . pubsub ( :numsub , channel_name , 'boo' )
121
118
122
- redis . publish ( "foo" , "s1" )
119
+ redis . publish ( channel_name , "s1" )
123
120
thread . join
124
121
125
- assert_equal channels_result , [ 'foo' ]
126
- assert_equal numsub_result , [ 'foo' , 1 , 'boo' , 0 ]
122
+ assert_includes channels_result , channel_name
123
+ assert_equal [ channel_name , 1 , 'boo' , 0 ] , numsub_result
127
124
end
128
125
129
126
def test_subscribe_connection_usable_after_raise
130
127
@subscribed = false
131
128
132
- thread = Thread . new do
133
- r . subscribe ( "foo" ) do |on |
129
+ thread = new_thread do | r |
130
+ r . subscribe ( channel_name ) do |on |
134
131
on . subscribe do |_channel , _total |
135
132
@subscribed = true
136
133
end
137
134
138
135
on . message do |_channel , _message |
136
+ r . unsubscribe
139
137
raise TestError
140
138
end
141
139
end
@@ -145,7 +143,7 @@ def test_subscribe_connection_usable_after_raise
145
143
# Wait until the subscription is active before publishing
146
144
Thread . pass until @subscribed
147
145
148
- Redis . new ( OPTIONS ) . publish ( "foo" , "s1" )
146
+ redis . publish ( channel_name , "s1" )
149
147
150
148
thread . join
151
149
@@ -155,8 +153,8 @@ def test_subscribe_connection_usable_after_raise
155
153
def test_psubscribe_connection_usable_after_raise
156
154
@subscribed = false
157
155
158
- thread = Thread . new do
159
- r . psubscribe ( "f *" ) do |on |
156
+ thread = new_thread do | r |
157
+ r . psubscribe ( "channel: *" ) do |on |
160
158
on . psubscribe do |_pattern , _total |
161
159
@subscribed = true
162
160
end
@@ -171,7 +169,7 @@ def test_psubscribe_connection_usable_after_raise
171
169
# Wait until the subscription is active before publishing
172
170
Thread . pass until @subscribed
173
171
174
- Redis . new ( OPTIONS ) . publish ( "foo" , "s1" )
172
+ redis . publish ( channel_name , "s1" )
175
173
176
174
thread . join
177
175
@@ -181,34 +179,34 @@ def test_psubscribe_connection_usable_after_raise
181
179
def test_subscribe_within_subscribe
182
180
@channels = [ ]
183
181
184
- thread = Thread . new do
185
- r . subscribe ( "foo" ) do |on |
182
+ thread = new_thread do | r |
183
+ r . subscribe ( channel_name ) do |on |
186
184
on . subscribe do |channel , _total |
187
185
@channels << channel
188
186
189
- r . subscribe ( "bar" ) if channel == "foo"
187
+ r . subscribe ( "bar" ) if channel == channel_name
190
188
r . unsubscribe if channel == "bar"
191
189
end
192
190
end
193
191
end
194
192
195
193
thread . join
196
194
197
- assert_equal [ "foo" , "bar" ] , @channels
195
+ assert_equal [ channel_name , "bar" ] , @channels
198
196
end
199
197
200
198
def test_other_commands_within_a_subscribe
201
- r . subscribe ( "foo" ) do |on |
199
+ r . subscribe ( channel_name ) do |on |
202
200
on . subscribe do |_channel , _total |
203
201
r . set ( "bar" , "s2" )
204
- r . unsubscribe ( "foo" )
202
+ r . unsubscribe ( channel_name )
205
203
end
206
204
end
207
205
end
208
206
209
207
def test_subscribe_without_a_block
210
208
assert_raises LocalJumpError do
211
- r . subscribe ( "foo" )
209
+ r . subscribe ( channel_name )
212
210
end
213
211
end
214
212
@@ -245,7 +243,7 @@ def test_subscribe_past_a_timeout
245
243
def test_subscribe_with_timeout
246
244
received = false
247
245
248
- r . subscribe_with_timeout ( LOW_TIMEOUT , "foo" ) do |on |
246
+ r . subscribe_with_timeout ( LOW_TIMEOUT , channel_name ) do |on |
249
247
on . message do |_channel , _message |
250
248
received = true
251
249
end
@@ -257,12 +255,26 @@ def test_subscribe_with_timeout
257
255
def test_psubscribe_with_timeout
258
256
received = false
259
257
260
- r . psubscribe_with_timeout ( LOW_TIMEOUT , "f *" ) do |on |
258
+ r . psubscribe_with_timeout ( LOW_TIMEOUT , "channel: *" ) do |on |
261
259
on . message do |_channel , _message |
262
260
received = true
263
261
end
264
262
end
265
263
266
264
refute received
267
265
end
266
+
267
+ private
268
+
269
+ def new_thread ( &block )
270
+ redis = Redis . new ( OPTIONS )
271
+ thread = Thread . new ( redis , &block )
272
+ thread . report_on_exception = false
273
+ @threads [ thread ] = redis
274
+ thread
275
+ end
276
+
277
+ def channel_name
278
+ @channel_name ||= "channel:#{ rand } "
279
+ end
268
280
end
0 commit comments