Skip to content

Commit d24da3e

Browse files
committed
No need to unsubscribe from ensure block
1 parent 15ae67c commit d24da3e

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed

lib/redis/subscribe.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ def subscription(start, stop, channels, block)
3939
break if unsubscribed
4040
end
4141
ensure
42-
send(stop) if !unsubscribed
42+
# No need to unsubscribe here. The real client closes the connection
43+
# whenever an exception is raised (see #ensure_connected).
4344
end
4445
end
4546
end

test/publish_subscribe_test.rb

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ class TestPublishSubscribe < Test::Unit::TestCase
66

77
include Helper::Client
88

9+
class TestError < StandardError
10+
end
11+
912
def test_subscribe_and_unsubscribe
1013
@subscribed = false
1114
@unsubscribed = false
@@ -84,6 +87,62 @@ def test_psubscribe_and_punsubscribe
8487
assert_equal "s1", @message
8588
end
8689

90+
def test_subscribe_connection_usable_after_raise
91+
@subscribed = false
92+
93+
wire = Wire.new do
94+
begin
95+
r.subscribe("foo") do |on|
96+
on.subscribe do |channel, total|
97+
@subscribed = true
98+
end
99+
100+
on.message do |channel, message|
101+
raise TestError
102+
end
103+
end
104+
rescue TestError
105+
end
106+
end
107+
108+
# Wait until the subscription is active before publishing
109+
Wire.pass while !@subscribed
110+
111+
Redis.new(OPTIONS).publish("foo", "s1")
112+
113+
wire.join
114+
115+
assert_equal "PONG", r.ping
116+
end
117+
118+
def test_psubscribe_connection_usable_after_raise
119+
@subscribed = false
120+
121+
wire = Wire.new do
122+
begin
123+
r.psubscribe("f*") do |on|
124+
on.psubscribe do |pattern, total|
125+
@subscribed = true
126+
end
127+
128+
on.pmessage do |pattern, channel, message|
129+
raise TestError
130+
end
131+
end
132+
rescue TestError
133+
end
134+
end
135+
136+
# Wait until the subscription is active before publishing
137+
Wire.pass while !@subscribed
138+
139+
Redis.new(OPTIONS).publish("foo", "s1")
140+
141+
wire.join
142+
143+
assert_equal "PONG", r.ping
144+
end
145+
87146
def test_subscribe_within_subscribe
88147
@channels = []
89148

0 commit comments

Comments
 (0)