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
+8 -4
View File
@@ -225,13 +225,17 @@ def convert_osm_to_pbf
end
def extracted?
File.exist?("#{@osm_file}.osrm") &&
File.exist?("#{@osm_file}.osrm.names") &&
File.exist?("#{@osm_file}.osrm.restrictions")
Dir.chdir TEST_FOLDER do
File.exist?("#{@osm_file}.osrm") &&
File.exist?("#{@osm_file}.osrm.names") &&
File.exist?("#{@osm_file}.osrm.restrictions")
end
end
def prepared?
File.exist?("#{@osm_file}.osrm.hsgr")
Dir.chdir TEST_FOLDER do
File.exist?("#{@osm_file}.osrm.hsgr")
end
end
def write_timestamp
+18
View File
@@ -17,6 +17,20 @@ PROFILES_PATH = File.join ROOT_FOLDER, 'profiles'
BIN_PATH = File.join ROOT_FOLDER, 'build'
DEFAULT_INPUT_FORMAT = 'osm'
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}"
@@ -55,3 +69,7 @@ end
AfterConfiguration do |config|
clear_log_files
end
at_exit do
OSRMLoader.shutdown
end
+50 -44
View File
@@ -9,76 +9,82 @@ end
OSRM_ROUTED_LOG_FILE = 'osrm-routed.log'
class OSRMBackgroundLauncher
def initialize input_file, &block
class OSRMLoader
@@pid = nil
def self.load input_file, &block
@input_file = input_file
Dir.chdir TEST_FOLDER do
begin
launch
yield
ensure
shutdown
end
self.load_data
self.launch unless @@pid
yield
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 launch
Timeout.timeout(OSRM_TIMEOUT) do
osrm_up
wait_for_connection
def self.launch
Timeout.timeout(LAUNCH_TIMEOUT) do
self.osrm_up
self.wait_for_connection
end
rescue Timeout::Error
raise RoutedError.new "Launching osrm-routed timed out."
end
def shutdown
Timeout.timeout(OSRM_TIMEOUT) do
osrm_down
def self.shutdown
Timeout.timeout(SHUTDOWN_TIMEOUT) do
self.osrm_down
end
rescue Timeout::Error
kill
self.kill
raise RoutedError.new "Shutting down osrm-routed timed out."
end
def osrm_up?
if @pid
begin
if Process.waitpid(@pid, Process::WNOHANG) then
false
else
true
end
rescue Errno::ESRCH, Errno::ECHILD
false
end
def self.osrm_up?
if @@pid
s = `ps -o state -p #{@@pid}`.split[1].to_s.strip
up = (s =~ /^[DRST]/) != nil
# puts "=== osrm-routed, status pid #{@@pid}: #{s} (#{up ? 'up' : 'down'})"
up
else
false
end
end
def osrm_up
return if osrm_up?
@pid = Process.spawn("#{BIN_PATH}/osrm-routed #{@input_file} --port #{OSRM_PORT}",:out=>OSRM_ROUTED_LOG_FILE, :err=>OSRM_ROUTED_LOG_FILE)
Process.detach(@pid) # avoid zombie processes
def self.osrm_up
return if self.osrm_up?
puts "=== launching osrm-routed"
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
def osrm_down
if @pid
Process.kill TERMSIGNAL, @pid
wait_for_shutdown
def self.osrm_down
if @@pid
puts '=== shutting down osrm'
log_time "Process.kill 'TERM', #{@@pid}"
Process.kill 'TERM', @@pid
self.wait_for_shutdown
end
end
def kill
if @pid
Process.kill 'KILL', @pid
def self.kill
if @@pid
puts '=== killing osrm'
log_time "Process.kill 'KILL', @@pid"
Process.kill 'KILL', @@pid
end
end
def wait_for_connection
def self.wait_for_connection
while true
begin
log_time "TCPSocket.new('127.0.0.1', OSRM_PORT)"
socket = TCPSocket.new('127.0.0.1', OSRM_PORT)
return
rescue Errno::ECONNREFUSED
@@ -87,8 +93,8 @@ class OSRMBackgroundLauncher
end
end
def wait_for_shutdown
while osrm_up?
def self.wait_for_shutdown
while self.osrm_up?
sleep 0.1
end
end
+2
View File
@@ -18,6 +18,7 @@ def request_path path, waypoints=[], options={}
uri = URI.parse ["#{HOST}/#{path}", params].compact.join('?')
@query = uri.to_s
Timeout.timeout(OSRM_TIMEOUT) do
log_time "Net::HTTP.get_response #{uri}"
Net::HTTP.get_response uri
end
rescue Errno::ECONNREFUSED => e
@@ -30,6 +31,7 @@ def request_url path
uri = URI.parse"#{HOST}/#{path}"
@query = uri.to_s
Timeout.timeout(OSRM_TIMEOUT) do
log_time "Net::HTTP.get_response #{uri}"
Net::HTTP.get_response uri
end
rescue Errno::ECONNREFUSED => e