Skip to content

Commit

Permalink
Final (almost) version of AMI client
Browse files Browse the repository at this point in the history
  • Loading branch information
andrius committed Aug 7, 2019
1 parent a80cd5b commit 8e7cc1b
Show file tree
Hide file tree
Showing 6 changed files with 214 additions and 111 deletions.
35 changes: 35 additions & 0 deletions spec/ami_hooks_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
require "./spec_helper"

describe Asterisk::AMI do
# Testing basic actions
describe "#on_hooks" do
it "should trigger 'FullyBooted' callback" do
# can't use with_ami wrapper: FullyBooted aproaching next after login
ami = Asterisk::AMI.new username: "asterisk.cr", secret: "asterisk.cr"

ami.on_event("FullyBooted") do |ami, event|
event.event.should eq("FullyBooted")
event["status"].should match /Fully Booted/i
# runner already processed "FullyBooted" and ami.connected? expected to
# be true
# pp ami
# ami.connected?.should be_true
end

ami.login
# callback will be triggered by AMI right after login action
sleep 0.2
ami.logoff
end

it "should trigger 'on_close' callback" do
with_ami do |ami|
ami.on_close do |ami|
# AMI connection should be closed already
ami.connected?.should be_false
end
end
end

end
end
19 changes: 15 additions & 4 deletions spec/ami_loadtest_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,18 @@ describe Asterisk::AMI do
it "should correctly respond to all the invoked events within heavy loaded Asterisk" do
# # less noicy, please
# Asterisk.logger.level = Logger::ERROR

# consider increasing expects_answer_before timeout for higher value of
# fibers_count, CPU load could be high
expects_answer_before = 1.0

# how many parallel AMI connections should be tested
fibers_count = 10
test_loops_count = 500
fibers = Channel(Nil).new(fibers_count)

# how much loops of tests to execute
test_loops_count = 500

fibers_count.times do |spawn_no|
spawn_no_pretty = "0000#{spawn_no}"[-4, 4]
foobar = "foobar_#{spawn_no_pretty}"
Expand All @@ -29,7 +38,7 @@ describe Asterisk::AMI do
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 = ami.send_action({"action" => "SIPpeers", "actionid" => actionid}, expects_answer_before: expects_answer_before)
response.success?.should be_true
response.actionid.should eq(actionid)
response["eventlist"].should match /^start$/i
Expand Down Expand Up @@ -65,10 +74,12 @@ describe Asterisk::AMI do

# short random pause after loop to randomize fibers data
sleep 0.002 + rand(0.05)

rescue ex
puts %(\n\nAMI loadtest spec: #{ex.class}:#{ex.message}\n#{ex.backtrace.pretty_inspect}\n\nLatest response: #{response.inspect rescue "-- n/a ---"}\n\n)
break
end # test loop

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
Expand Down
26 changes: 26 additions & 0 deletions spec/ami_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@ require "./spec_helper"
describe Asterisk::AMI do
# Testing basic actions
describe "#basic_actions" do
it "after login, client should set ami_version, asterisk_version and asterisk_platform" do
with_ami do |ami|
ami.ami_version.should_not be_nil
ami.asterisk_version.should_not be_nil
ami.asterisk_platform.should_not be_nil
end
end

it "should respond with 'Pong' to action 'ping'" do
with_ami do |ami|
10.times do |i|
Expand Down Expand Up @@ -123,5 +131,23 @@ describe Asterisk::AMI do
end
end

it "ami method 'command' should return expected result" do
with_ami do |ami|
# returns one line
ami.command("core show version") =~ /Asterisk (\d{1,2}.\d{1,2}.\d{1,2}).+on a (\S+)/
asterisk_version = $1
asterisk_platform = $2
asterisk_version.should be_a(String)
asterisk_platform.should be_a(String)

# returns array (two lines)
ami.command("core show uptime").as(Array(String)).join("\n") =~ /(?|uptime: (.+)|reload: (.+))/i
uptime = $1
last_reload = $1
uptime.should be_a(String)
last_reload.should be_a(String)
end
end

end
end
2 changes: 1 addition & 1 deletion spec/asterisk_connection_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe Asterisk::Server do
end

it "after start, Asterisk Manager should listen on 127.0.0.1:5038" do
Asterisk::Server.port_is_open?("5038").should be_true
Asterisk::Server.port_open?("5038").should be_true
end

it "should stop by 'core stop now' CLI command" do
Expand Down
Loading

0 comments on commit 8e7cc1b

Please sign in to comment.