Skip to content

Commit fd7b3c0

Browse files
committed
Use explicit method to clear event handlers.
1 parent b73ec51 commit fd7b3c0

File tree

4 files changed

+31
-8
lines changed

4 files changed

+31
-8
lines changed

lib/rabbitmq/channel.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@ def on_event(*args, &block)
6767
end
6868
alias_method :on, :on_event
6969

70+
# @see {Client#clear_event_handler}
71+
def clear_event_handler(*args)
72+
@client.clear_event_handler(@id, *args)
73+
end
74+
7075
# @see {Client#run_loop!}
7176
# The block will be yielded all non-exception events *for any channel*.
7277
def run_loop!(*args, &block)

lib/rabbitmq/client.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,12 +142,22 @@ def fetch_response(channel_id, method, timeout: protocol_timeout)
142142
def on_event(channel_id, method, callable=nil, &block)
143143
handler = block || callable
144144
raise ArgumentError, "expected block or callable as the event handler" \
145-
unless handler.nil? or handler.respond_to?(:call)
145+
unless handler.respond_to?(:call)
146146

147147
@event_handlers[Integer(channel_id)][method.to_sym] = handler
148148
handler
149149
end
150150

151+
# Unregister the event handler associated with the given channel and method.
152+
#
153+
# @param channel_id [Integer] The channel number to watch for.
154+
# @param method [Symbol] The type of protocol method to watch for.
155+
# @return [Proc,nil] This removed handler, if any.
156+
#
157+
def clear_event_handler(channel_id, method)
158+
@event_handlers[Integer(channel_id)].delete(method.to_sym)
159+
end
160+
151161
# Fetch and handle events in a loop that blocks the calling thread.
152162
# The loop will continue until the {#break!} method is called from within
153163
# an event handler, or until the given timeout duration has elapsed.

spec/channel_spec.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@
6969
subject.on(*a, &b).should eq res
7070
end
7171

72+
specify "#clear_event_handler" do
73+
client.should_receive(:clear_event_handler).with(id, *a) { res }
74+
subject.clear_event_handler(*a).should eq res
75+
end
76+
7277
specify "#send_request" do
7378
client.should_receive(:send_request).with(id, *a) { res }
7479
subject.send_request(*a).should eq res

spec/client_spec.rb

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -119,14 +119,10 @@
119119
let(:other_bucket) { [] }
120120

121121
it "requires a block or callable object to be given as the handler" do
122-
expect { subject.on_event(11, :channel_open_ok, :neither) }.to \
122+
expect { subject.on_event(11, :channel_open_ok) }.to \
123123
raise_error ArgumentError, /block or callable/
124124
end
125125

126-
it "allows nil to clear the handler" do
127-
subject.on_event(11, :channel_open_ok, nil)
128-
end
129-
130126
shared_examples "handling events" do
131127
it "calls the handler for uncaught events" do
132128
subject.send_request(11, :channel_open)
@@ -172,15 +168,22 @@
172168
other_bucket.should be_empty
173169
end
174170

175-
it "clears the handler when nil is registered" do
176-
subject.on_event(11, :channel_open_ok, nil)
171+
it "clears the handler when explicitly unregistered" do
172+
subject.clear_event_handler(11, :channel_open_ok).should respond_to :call
177173

178174
subject.send_request(11, :channel_open)
179175
subject.fetch_response(11, :channel_open_ok)
180176

181177
bucket.should be_empty
182178
end
183179

180+
it "clears the handler and returns nil when already cleared" do
181+
subject.clear_event_handler(11, :channel_close_ok).should eq nil
182+
183+
subject.clear_event_handler(11, :channel_open_ok).should respond_to :call
184+
subject.clear_event_handler(11, :channel_open_ok).should eq nil
185+
end
186+
184187
it "calls the block passed to run_loop! for handler-matching events" do
185188
subject.send_request(11, :channel_open)
186189
subject.run_loop! do |event|

0 commit comments

Comments
 (0)