spec/adhearsion/call_spec.rb in adhearsion-2.0.0.beta1 vs spec/adhearsion/call_spec.rb in adhearsion-2.0.0.rc1

- old
+ new

@@ -1,10 +1,12 @@ +# encoding: utf-8 + require 'spec_helper' module Adhearsion describe Call do - let(:mock_client) { flexmock('Client').tap &:should_ignore_missing } + let(:mock_client) { flexmock('Client').tap(&:should_ignore_missing) } let(:call_id) { rand } let(:headers) { nil } let(:to) { 'sip:you@there.com' } let(:from) { 'sip:me@here.com' } @@ -25,65 +27,64 @@ Adhearsion.active_calls.clear! end it { should respond_to :<< } - its(:end_reason) { should == nil } + its(:end_reason) { should be == nil } it { should be_active } - its(:commands) { should be_a Call::CommandRegistry } its(:commands) { should be_empty } - its(:id) { should == call_id } - its(:to) { should == to } - its(:from) { should == from } + its(:id) { should be == call_id } + its(:to) { should be == to } + its(:from) { should be == from } its(:offer) { should be offer } its(:client) { should be mock_client } - its(:after_end_hold_time) { should == 30 } + its(:after_end_hold_time) { should be == 30 } describe "its variables" do context "with an offer with headers" do let(:headers) { {:x_foo => 'bar'} } - its(:variables) { should == headers } + its(:variables) { should be == headers } it "should be made available via []" do - subject[:x_foo].should == 'bar' + subject[:x_foo].should be == 'bar' end it "should be alterable using []=" do subject[:x_foo] = 'baz' - subject[:x_foo].should == 'baz' + subject[:x_foo].should be == 'baz' end context "when receiving an event with headers" do let(:event) { Punchblock::Event::End.new :headers => {:x_bar => 'foo'} } it "should merge later headers" do subject << event - subject.variables.should == {:x_foo => 'bar', :x_bar => 'foo'} + subject.variables.should be == {:x_foo => 'bar', :x_bar => 'foo'} end end context "when sending a command with headers" do let(:command) { Punchblock::Command::Accept.new :headers => {:x_bar => 'foo'} } it "should merge later headers" do subject.write_command command - subject.variables.should == {:x_foo => 'bar', :x_bar => 'foo'} + subject.variables.should be == {:x_foo => 'bar', :x_bar => 'foo'} end end end context "with an offer without headers" do let(:headers) { nil } - its(:variables) { should == {} } + its(:variables) { should be == {} } end context "without an offer" do let(:offer) { nil } - its(:variables) { should == {} } + its(:variables) { should be == {} } end end describe 'without an offer' do it 'should not raise an exception' do @@ -131,11 +132,11 @@ subject.should_not be_active end it "should set the end reason" do subject << end_event - subject.end_reason.should == :hangup + subject.end_reason.should be == :hangup end it "should instruct the command registry to terminate" do flexmock(subject.commands).should_receive(:terminate).once subject << end_event @@ -143,14 +144,14 @@ it "removes itself from the active calls" do size_before = Adhearsion.active_calls.size Adhearsion.active_calls << subject - Adhearsion.active_calls.size.should > size_before + Adhearsion.active_calls.size.should be > size_before subject << end_event - Adhearsion.active_calls.size.should == size_before + Adhearsion.active_calls.size.should be == size_before end it "shuts down the actor" do flexmock subject.wrapped_object, :after_end_hold_time => 2 subject << end_event @@ -188,11 +189,11 @@ it "#remove_tag" do subject.tag :moderator subject.tag :female subject.remove_tag :female subject.tag :male - subject.tags.should == [:moderator, :male] + subject.tags.should be == [:moderator, :male] end describe "#tagged_with?" do it 'with one tag' do subject.tag :guest @@ -212,21 +213,21 @@ let(:mock_command) { flexmock('Command') } it "should asynchronously write the command to the Punchblock connection" do mock_client = flexmock('Client') flexmock(subject.wrapped_object).should_receive(:client).once.and_return mock_client - mock_client.should_receive(:execute_command).once.with(mock_command, :call_id => subject.id).and_return true + mock_client.should_receive(:execute_command).once.with(mock_command, :call_id => subject.id, :async => true).and_return true subject.write_command mock_command end describe "with a hungup call" do before do flexmock(subject.wrapped_object).should_receive(:active?).and_return(false) end it "should raise a Hangup exception" do - lambda { subject.write_command mock_command }.should raise_error(Hangup) + lambda { subject.write_command mock_command }.should raise_error(Call::Hangup) end describe "if the command is a Hangup" do let(:mock_command) { Punchblock::Command::Hangup.new } @@ -256,10 +257,11 @@ subject.commands.should_not be_empty end it "blocks until a response is received" do slow_command = Punchblock::Command::Dial.new + slow_command.request! Thread.new do sleep 0.5 slow_command.response = response end starting_time = Time.now @@ -272,26 +274,35 @@ subject.write_and_await_response(message).should be message end end describe "with an error response" do - let(:new_exception) { Class.new Exception } + let(:new_exception) { Punchblock::ProtocolError } let(:response) { new_exception.new } it "raises the error" do flexmock(Events).should_receive(:trigger).never lambda { subject.write_and_await_response message }.should raise_error new_exception end + + context "where the name is :item_not_found" do + let(:response) { new_exception.new :item_not_found } + + it "should raise a Hangup exception" do + flexmock(Events).should_receive(:trigger).never + lambda { subject.write_and_await_response message }.should raise_error Call::Hangup + end + end end describe "when the response times out" do before do message.should_receive(:response).and_raise Timeout::Error end it "should raise the error in the caller but not crash the actor" do - lambda { subject.write_and_await_response message }.should raise_error Timeout::Error + lambda { subject.write_and_await_response message }.should raise_error Call::CommandTimeout, message.to_s sleep 0.5 subject.should be_alive end end end @@ -495,10 +506,67 @@ lambda { subject.join target }.should raise_error ArgumentError, /call ID and mixer name/ end end end + describe "#unjoin" do + def expect_unjoin_with_options(options = {}) + Punchblock::Command::Unjoin.new(options).tap do |unjoin| + expect_message_waiting_for_response unjoin + end + end + + context "with a call" do + let(:call_id) { rand.to_s } + let(:target) { flexmock Call.new, :id => call_id } + + it "should send an unjoin command unjoining from the provided call ID" do + expect_unjoin_with_options :other_call_id => call_id + subject.unjoin target + end + end + + context "with a call ID" do + let(:target) { rand.to_s } + + it "should send an unjoin command unjoining from the provided call ID" do + expect_unjoin_with_options :other_call_id => target + subject.unjoin target + end + end + + context "with a call ID as a hash key" do + let(:call_id) { rand.to_s } + let(:target) { { :call_id => call_id } } + + it "should send an unjoin command unjoining from the provided call ID" do + expect_unjoin_with_options :other_call_id => call_id + subject.unjoin target + end + end + + context "with a mixer name as a hash key" do + let(:mixer_name) { rand.to_s } + let(:target) { { :mixer_name => mixer_name } } + + it "should send an unjoin command unjoining from the provided call ID" do + expect_unjoin_with_options :mixer_name => mixer_name + subject.unjoin target + end + end + + context "with a call ID and a mixer name as hash keys" do + let(:call_id) { rand.to_s } + let(:mixer_name) { rand.to_s } + let(:target) { { :call_id => call_id, :mixer_name => mixer_name } } + + it "should raise an ArgumentError" do + lambda { subject.unjoin target }.should raise_error ArgumentError, /call ID and mixer name/ + end + end + end + describe "#mute" do it 'should send a Mute message' do expect_message_waiting_for_response Punchblock::Command::Mute.new subject.mute end @@ -610,12 +678,12 @@ command.request! subject << command end subject.terminate commands.each do |command| - command.response.should be_a Hangup + command.response.should be_a Call::Hangup end - finished_command.response.should == :foo + finished_command.response.should be == :foo end end end end end