use osrm-database during testing

This commit is contained in:
Emil Tin 2014-01-24 21:14:38 +01:00 committed by Emil Tin
parent bc6466cc36
commit 7ba8e51fa9
12 changed files with 192 additions and 55 deletions

View File

@ -0,0 +1,72 @@
@datastore
Feature: Loading data through datastore
# Normally when launching osrm-routed, it will keep running as a server until it's shut down.
# For testing program options, the --trial option is used, which causes osrm-routed to quit
# immediately after initialization. This makes testing easier and faster.
#
# The {base} part of the options to osrm-routed will be expanded to the actual base path of
# the preprocessed file.
Background:
Given the profile "testbot"
Scenario: Load data with osrm-datastore - medium size
Given the node map
| a | b | c |
| d | e | f |
And the ways
| nodes |
| abc |
| def |
| ad |
| be |
| cf |
And the data has been prepared
When I run "osrm-datastore --springclean"
Then stderr should be empty
And stdout should contain /^\[info\] spring-cleaning all shared memory regions/
And it should exit with code 0
When I run "osrm-datastore {base}.osrm"
Then stderr should be empty
And stdout should contain /^\[info\] all data loaded/
And it should exit with code 0
When I run "osrm-routed --sharedmemory --trial"
Then stderr should be empty
And stdout should contain /^\[info\] starting up engines/
And stdout should contain /\d{1,2}\.\d{1,2}\.\d{1,2}/
And stdout should contain /compiled at/
And stdout should contain /^\[info\] loaded plugin: viaroute/
And stdout should contain /^\[info\] trial run/
And stdout should contain /^\[info\] shutdown completed/
And it should exit with code 0
Scenario: Load data with osrm-datastore - small size
Given the node map
| a | b |
And the ways
| nodes |
| ab |
And the data has been prepared
When I run "osrm-datastore --springclean"
Then stderr should be empty
And stdout should contain /^\[info\] spring-cleaning all shared memory regions/
And it should exit with code 0
When I run "osrm-datastore {base}.osrm"
Then stderr should be empty
And stdout should contain /^\[info\] all data loaded/
And it should exit with code 0
When I run "osrm-routed --sharedmemory --trial"
Then stderr should be empty
And stdout should contain /^\[info\] starting up engines/
And stdout should contain /\d{1,2}\.\d{1,2}\.\d{1,2}/
And stdout should contain /compiled at/
And stdout should contain /^\[info\] loaded plugin: viaroute/
And stdout should contain /^\[info\] trial run/
And stdout should contain /^\[info\] shutdown completed/
And it should exit with code 0

View File

@ -1,7 +1,7 @@
When /^I request locate I should get$/ do |table| When /^I request locate I should get$/ do |table|
reprocess reprocess
actual = [] actual = []
OSRMBackgroundLauncher.new("#{@osm_file}.osrm") do OSRMLoader.load("#{@osm_file}.osrm") do
table.hashes.each_with_index do |row,ri| table.hashes.each_with_index do |row,ri|
in_node = find_node_by_name row['in'] in_node = find_node_by_name row['in']
raise "*** unknown in-node '#{row['in']}" unless in_node raise "*** unknown in-node '#{row['in']}" unless in_node

View File

@ -1,7 +1,7 @@
When /^I request nearest I should get$/ do |table| When /^I request nearest I should get$/ do |table|
reprocess reprocess
actual = [] actual = []
OSRMBackgroundLauncher.new("#{@osm_file}.osrm") do OSRMLoader.load("#{@osm_file}.osrm") do
table.hashes.each_with_index do |row,ri| table.hashes.each_with_index do |row,ri|
in_node = find_node_by_name row['in'] in_node = find_node_by_name row['in']
raise "*** unknown in-node '#{row['in']}" unless in_node raise "*** unknown in-node '#{row['in']}" unless in_node

View File

@ -1,6 +1,6 @@
When(/^I run "osrm\-routed\s?(.*?)"$/) do |options| When(/^I run "osrm\-routed\s?(.*?)"$/) do |options|
begin begin
Timeout.timeout(1) { run_bin 'osrm-routed', options } Timeout.timeout(SHUTDOWN_TIMEOUT) { run_bin 'osrm-routed', options }
rescue Timeout::Error rescue Timeout::Error
raise "*** osrm-routed didn't quit. Maybe the --trial option wasn't used?" raise "*** osrm-routed didn't quit. Maybe the --trial option wasn't used?"
end end
@ -14,6 +14,10 @@ When(/^I run "osrm\-prepare\s?(.*?)"$/) do |options|
run_bin 'osrm-prepare', options run_bin 'osrm-prepare', options
end end
When(/^I run "osrm\-datastore\s?(.*?)"$/) do |options|
run_bin 'osrm-datastore', options
end
Then /^it should exit with code (\d+)$/ do |code| Then /^it should exit with code (\d+)$/ do |code|
expect(@exit_code).to eq( code.to_i ) expect(@exit_code).to eq( code.to_i )
end end

View File

@ -1,6 +1,6 @@
When /^I request \/(.*)$/ do |path| When /^I request \/(.*)$/ do |path|
reprocess reprocess
OSRMBackgroundLauncher.new("#{@osm_file}.osrm") do OSRMLoader.load("#{@osm_file}.osrm") do
@response = request_path path @response = request_path path
end end
end end
@ -43,4 +43,4 @@ Then /^"([^"]*)" should return code (\d+)$/ do |binary, code|
expect(@process_error.is_a?(OSRMError)).to eq(true) expect(@process_error.is_a?(OSRMError)).to eq(true)
expect(@process_error.process).to eq(binary) expect(@process_error.process).to eq(binary)
expect(@process_error.code.to_i).to eq(code.to_i) expect(@process_error.code.to_i).to eq(code.to_i)
end end

View File

@ -44,7 +44,7 @@ Then /^routability should be$/ do |table|
if table.headers&["forw","backw","bothw"] == [] if table.headers&["forw","backw","bothw"] == []
raise "*** routability tabel must contain either 'forw', 'backw' or 'bothw' column" raise "*** routability tabel must contain either 'forw', 'backw' or 'bothw' column"
end end
OSRMBackgroundLauncher.new("#{@osm_file}.osrm") do OSRMLoader.load("#{@osm_file}.osrm") do
table.hashes.each_with_index do |row,i| table.hashes.each_with_index do |row,i|
output_row = row.dup output_row = row.dup
attempts = [] attempts = []

View File

@ -1,7 +1,7 @@
When /^I route I should get$/ do |table| When /^I route I should get$/ do |table|
reprocess reprocess
actual = [] actual = []
OSRMBackgroundLauncher.new("#{@osm_file}.osrm") do OSRMLoader.load("#{@osm_file}.osrm") do
table.hashes.each_with_index do |row,ri| table.hashes.each_with_index do |row,ri|
if row['request'] if row['request']
got = {'request' => row['request'] } got = {'request' => row['request'] }

View File

@ -225,13 +225,17 @@ def convert_osm_to_pbf
end end
def extracted? def extracted?
File.exist?("#{@osm_file}.osrm") && Dir.chdir TEST_FOLDER do
File.exist?("#{@osm_file}.osrm.names") && File.exist?("#{@osm_file}.osrm") &&
File.exist?("#{@osm_file}.osrm.restrictions") File.exist?("#{@osm_file}.osrm.names") &&
File.exist?("#{@osm_file}.osrm.restrictions")
end
end end
def prepared? def prepared?
File.exist?("#{@osm_file}.osrm.hsgr") Dir.chdir TEST_FOLDER do
File.exist?("#{@osm_file}.osrm.hsgr")
end
end end
def write_timestamp def write_timestamp

View File

@ -17,6 +17,20 @@ PROFILES_PATH = File.join ROOT_FOLDER, 'profiles'
BIN_PATH = File.join ROOT_FOLDER, 'build' BIN_PATH = File.join ROOT_FOLDER, 'build'
DEFAULT_INPUT_FORMAT = 'osm' DEFAULT_INPUT_FORMAT = 'osm'
DEFAULT_ORIGIN = [1,1] DEFAULT_ORIGIN = [1,1]
LAUNCH_TIMEOUT = 1
SHUTDOWN_TIMEOUT = 10
def log_time_and_run cmd
log_time cmd
`#{cmd}`
end
def log_time cmd
puts "[#{Time.now.strftime('%Y-%m-%d %H:%M:%S:%L')}] #{cmd}"
end
puts "Ruby version #{RUBY_VERSION}" puts "Ruby version #{RUBY_VERSION}"
@ -55,3 +69,7 @@ end
AfterConfiguration do |config| AfterConfiguration do |config|
clear_log_files clear_log_files
end end
at_exit do
OSRMLoader.shutdown
end

View File

@ -9,76 +9,82 @@ end
OSRM_ROUTED_LOG_FILE = 'osrm-routed.log' OSRM_ROUTED_LOG_FILE = 'osrm-routed.log'
class OSRMBackgroundLauncher class OSRMLoader
def initialize input_file, &block
@@pid = nil
def self.load input_file, &block
@input_file = input_file @input_file = input_file
Dir.chdir TEST_FOLDER do Dir.chdir TEST_FOLDER do
begin self.load_data
launch self.launch unless @@pid
yield yield
ensure
shutdown
end
end end
end end
def self.load_data
puts "=== loading data with osrm-datastore"
log_time_and_run "#{BIN_PATH}/osrm-datastore --springclean"
log_time_and_run "#{BIN_PATH}/osrm-datastore #{@input_file}"
end
private def self.launch
Timeout.timeout(LAUNCH_TIMEOUT) do
def launch self.osrm_up
Timeout.timeout(OSRM_TIMEOUT) do self.wait_for_connection
osrm_up
wait_for_connection
end end
rescue Timeout::Error rescue Timeout::Error
raise RoutedError.new "Launching osrm-routed timed out." raise RoutedError.new "Launching osrm-routed timed out."
end end
def shutdown def self.shutdown
Timeout.timeout(OSRM_TIMEOUT) do Timeout.timeout(SHUTDOWN_TIMEOUT) do
osrm_down self.osrm_down
end end
rescue Timeout::Error rescue Timeout::Error
kill self.kill
raise RoutedError.new "Shutting down osrm-routed timed out." raise RoutedError.new "Shutting down osrm-routed timed out."
end end
def self.osrm_up?
def osrm_up? if @@pid
if @pid s = `ps -o state -p #{@@pid}`.split[1].to_s.strip
begin up = (s =~ /^[DRST]/) != nil
if Process.waitpid(@pid, Process::WNOHANG) then # puts "=== osrm-routed, status pid #{@@pid}: #{s} (#{up ? 'up' : 'down'})"
false up
else else
true false
end
rescue Errno::ESRCH, Errno::ECHILD
false
end
end end
end end
def osrm_up def self.osrm_up
return if osrm_up? return if self.osrm_up?
@pid = Process.spawn("#{BIN_PATH}/osrm-routed #{@input_file} --port #{OSRM_PORT}",:out=>OSRM_ROUTED_LOG_FILE, :err=>OSRM_ROUTED_LOG_FILE) puts "=== launching osrm-routed"
Process.detach(@pid) # avoid zombie processes log %[Process.spawn("#{BIN_PATH}/osrm-routed --sharedmemory=1 --port #{OSRM_PORT}",:out=>'#{OSRM_ROUTED_LOG_FILE}', :err=>'#{OSRM_ROUTED_LOG_FILE}')]
@@pid = Process.spawn("#{BIN_PATH}/osrm-routed --sharedmemory=1 --port #{OSRM_PORT}",:out=>OSRM_ROUTED_LOG_FILE, :err=>OSRM_ROUTED_LOG_FILE)
end end
def osrm_down def self.osrm_down
if @pid if @@pid
Process.kill TERMSIGNAL, @pid puts '=== shutting down osrm'
wait_for_shutdown log_time "Process.kill 'TERM', #{@@pid}"
Process.kill 'TERM', @@pid
self.wait_for_shutdown
end end
end end
def kill def self.kill
if @pid if @@pid
Process.kill 'KILL', @pid puts '=== killing osrm'
log_time "Process.kill 'KILL', @@pid"
Process.kill 'KILL', @@pid
end end
end end
def wait_for_connection def self.wait_for_connection
while true while true
begin begin
log_time "TCPSocket.new('127.0.0.1', OSRM_PORT)"
socket = TCPSocket.new('127.0.0.1', OSRM_PORT) socket = TCPSocket.new('127.0.0.1', OSRM_PORT)
return return
rescue Errno::ECONNREFUSED rescue Errno::ECONNREFUSED
@ -87,8 +93,8 @@ class OSRMBackgroundLauncher
end end
end end
def wait_for_shutdown def self.wait_for_shutdown
while osrm_up? while self.osrm_up?
sleep 0.1 sleep 0.1
end end
end end

View File

@ -18,6 +18,7 @@ def request_path path, waypoints=[], options={}
uri = URI.parse ["#{HOST}/#{path}", params].compact.join('?') uri = URI.parse ["#{HOST}/#{path}", params].compact.join('?')
@query = uri.to_s @query = uri.to_s
Timeout.timeout(OSRM_TIMEOUT) do Timeout.timeout(OSRM_TIMEOUT) do
log_time "Net::HTTP.get_response #{uri}"
Net::HTTP.get_response uri Net::HTTP.get_response uri
end end
rescue Errno::ECONNREFUSED => e rescue Errno::ECONNREFUSED => e
@ -30,6 +31,7 @@ def request_url path
uri = URI.parse"#{HOST}/#{path}" uri = URI.parse"#{HOST}/#{path}"
@query = uri.to_s @query = uri.to_s
Timeout.timeout(OSRM_TIMEOUT) do Timeout.timeout(OSRM_TIMEOUT) do
log_time "Net::HTTP.get_response #{uri}"
Net::HTTP.get_response uri Net::HTTP.get_response uri
end end
rescue Errno::ECONNREFUSED => e rescue Errno::ECONNREFUSED => e

View File

@ -0,0 +1,31 @@
@routing @datastore
Feature: Temporary tests related to osrm-datastore
Background:
Given the profile "testbot"
Scenario: Scenario ab
Given the node map
| a | b |
And the ways
| nodes |
| ab |
When I route I should get
| from | to | route |
| a | b | ab |
| b | a | ab |
Scenario: Scenaria xy
Given the node map
| x | y |
And the ways
| nodes |
| xy |
When I route I should get
| from | to | route |
| x | y | xy |
| y | x | xy |