spec/punchblock/translator/asterisk_spec.rb in punchblock-2.1.1 vs spec/punchblock/translator/asterisk_spec.rb in punchblock-2.2.0

- old
+ new

@@ -16,25 +16,10 @@ its(:ami_client) { should be ami_client } its(:connection) { should be connection } after { translator.terminate if translator.alive? } - describe '#shutdown' do - it "instructs all calls to shutdown" do - call = Asterisk::Call.new 'foo', subject, ami_client, connection - call.async.should_receive(:shutdown).once - subject.register_call call - subject.shutdown - end - - it "terminates the actor" do - subject.shutdown - sleep 0.2 - subject.should_not be_alive - end - end - describe '#execute_command' do describe 'with a call command' do let(:command) { Command::Answer.new } let(:call_id) { 'abc123' } @@ -134,82 +119,35 @@ call.stub(:id).and_return call_id subject.register_call call end it 'sends the command to the call for execution' do - call.async.should_receive(:execute_command).once.with command + call.should_receive(:execute_command).once.with command subject.execute_call_command command end - end - let :end_error_event do - Punchblock::Event::End.new reason: :error, target_call_id: call_id - end + context 'when it raises' do + before do + call.should_receive(:execute_command).and_raise StandardError + end - context "for an outgoing call which began executing but crashed" do - let(:dial_command) { Command::Dial.new :to => 'SIP/1234', :from => 'abc123' } + let(:other_command) { Command::Answer.new target_call_id: call_id } - let(:call_id) { dial_command.response.call_id } + it 'sends an error in response to the command' do + subject.execute_call_command command - before do - subject.async.should_receive(:execute_global_command) - subject.execute_command dial_command - end + subject.call_with_id(call_id).should be_nil - it 'sends an error in response to the command' do - call = subject.call_with_id call_id - - call.wrapped_object.define_singleton_method(:oops) do - raise 'Woops, I died' + other_command.request! + subject.execute_call_command other_command + other_command.response.should be == ProtocolError.new.setup(:item_not_found, "Could not find a call with ID #{call_id}", call_id) end - - connection.should_receive(:handle_event).once.with end_error_event - - lambda { call.oops }.should raise_error(/Woops, I died/) - sleep 0.1 - call.should_not be_alive - subject.call_with_id(call_id).should be_nil - - command.request! - subject.execute_call_command command - command.response.should be == ProtocolError.new.setup(:item_not_found, "Could not find a call with ID #{call_id}", call_id) end end - context "for an incoming call which began executing but crashed" do - let :ami_event do - RubyAMI::Event.new 'AsyncAGI', - 'SubEvent' => "Start", - 'Channel' => "SIP/1234-00000000", - 'Env' => "agi_request%3A%20async%0Aagi_channel%3A%20SIP%2F1234-00000000%0Aagi_language%3A%20en%0Aagi_type%3A%20SIP%0Aagi_uniqueid%3A%201320835995.0%0Aagi_version%3A%201.8.4.1%0Aagi_callerid%3A%205678%0Aagi_calleridname%3A%20Jane%20Smith%0Aagi_callingpres%3A%200%0Aagi_callingani2%3A%200%0Aagi_callington%3A%200%0Aagi_callingtns%3A%200%0Aagi_dnid%3A%201000%0Aagi_rdnis%3A%20unknown%0Aagi_context%3A%20default%0Aagi_extension%3A%201000%0Aagi_priority%3A%201%0Aagi_enhanced%3A%200.0%0Aagi_accountcode%3A%20%0Aagi_threadid%3A%204366221312%0A%0A" - end - - let(:call) { subject.call_for_channel('SIP/1234-00000000') } - let(:call_id) { call.id } - - before do - connection.stub :handle_event - subject.handle_ami_event ami_event - call_id - end - - it 'sends an error in response to the command' do - call.wrapped_object.define_singleton_method(:oops) do - raise 'Woops, I died' - end - - connection.should_receive(:handle_event).once.with end_error_event - - lambda { call.oops }.should raise_error(/Woops, I died/) - sleep 0.1 - call.should_not be_alive - subject.call_with_id(call_id).should be_nil - - command.request! - subject.execute_call_command command - command.response.should be == ProtocolError.new.setup(:item_not_found, "Could not find a call with ID #{call_id}", call_id) - end + let :end_error_event do + Punchblock::Event::End.new reason: :error, target_call_id: call_id end context "with an unknown call ID" do it 'sends an error in response to the command' do command.request! @@ -234,11 +172,11 @@ before do subject.register_component component end it 'sends the command to the component for execution' do - component.async.should_receive(:execute_command).once.with command + component.should_receive(:execute_command).once.with command subject.execute_component_command command end end context "with an unknown component ID" do @@ -260,18 +198,18 @@ ami_client.stub(:send_action).and_return RubyAMI::Response.new end it 'should be able to look up the call by channel ID' do subject.execute_global_command command - call_actor = subject.call_for_channel('SIP/1234') - call_actor.wrapped_object.should be_a Asterisk::Call + call = subject.call_for_channel('SIP/1234') + call.should be_a Asterisk::Call end it 'should instruct the call to send a dial' do mock_call = double('Asterisk::Call').as_null_object - Asterisk::Call.should_receive(:new_link).once.and_return mock_call - mock_call.async.should_receive(:dial).once.with command + Asterisk::Call.should_receive(:new).once.and_return mock_call + mock_call.should_receive(:dial).once.with command subject.execute_global_command command end end context 'with an AMI action' do @@ -281,11 +219,11 @@ let(:mock_action) { double('Asterisk::Component::Asterisk::AMIAction').as_null_object } it 'should create a component actor and execute it asynchronously' do Asterisk::Component::Asterisk::AMIAction.should_receive(:new).once.with(command, subject, ami_client).and_return mock_action - mock_action.async.should_receive(:execute).once + mock_action.should_receive(:execute).once subject.execute_global_command command end it 'registers the component' do Asterisk::Component::Asterisk::AMIAction.should_receive(:new).once.with(command, subject, ami_client).and_return mock_action @@ -334,10 +272,20 @@ it 'should create a Punchblock AMI event object and pass it to the connection' do subject.connection.should_receive(:handle_event).once.with expected_pb_event subject.handle_ami_event ami_event end + context "when the event doesn't pass the filter" do + before { described_class.event_filter = ->(event) { false } } + after { described_class.event_filter = nil } + + it 'does not send the AMI event to the connection as a PB event' do + subject.connection.should_receive(:handle_event).never + subject.handle_ami_event ami_event + end + end + context 'with something that is not a RubyAMI::Event' do it 'does not send anything to the connection' do subject.connection.should_receive(:handle_event).never subject.handle_ami_event :foo end @@ -363,14 +311,14 @@ before { subject.wrapped_object.stub :handle_pb_event } it 'should be able to look up the call by channel ID' do subject.handle_ami_event ami_event - call_actor = subject.call_for_channel('SIP/1234-00000000') - call_actor.should be_a Asterisk::Call - call_actor.agi_env.should be_a Hash - call_actor.agi_env.should be == { + call = subject.call_for_channel('SIP/1234-00000000') + call.should be_a Asterisk::Call + call.agi_env.should be_a Hash + call.agi_env.should be == { :agi_request => 'async', :agi_channel => 'SIP/1234-00000000', :agi_language => 'en', :agi_type => 'SIP', :agi_uniqueid => '1320835995.0', @@ -392,12 +340,12 @@ } end it 'should instruct the call to send an offer' do mock_call = double('Asterisk::Call').as_null_object - Asterisk::Call.should_receive(:new_link).once.and_return mock_call - mock_call.async.should_receive(:send_offer).once + Asterisk::Call.should_receive(:new).once.and_return mock_call + mock_call.should_receive(:send_offer).once subject.handle_ami_event ami_event end context 'if a call already exists for a matching channel' do let(:call) { Asterisk::Call.new "SIP/1234-00000000", subject, ami_client, connection } @@ -405,11 +353,11 @@ before do subject.register_call call end it "should not create a new call" do - Asterisk::Call.should_receive(:new_link).never + Asterisk::Call.should_receive(:new).never subject.handle_ami_event ami_event end end context "for a 'h' extension" do @@ -521,11 +469,11 @@ before do subject.register_call call end it 'sends the AMI event to the call and to the connection as a PB event' do - call.async.should_receive(:process_ami_event).once.with ami_event + call.should_receive(:process_ami_event).once.with ami_event subject.handle_ami_event ami_event end context 'with a Channel1 and Channel2 specified on the event' do let :ami_event do @@ -542,12 +490,12 @@ end before { subject.register_call call2 } it 'should send the event to both calls and to the connection once as a PB event' do - call.async.should_receive(:process_ami_event).once.with ami_event - call2.async.should_receive(:process_ami_event).once.with ami_event + call.should_receive(:process_ami_event).once.with ami_event + call2.should_receive(:process_ami_event).once.with ami_event subject.handle_ami_event ami_event end end end end @@ -576,15 +524,15 @@ before do subject.register_call call end it 'sends the AMI event to the call and to the connection as a PB event if it is an allowed event' do - call.async.should_receive(:process_ami_event).once.with ami_event + call.should_receive(:process_ami_event).once.with ami_event subject.handle_ami_event ami_event end it 'does not send the AMI event to a bridged channel if it is not allowed' do - call.async.should_receive(:process_ami_event).never.with ami_event2 + call.should_receive(:process_ami_event).never.with ami_event2 subject.handle_ami_event ami_event2 end end end