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

View File

@ -1,7 +1,6 @@
When /^I preprocess data$/ do When /^I preprocess data$/ do
begin begin
osrm_kill
reprocess reprocess
rescue OSRMError => e rescue OSRMError => e
@process_error = e @process_error = e

View File

@ -1,5 +1,4 @@
When /^I request \/(.*)$/ do |path| When /^I request \/(.*)$/ do |path|
osrm_kill
reprocess reprocess
OSRMLauncher.new do OSRMLauncher.new do
@response = request_path path @response = request_path path

View File

@ -134,7 +134,6 @@ Then /^"([^"]*)" should be returned$/ do |route|
end end
Then /^routability should be$/ do |table| Then /^routability should be$/ do |table|
osrm_kill
build_ways_from_table table build_ways_from_table table
reprocess reprocess
actual = [] actual = []
@ -176,7 +175,6 @@ Then /^routability should be$/ do |table|
end end
When /^I route I should get$/ do |table| When /^I route I should get$/ do |table|
osrm_kill
reprocess reprocess
actual = [] actual = []
OSRMLauncher.new do OSRMLauncher.new do
@ -265,3 +263,11 @@ When /^I route I should get$/ do |table|
end end
table.routing_diff! actual table.routing_diff! actual
end end
When /^I route (\d+) times I should get$/ do |n,table|
ok = true
n.to_i.times do
ok = false unless step "I route I should get", table
end
ok
end

View File

@ -0,0 +1,34 @@
@stress @launch
Feature: Launching and shutting down
Background:
Given the speedprofile "testbot"
Scenario: Repeated launch and shutdown
Given a grid size of 10000 meters
Given the node map
| h | a | b |
| g | x | c |
| f | e | d |
And the ways
| nodes | highway |
| xa | primary |
| xb | primary |
| xc | primary |
| xd | primary |
| xe | primary |
| xf | primary |
| xg | primary |
| xh | primary |
When I route 100 times I should get
| from | to | route |
| x | a | xa |
| x | b | xb |
| x | c | xc |
| x | d | xd |
| x | e | xe |
| x | f | xf |
| x | g | xg |
| x | h | xh |

View File

@ -15,7 +15,7 @@ def write_server_ini
s=<<-EOF s=<<-EOF
Threads = 1 Threads = 1
IP = 0.0.0.0 IP = 0.0.0.0
Port = 5000 Port = #{OSRM_PORT}
hsgrData=#{@osm_file}.osrm.hsgr hsgrData=#{@osm_file}.osrm.hsgr
nodesData=#{@osm_file}.osrm.nodes nodesData=#{@osm_file}.osrm.nodes

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

View File

@ -1,3 +1,6 @@
STRESS_TIMEOUT = 300
Before do |scenario| Before do |scenario|
@scenario_title = scenario.title @scenario_title = scenario.title
@scenario_time = Time.now.strftime("%Y-%m-%dT%H:%m:%SZ") @scenario_time = Time.now.strftime("%Y-%m-%dT%H:%m:%SZ")
@ -7,12 +10,11 @@ Before do |scenario|
set_grid_size DEFAULT_GRID_SIZE set_grid_size DEFAULT_GRID_SIZE
end end
Around('@routing') do |scenario, block| Around('@stress') do |scenario, block|
Timeout.timeout(10) do Timeout.timeout(STRESS_TIMEOUT) do
block.call block.call
end end
end end
After do After do
osrm_kill
end end

View File

@ -1,71 +1,83 @@
require 'socket' require 'socket'
require 'sys/proctable' require 'sys/proctable'
LAUNCH_TIMEOUT = 5
SHUTDOWN_TIMEOUT = 5
class OSRMLauncher class OSRMLauncher
def initialize &block def initialize &block
Dir.chdir TEST_FOLDER do Dir.chdir TEST_FOLDER do
begin
begin
Timeout.timeout(LAUNCH_TIMEOUT) do
osrm_up osrm_up
wait_for_connection
end
rescue Timeout::Error
raise "*** Launching osrm-routed timed out."
end
yield yield
ensure
begin
Timeout.timeout(SHUTDOWN_TIMEOUT) do
osrm_down osrm_down
end end
rescue Timeout::Error
raise "*** Shutting down osrm-routed timed out."
end
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
end end
end end
end end
def osrm_up? def osrm_up?
find_pid('osrm-routed') != nil if @pipe
end begin
Process.getpgid @pipe.pid
def find_pid name true
each_process(name) { |pid,state| return pid.to_i } rescue Errno::ESRCH
return nil false
end
else
false
end
end end
def osrm_up def osrm_up
return if osrm_up? return if osrm_up?
pipe = IO.popen('../osrm-routed 1>>osrm-routed.log 2>>osrm-routed.log') #exec avoids popen running osrm-routed inside a shell
timeout = 5 #if the cmd is run inside a shell, popen returns the pid for the shell, and if we try to kill it,
(timeout*10).times do #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 begin
socket = TCPSocket.new('localhost', 5000) socket = TCPSocket.new('localhost', OSRM_PORT)
socket.puts 'ping' return
rescue Errno::ECONNREFUSED rescue Errno::ECONNREFUSED
sleep 0.1 sleep 0.1
end end
end end
sleep 0.1
end end
def osrm_down def wait_for_shutdown
each_process('osrm-routed') { |pid,state| Process.kill 'TERM', pid } while osrm_up?
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
sleep 0.1 sleep 0.1
end end
raise "*** Could not terminate #{name}."
end end

View File

@ -1,12 +1,15 @@
require 'net/http' require 'net/http'
HOST = 'http://localhost:5000' HOST = "http://localhost:#{OSRM_PORT}"
REQUEST_TIMEOUT = 1
def request_path path def request_path path
@query = path @query = path
log path log path
uri = URI.parse "#{HOST}/#{path}" uri = URI.parse "#{HOST}/#{path}"
Timeout.timeout(REQUEST_TIMEOUT) do
Net::HTTP.get_response uri Net::HTTP.get_response uri
end
rescue Errno::ECONNREFUSED => e rescue Errno::ECONNREFUSED => e
raise "*** osrm-routed is not running." raise "*** osrm-routed is not running."
rescue Timeout::Error rescue Timeout::Error