Skip to content

Commit

Permalink
updated specs
Browse files Browse the repository at this point in the history
  • Loading branch information
andrius committed Aug 6, 2019
1 parent 8176487 commit a80cd5b
Show file tree
Hide file tree
Showing 7 changed files with 212 additions and 239 deletions.
41 changes: 16 additions & 25 deletions spec/ami_connection_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ describe Asterisk::AMI do
unless Asterisk::Server.running?
Asterisk::Server.start
# let Asterisk boot
sleep 3.seconds
sleep 5.seconds
end
end

Expand All @@ -33,7 +33,6 @@ describe Asterisk::AMI do
ami = Asterisk::AMI.new username: "asterisk.cr", secret: "asterisk.cr"
ami.login
ami.connected?.should be_true
sleep 0.01
ami.logoff
ami.connected?.should be_false
end
Expand All @@ -42,33 +41,25 @@ describe Asterisk::AMI do
ami = Asterisk::AMI.new username: "asterisk.cr", secret: "asterisk.cr"
ami.login
ami.connected?.should be_true
channel = Channel(Exception? | Nil).new
spawn do
sleep 2.seconds
Asterisk::Server.kill
channel.send(nil)
end
channel.receive
sleep 0.5.seconds
sleep 2.seconds
Asterisk::Server.kill
Asterisk::Server.running?.should be_false
ami.connected?.should be_false
ami.logoff
end

# TODO: rework this test
# it "should not let to login if asterisk is not yet fully booted" do
# Asterisk::Server.kill
# Asterisk::Server.running?.should be_false
# Asterisk::Server.start
# sleep 0.2
# loop do
# sleep 0.1
# break if Asterisk::Server.running?
# end
# ami = Asterisk::AMI.new username: "asterisk.cr", secret: "asterisk.cr"
# expect_raises(Asterisk::AMI::NotBootedError) do
# ami.login
# end
# end
it "should not let to login if asterisk is not yet fully booted" do
Asterisk::Server.kill
Asterisk::Server.running?.should be_false
Asterisk::Server.start
# nmap dependency
while Asterisk::Server.port_closed?
sleep 0.01
end
ami = Asterisk::AMI.new username: "asterisk.cr", secret: "asterisk.cr"
expect_raises(Asterisk::AMI::NotBootedError) do
ami.login
end
end
end
end
100 changes: 56 additions & 44 deletions spec/ami_loadtest_spec.cr
Original file line number Diff line number Diff line change
@@ -1,66 +1,78 @@
require "./spec_helper"


describe Asterisk::AMI do
Spec.before_each do
# less noicy, please
# Asterisk.logger.level = Logger::ERROR
# Ensure that asterisk is up and running for each test
unless Asterisk::Server.running?
Asterisk::Server.start
# let Asterisk boot
sleep 3.seconds
end
end

it "should correctly respond to all the invoked events within heavy loaded Asterisk" do
# # less noicy, please
# Asterisk.logger.level = Logger::ERROR
fibers_count = 10
test_loops_count = 500
fibers = Channel(Nil).new(fibers_count)
fibers_count.times do |spawn_no|
spawn_no_pretty = "0000#{spawn_no}"[-4,4]
spawn_no_pretty = "0000#{spawn_no}"[-4, 4]
foobar = "foobar_#{spawn_no_pretty}"
spawn do
ami = Asterisk::AMI.new username: "asterisk.cr", secret: "asterisk.cr"
# ami = Asterisk::AMI.new port: "15038", username: "88a26ad6ee3cff2cbb2fe3f49f7532e6", secret: "e47108082ec2bb74d83f7b3067b19396"
ami.login
with_ami(username: "asterisk.cr", secret: "asterisk.cr") do |ami|
test_loops_count.times do |test_no|
test_no_pretty = "0000#{test_no}"[-4, 4]

50.times do |test_no|
test_no_pretty = "0000#{test_no}"[-4,4]
actionid = Random::Secure.hex(8)
response = ami.send_action({"action" => "Ping", "actionid" => actionid})
response.success?.should be_true
response.actionid.should eq(actionid)
response["ping"].should match /Pong/i

response = ami.send_action({"action" => "Ping"})
response["ping"].should match /Pong/i
actionid = Random::Secure.hex(8)
foobar_value = %(#{spawn_no_pretty}-#{test_no_pretty}-#{Random::Secure.hex(8)})
response = ami.send_action({"action" => "Setvar", "Variable" => foobar, "Value" => foobar_value, "actionid" => actionid})
response.success?.should be_true
response.actionid.should eq(actionid)
response.message.should match /Variable Set/i

foobar_value = %(#{spawn_no_pretty}-#{test_no_pretty}-#{Random::Secure.hex(8)})
response = ami.send_action({"action" => "Setvar", "Variable" => foobar, "Value" => foobar_value})
response.message.should match /Variable Set/i
actionid = Random::Secure.hex(8)
response = ami.send_action({"action" => "SIPpeers", "actionid" => actionid}, expects_answer_before: 0.5)
response.success?.should be_true
response.actionid.should eq(actionid)
response["eventlist"].should match /^start$/i
response.message.should match /list will follow/i

response = ami.send_action({"action" => "SIPpeers"}, expects_answer_before: 0.5)
response["eventlist"].should match /^start$/i
response.message.should match /list will follow/i
actionid = Random::Secure.hex(8)
response = ami.send_action({"action" => "Getvar", "Variable" => foobar, "actionid" => actionid})
response.success?.should be_true
response.actionid.should eq(actionid)
response.value.should eq(foobar_value)

response = ami.send_action({"action" => "Getvar", "Variable" => foobar})
response.value.should eq(foobar_value)
actionid = Random::Secure.hex(8)
response = ami.send_action({"action" => "ListCommands", "actionid" => actionid})
response.success?.should be_true
response.actionid.should eq(actionid)
response.data.has_key?("blindtransfer").should be_truthy
response.success?.should be_true

response = ami.send_action({"action" => "ListCommands"})
response.data.has_key?("blindtransfer").should be_truthy
# actionid = Random::Secure.hex(8)
# response = ami.send_action({"action" => "Queues", "actionid" => actionid})
# response.actionid?.should be_falsey
# response["unknown"].should match /No queues/i

# response = ami.send_action({"action" => "Queues"})
# response["unknown"].should match /No queues/i
actionid = Random::Secure.hex(8)
response = ami.send_action({"action" => "Command", "command" => "agi show commands", "actionid" => actionid})
response.actionid.should eq(actionid)
response.output.as(Array(String)).join("\n").should match /database del/im

response = ami.send_action({"action" => "Command", "command" => "agi show commands"})
response.output.as(Array(String)).join("\n").should match /database del/im
actionid = Random::Secure.hex(8)
response = ami.send_action({"action" => "Command", "command" => "sip show peers", "actionid" => actionid})
response.actionid.should eq(actionid)
response.output.as(Array(String)).join("\n").should match /test-account-905/im

response = ami.send_action({"action" => "Command", "command" => "sip show peers"})
response.output.as(Array(String)).join("\n").should match /test-account-905/im
# short random pause after loop to randomize fibers data
sleep 0.002 + rand(0.05)
end # test loop

# short random pause after loop to randomize fibers data
sleep 0.002 + rand(0.05)
end # test loop
rescue ex
logger.error "AMI loadtest spec: #{ex.class}:#{ex.message} #{ex.backtrace}"
ensure
fibers.send nil
end # spawn
rescue ex
puts "\n\nAMI loadtest spec: #{ex.class}:#{ex.message}\n#{ex.backtrace.pretty_inspect}\n\n"
ensure
fibers.send nil
end # with_ami
end # spawn
end

fibers.receive
Expand Down
Loading

0 comments on commit a80cd5b

Please sign in to comment.