Commit 14d6cda4 authored by James Adam & James Mead's avatar James Adam & James Mead
Browse files

It should be easier to invoke timeouts in tests for Openvoice.

Instead of calling continue on a handler instance directly (which is hard to reach in tests), we now have the handler watch for "Timeout" pseudo-events, which are far easier to send to the handler (e.g. via `Connfu.event_processor.handle_event`).
parent 59247bf0
......@@ -177,11 +177,23 @@ module Connfu
@observed_call_ids ||= []
end
class Timeout
attr_reader :call_id
def initialize(call_id)
@call_id = call_id
end
end
def wait_for(*events)
options = events.last.is_a?(Hash) ? events.pop : {}
timeout = options[:timeout]
@waiting_for = events
timer = EM.add_timer(timeout) { continue } if timeout
if timeout
@waiting_for << Connfu::Dsl::Timeout
timer = EM.add_timer(timeout) do
Connfu.event_processor.handle_event(Connfu::Dsl::Timeout.new(call_id))
end
end
result = wait
EM.cancel_timer(timer) if result && timer
result
......
......@@ -6,7 +6,7 @@ describe Connfu::Dsl do
end
before do
Connfu.connection = TestConnection.new
setup_connfu(nil)
subject.stub(:wait)
end
......@@ -310,12 +310,21 @@ describe Connfu::Dsl do
subject.send(:wait_for, Connfu::Event::Answered, :timeout => 5)
end
it 'should continue execution of the script after the timeout' do
it 'should schedule a timeout event to be given to the handler in the future' do
EM.stub!(:add_timer).and_yield()
subject.should_receive(:continue)
Connfu.event_processor.should_receive(:handle_event).with do |timeout_event|
timeout_event.is_a?(Connfu::Dsl::Timeout) &&
timeout_event.call_id == subject.call_id
end
subject.send(:wait_for, Connfu::Event::Answered, :timeout => 10)
end
it 'should tell the handler to expect a timeout event' do
EM.stub!(:add_timer)
subject.send(:wait_for, Connfu::Event::Answered, :timeout => 10)
subject.should be_waiting_for(Connfu::Dsl::Timeout.new(subject.call_id))
end
it 'should cancel the timer when the pending event is received' do
EM.stub!(:add_timer).and_return(:timer_signature)
subject.stub!(:wait).and_return(Connfu::Event::Answered.new)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment