improve cuke process management, support OSRM_PORT

This commit is contained in:
Emil Tin
2012-12-15 12:34:53 +01:00
parent 29344f55ae
commit ae106a3a90
9 changed files with 122 additions and 57 deletions
+1 -1
View File
@@ -15,7 +15,7 @@ def write_server_ini
s=<<-EOF
Threads = 1
IP = 0.0.0.0
Port = 5000
Port = #{OSRM_PORT}
hsgrData=#{@osm_file}.osrm.hsgr
nodesData=#{@osm_file}.osrm.nodes
+11 -1
View File
@@ -1 +1,11 @@
require 'rspec/expectations'
require 'rspec/expectations'
DEFAULT_PORT = 5000
if ENV["OSRM_PORT"]
OSRM_PORT = ENV["OSRM_PORT"].to_i
puts "Port set to #{OSRM_PORT}"
else
OSRM_PORT = DEFAULT_PORT
puts "Using default port #{OSRM_PORT}"
end
+5 -3
View File
@@ -1,3 +1,6 @@
STRESS_TIMEOUT = 300
Before do |scenario|
@scenario_title = scenario.title
@scenario_time = Time.now.strftime("%Y-%m-%dT%H:%m:%SZ")
@@ -7,12 +10,11 @@ Before do |scenario|
set_grid_size DEFAULT_GRID_SIZE
end
Around('@routing') do |scenario, block|
Timeout.timeout(10) do
Around('@stress') do |scenario, block|
Timeout.timeout(STRESS_TIMEOUT) do
block.call
end
end
After do
osrm_kill
end
+58 -46
View File
@@ -1,71 +1,83 @@
require 'socket'
require 'sys/proctable'
LAUNCH_TIMEOUT = 5
SHUTDOWN_TIMEOUT = 5
class OSRMLauncher
def initialize &block
Dir.chdir TEST_FOLDER do
osrm_up
yield
osrm_down
end
end
end
def each_process name, &block
Sys::ProcTable.ps do |process|
if process.comm.strip == name.strip
yield process.pid.to_i, process.state.strip
begin
begin
Timeout.timeout(LAUNCH_TIMEOUT) do
osrm_up
wait_for_connection
end
rescue Timeout::Error
raise "*** Launching osrm-routed timed out."
end
yield
ensure
begin
Timeout.timeout(SHUTDOWN_TIMEOUT) do
osrm_down
end
rescue Timeout::Error
raise "*** Shutting down osrm-routed timed out."
end
end
end
end
end
def osrm_up?
find_pid('osrm-routed') != nil
end
def find_pid name
each_process(name) { |pid,state| return pid.to_i }
return nil
if @pipe
begin
Process.getpgid @pipe.pid
true
rescue Errno::ESRCH
false
end
else
false
end
end
def osrm_up
return if osrm_up?
pipe = IO.popen('../osrm-routed 1>>osrm-routed.log 2>>osrm-routed.log')
timeout = 5
(timeout*10).times do
#exec avoids popen running osrm-routed inside a shell
#if the cmd is run inside a shell, popen returns the pid for the shell, and if we try to kill it,
#the child process is orphaned, and we can't terminate it.
@pipe = IO.popen('exec ../osrm-routed 1>>osrm-routed.log 2>>osrm-routed.log')
end
def osrm_down
if @pipe
Process.kill 'TERM', @pipe.pid
wait_for_shutdown
@pipe = nil
end
end
def kill
if @pipe
Process.kill 'KILL', @pipe.pid
end
end
def wait_for_connection
while true
begin
socket = TCPSocket.new('localhost', 5000)
socket.puts 'ping'
socket = TCPSocket.new('localhost', OSRM_PORT)
return
rescue Errno::ECONNREFUSED
sleep 0.1
end
end
sleep 0.1
end
def osrm_down
each_process('osrm-routed') { |pid,state| Process.kill 'TERM', pid }
each_process('osrm-prepare') { |pid,state| Process.kill 'TERM', pid }
each_process('osrm-extract') { |pid,state| Process.kill 'TERM', pid }
wait_for_shutdown 'osrm-routed'
wait_for_shutdown 'osrm-prepare'
wait_for_shutdown 'osrm-extract'
end
def osrm_kill
each_process('osrm-routed') { |pid,state| Process.kill 'KILL', pid }
each_process('osrm-prepare') { |pid,state| Process.kill 'KILL', pid }
each_process('osrm-extract') { |pid,state| Process.kill 'KILL', pid }
wait_for_shutdown 'osrm-routed'
wait_for_shutdown 'osrm-prepare'
wait_for_shutdown 'osrm-extract'
end
def wait_for_shutdown name
timeout = 10
(timeout*10).times do
return if find_pid(name) == nil
def wait_for_shutdown
while osrm_up?
sleep 0.1
end
raise "*** Could not terminate #{name}."
end
+5 -2
View File
@@ -1,12 +1,15 @@
require 'net/http'
HOST = 'http://localhost:5000'
HOST = "http://localhost:#{OSRM_PORT}"
REQUEST_TIMEOUT = 1
def request_path path
@query = path
log path
uri = URI.parse "#{HOST}/#{path}"
Net::HTTP.get_response uri
Timeout.timeout(REQUEST_TIMEOUT) do
Net::HTTP.get_response uri
end
rescue Errno::ECONNREFUSED => e
raise "*** osrm-routed is not running."
rescue Timeout::Error