Skip to content

Commit

Permalink
Call.execute_agi_command porting to the new AsyncAGI events. Failing …
Browse files Browse the repository at this point in the history
…test.
  • Loading branch information
lpradovera committed May 28, 2015
1 parent 2b99249 commit 876a764
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 18 deletions.
1 change: 0 additions & 1 deletion lib/punchblock/translator/asterisk.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ def handle_ami_event(event)
handle_varset_ami_event event

ami_dispatch_to_or_create_call event

if !ami_event_known_call?(event) && self.class.event_passes_filter?(event)
handle_pb_event Event::Asterisk::AMI::Event.new(name: event.name, headers: event.headers)
end
Expand Down
9 changes: 9 additions & 0 deletions lib/punchblock/translator/asterisk/call.rb
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,15 @@ def process_ami_event(ami_event)
@answered = true
send_pb_event Event::Answered.new(timestamp: ami_event.best_time)
end
when 'AsyncAGIStart'
if @answered == false
@answered = true
send_pb_event Event::Answered.new(timestamp: ami_event.best_time)
end
when 'AsyncAGIExec'
if component = component_with_id(ami_event['CommandID'])
component.handle_ami_event ami_event
end
when 'Newstate'
case ami_event['ChannelState']
when '5'
Expand Down
102 changes: 85 additions & 17 deletions spec/punchblock/translator/asterisk/call_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,34 @@ class Asterisk
end
end

def should_send_answered_event
expected_answered = Punchblock::Event::Answered.new
expected_answered.target_call_id = subject.id
expect(translator).to receive(:handle_pb_event).with expected_answered
subject.process_ami_event ami_event
end

def answered_should_be_true
subject.process_ami_event ami_event
expect(subject.answered?).to be_true
end

def should_only_send_one_answered_event
expected_answered = Punchblock::Event::Answered.new
expected_answered.target_call_id = subject.id
expect(translator).to receive(:handle_pb_event).with(expected_answered).once
subject.process_ami_event ami_event
subject.process_ami_event ami_event
end

def should_use_ami_timestamp_for_rayo_event
expected_answered = Punchblock::Event::Answered.new target_call_id: subject.id,
timestamp: DateTime.new(2014, 2, 25, 22, 46, 20)
expect(translator).to receive(:handle_pb_event).with expected_answered

subject.process_ami_event ami_event
end

context 'with an AsyncAGI Start event' do
let(:ami_event) do
RubyAMI::Event.new "AsyncAGI",
Expand All @@ -550,24 +578,16 @@ class Asterisk
end

it 'should send an answered event' do
expected_answered = Punchblock::Event::Answered.new
expected_answered.target_call_id = subject.id
expect(translator).to receive(:handle_pb_event).with expected_answered
subject.process_ami_event ami_event
should_send_answered_event
end

it '#answered? should be true' do
subject.process_ami_event ami_event
expect(subject.answered?).to be_true
answered_should_be_true
end

context "for a second time" do
it 'should only send one answered event' do
expected_answered = Punchblock::Event::Answered.new
expected_answered.target_call_id = subject.id
expect(translator).to receive(:handle_pb_event).with(expected_answered).once
subject.process_ami_event ami_event
subject.process_ami_event ami_event
should_only_send_one_answered_event
end
end

Expand All @@ -581,11 +601,42 @@ class Asterisk
end

it "should use the AMI timestamp for the Rayo event" do
expected_answered = Punchblock::Event::Answered.new target_call_id: subject.id,
timestamp: DateTime.new(2014, 2, 25, 22, 46, 20)
expect(translator).to receive(:handle_pb_event).with expected_answered
should_use_ami_timestamp_for_rayo_event
end
end
end

subject.process_ami_event ami_event
context 'with an Asterisk 13 AsyncAGIStart event' do
let(:ami_event) do
RubyAMI::Event.new "AsyncAGIStart",
"Channel" => "SIP/1234-00000000",
"Env" => "agi_request%3A%20async%0Aagi_channel%3A%20SIP%2Fuserb-00000006%0Aagi_language%3A%20en%0Aagi_type%3A%20SIP%0Aagi_uniqueid%3A%201390303636.6%0Aagi_version%3A%2011.7.0%0Aagi_callerid%3A%20userb%0Aagi_calleridname%3A%20User%20B%0Aagi_callingpres%3A%200%0Aagi_callingani2%3A%200%0Aagi_callington%3A%200%0Aagi_callingtns%3A%200%0Aagi_dnid%3A%20unknown%0Aagi_rdnis%3A%20unknown%0Aagi_context%3A%20adhearsion-redirect%0Aagi_extension%3A%201%0Aagi_priority%3A%201%0Aagi_enhanced%3A%200.0%0Aagi_accountcode%3A%20%0Aagi_threadid%3A%20139696536876800%0A%0A"
end

it 'should send an answered event' do
should_send_answered_event
end

it '#answered? should be true' do
answered_should_be_true
end

context "for a second time" do
it 'should only send one answered event' do
should_only_send_one_answered_event
end
end

context "when the AMI event has a timestamp" do
let :ami_event do
RubyAMI::Event.new "AsyncAGIStart",
"Channel" => "SIP/1234-00000000",
"Env" => "agi_request%3A%20async%0Aagi_channel%3A%20SIP%2Fuserb-00000006%0Aagi_language%3A%20en%0Aagi_type%3A%20SIP%0Aagi_uniqueid%3A%201390303636.6%0Aagi_version%3A%2011.7.0%0Aagi_callerid%3A%20userb%0Aagi_calleridname%3A%20User%20B%0Aagi_callingpres%3A%200%0Aagi_callingani2%3A%200%0Aagi_callington%3A%200%0Aagi_callingtns%3A%200%0Aagi_dnid%3A%20unknown%0Aagi_rdnis%3A%20unknown%0Aagi_context%3A%20adhearsion-redirect%0Aagi_extension%3A%201%0Aagi_priority%3A%201%0Aagi_enhanced%3A%200.0%0Aagi_accountcode%3A%20%0Aagi_threadid%3A%20139696536876800%0A%0A",
'Timestamp' => '1393368380.572575'
end

it "should use the AMI timestamp for the Rayo event" do
should_use_ami_timestamp_for_rayo_event
end
end
end
Expand Down Expand Up @@ -1767,6 +1818,7 @@ def grxml_doc(mode = :dtmf)
end
end


describe 'when receiving an AsyncAGI event' do
context 'of type Exec' do
let(:ami_event) do
Expand All @@ -1780,11 +1832,27 @@ def grxml_doc(mode = :dtmf)

it 'should return the result' do
fut = Celluloid::Future.new { subject.execute_agi_command 'EXEC ANSWER' }

sleep 0.25

subject.process_ami_event ami_event
expect(fut.value).to eq({code: 200, result: 123, data: 'timeout'})
end
end
end

describe 'when receiving an Asterisk 13 AsyncAGIExec event' do
context 'without a subtype' do
let(:ami_event) do
RubyAMI::Event.new 'AsyncAGIExec',
"Channel" => channel,
"CommandID" => Punchblock.new_uuid,
"Command" => "EXEC ANSWER",
"Result" => "200%20result=123%20(timeout)%0A"
end

it 'should return the result' do
fut = Celluloid::Future.new { subject.execute_agi_command 'EXEC ANSWER' }
sleep 0.25
subject.process_ami_event ami_event
expect(fut.value).to eq({code: 200, result: 123, data: 'timeout'})
end
end
Expand Down

0 comments on commit 876a764

Please sign in to comment.