Commit 5461b542 authored by James Adam & James Mead's avatar James Adam & James Mead
Browse files

This is an alternative approach for making the tests shorter.

Basically, now the connection knows how to respond to most commands; rather than tests having to explicitly state every incoming event, they only need to explicitly control the differences (i.e. 'pause after this event so I can check the command', or 'don't send this event').

Thoughts appreciated.
parent 50cf6459
......@@ -2,6 +2,8 @@ require "spec_helper"
describe "handling a call offer" do
testing_dsl do
def do_something_with(*args); end
on :offer do |offer|
do_something_with(
:from => offer.from,
......@@ -10,12 +12,14 @@ describe "handling a call offer" do
end
end
let(:actor) { Actor.new("James Adam <sip:james@127.0.0.1>") }
it "exposes who the call is from" do
dsl_instance.should_receive(:do_something_with).with(
hash_including(:from => "James Adam <sip:james@127.0.0.1>")
)
incoming :offer_presence, @call_jid, @client_jid, :from => "James Adam <sip:james@127.0.0.1>"
actor.call "<sip:usera@127.0.0.1>"
end
it "exposes who the call is being routed to" do
......@@ -30,7 +34,7 @@ describe "handling a call offer" do
hash_including(:to => parsed_hash)
)
incoming :offer_presence, @call_jid, @client_jid, :to => "<sip:usera@127.0.0.1>"
actor.call "<sip:usera@127.0.0.1>"
end
it "should deal with a call to a raw sip address" do
......@@ -45,15 +49,13 @@ describe "handling a call offer" do
hash_including(:to => parsed_hash)
)
incoming :offer_presence, @call_jid, @client_jid, :to => "sip:usera@127.0.0.1"
actor.call "sip:usera@127.0.0.1"
end
it "implicitly hangs up once handling is complete" do
handler_instance = Connfu.event_processor.handler_class.new({})
Connfu.event_processor.stub(:build_handler).and_return(handler_instance)
handler_instance.should_receive(:do_something_with).ordered
handler_instance.should_receive(:hangup).ordered
incoming :offer_presence, @call_jid, @client_jid
actor.call
last_command.should be_instance_of(Connfu::Commands::Hangup)
end
end
......@@ -65,18 +67,11 @@ describe "offer which is hungup by the DSL" do
end
end
before :each do
@call_jid = "call-id@server.whatever"
@client_jid = "usera@127.0.0.whatever/voxeo"
end
let(:actor) { Actor.new("James Adam <sip:james@127.0.0.1>") }
it "should not issue another hangup after it has been called in the DSL" do
handler_instance = Connfu.event_processor.handler_class.new({})
Connfu.event_processor.stub(:build_handler).and_return(handler_instance)
incoming :offer_presence, @call_jid, @client_jid
incoming :answer_result_iq, @call_jid
handler_instance.should_receive(:hangup).never
incoming :result_iq, @call_jid # from the hangup within DSL
incoming :hangup_presence, @call_jid
actor.call
Connfu.connection.commands.select { |c| c.is_a?(Connfu::Commands::Hangup) }.length.should == 1
end
end
......@@ -9,31 +9,30 @@ describe "say something on a call" do
end
end
before :each do
@call_id = "call-id"
@call_jid = "#{@call_id}@server.whatever"
@client_jid = "usera@127.0.0.whatever/voxeo"
end
let(:actor) { Actor.new("James Adam <sip:james@127.0.0.1>") }
it "should send first say command" do
incoming :offer_presence, @call_jid, @client_jid
Connfu.connection.pause_after :offer_presence
actor.call
last_command.should == Connfu::Commands::Say.new(:text => 'hello, this is connfu', :call_jid => @call_jid, :client_jid => @client_jid)
last_command.should == Connfu::Commands::Say.new(:text => 'hello, this is connfu', :call_jid => actor.call_jid, :client_jid => actor.client_jid)
end
it "should not send the second say command if the first command's success hasn't been received" do
incoming :offer_presence, @call_jid, @client_jid
incoming :say_result_iq, @call_jid
Connfu.connection.pause_after :say_result_iq
actor.call
last_command.should == Connfu::Commands::Say.new(:text => 'hello, this is connfu', :call_jid => @call_jid, :client_jid => @client_jid)
last_command.should == Connfu::Commands::Say.new(:text => 'hello, this is connfu', :call_jid => actor.call_jid, :client_jid => actor.client_jid)
end
it "should send the second say command once the first say command has completed" do
incoming :offer_presence, @call_jid, @client_jid
incoming :say_result_iq, @call_jid
incoming :say_success_presence, @call_jid
Connfu.connection.pause_after :say_success_presence
last_command.should == Connfu::Commands::Say.new(:text => 'http://www.phono.com/audio/troporocks.mp3', :call_jid => @call_jid, :client_jid => @client_jid)
actor.call
last_command.should == Connfu::Commands::Say.new(:text => 'http://www.phono.com/audio/troporocks.mp3', :call_jid => actor.call_jid, :client_jid => actor.client_jid)
end
end
......@@ -41,31 +40,26 @@ describe "stopping a say command" do
testing_dsl do
on :offer do |call|
answer
send_command Connfu::Commands::Say.new(:text => 'hello world', :call_jid => 'call-jid', :client_jid => 'client-jid')
send_command Connfu::Commands::Say.new(:text => 'hello world', :call_jid => call_jid, :client_jid => client_jid)
dial :to => "anyone", :from => "anyone else"
send_command Connfu::Commands::Stop.new(:component_id => 'component-id', :call_jid => 'call-jid', :client_jid => 'client-jid')
send_command Connfu::Commands::Stop.new(:component_id => 'say-component-id', :call_jid => call_jid, :client_jid => client_jid)
end
end
let(:stop_command) { Connfu::Commands::Stop.new(:component_id => 'component-id', :call_jid => 'call-jid', :client_jid => 'client-jid') }
let(:call_jid) { "call-id@#{PRISM_HOST}" }
let(:actor) { Actor.new("James Adam <sip:james@127.0.0.1>") }
it 'should send the stop command to an active say component' do
incoming :offer_presence, call_jid
incoming :answer_result_iq, call_jid, last_command.id
incoming :say_result_iq, call_jid
incoming :dial_result_iq, "dummy-call-so-we-can-wait-id", last_command.id
Connfu.connection.dont_send :say_success_presence
Connfu.connection.pause_after :dial_result_iq
actor.call
last_command.should == stop_command
last_command.should == Connfu::Commands::Stop.new(:component_id => 'say-component-id', :call_jid => actor.call_jid, :client_jid => actor.client_jid)
end
it 'should not send the stop command if the say component has already finished' do
incoming :offer_presence, call_jid
incoming :answer_result_iq, call_jid, last_command.id
incoming :say_result_iq, call_jid, 'component-id'
incoming :say_success_presence, "#{call_jid}/component-id"
incoming :dial_result_iq, "dummy-call-so-we-can-wait-id", last_command.id
actor.call
last_command.should_not == stop_command
last_command.should_not be_instance_of(Connfu::Commands::Stop)
end
end
\ No newline at end of file
......@@ -76,6 +76,23 @@ RSpec::Matchers.define :be_stanzas do |expected_stanzas|
end
end
class Actor
attr_reader :call_jid, :client_jid
def initialize(address)
@address = address
end
def call(recipient="<sip:usera@server.whatever>")
@call_jid="call-id@#{PRISM_HOST}"
@client_jid="#{PRISM_JID}/voxeo"
Connfu.connection.send_stanza :offer_presence, @call_jid, @client_jid, :from => @address, :to => recipient
while Connfu.connection.can_tick?
Connfu.connection.tick
end
end
end
class TestConnection
attr_accessor :commands
......@@ -83,10 +100,12 @@ class TestConnection
@commands = []
@handlers = {}
@domain = domain
@dont_send = []
end
def send_command(command)
@commands << command
@last_command = command
command.id
end
......@@ -107,6 +126,46 @@ class TestConnection
def wait_because_of_tropo_bug_133
# no-op when testing
end
def can_tick?
@last_command != nil
end
def tick
command = @last_command
@last_command = nil
case command
when Connfu::Commands::Answer
send_stanza :result_iq, command.call_jid, command.id
when Connfu::Commands::Hangup
send_stanza :result_iq, command.call_jid, command.id
when Connfu::Commands::Say
component_id = "say-component-id"
send_stanza :say_result_iq, command.call_jid, component_id
send_stanza :say_success_presence, command.call_jid + "/#{component_id}"
when Connfu::Commands::Dial
call_id = "new-call-id"
send_stanza :dial_result_iq, call_id, command.id
else
Connfu.logger.debug "no corresponding event required"
end
end
def pause_after(stanza)
@pause_after = stanza
end
def dont_send(stanza)
@dont_send << stanza
end
def send_stanza(*args)
if @paused || @dont_send.include?(args.first)
else
incoming(*args)
@paused = (args.first == @pause_after)
end
end
end
def last_command
......
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