def test_start_osrm_server(): """Test start_osrm_server method starts OSRM server""" already_running_servers = { proc.pid for proc in psutil.process_iter() if proc.cmdline()[0] == "osrm-routed" } controller = OsrmController( data_dir=settings.OSRM_DATA_DIR, server_ids=[server_id], port_bindings={server_id: 10_000}, process_ids={server_id: (1, 1.0)}, ) with patch("osrm.osrmcontroller.OsrmController.update_server_pid_in_redis" ) as update_server_pid_in_redis: controller.start_osrm_server(server_id) for proc in psutil.process_iter(): if proc.cmdline(): if (proc.cmdline()[0] == "osrm-routed" and proc.pid not in already_running_servers): update_server_pid_in_redis.assert_called_with( server_name=server_id.name, server_process_info=(proc.pid, proc.create_time()), ) assert proc.is_running()
def test_status(): """ Test status method. """ controller = OsrmController( data_dir=settings.OSRM_DATA_DIR, server_ids=[server_id], port_bindings={server_id: 10_000}, process_ids={server_id: (1, 1.0)}, ) with patch( "osrm.osrmcontroller.OsrmController.get_process_for_server_id", lambda _, __: psutil.Process(), ): with patch("osrm.osrmcontroller.psutil.Process.status", lambda _: "running"): status = controller.status() assert status == { server_id.name: { "port": 10_000, "status": "Running", "busy": True } }
def init_server(server_name: str): """ Init OSRM server :param server_name: name of server to initialize """ osrm_controller = OsrmController.get_controller_from_redis() osrm_controller.init_osrm_server(osrm_controller.get_server_id(server_name))
def test_contract_with_traffic(controller_contract): """ Test contract with traffic task. """ server_id = OsrmServerId( name="Car", data="/data/Car/lj-graph-for-tests.osm.pbf", profile="'/data/Car/car.lua'", ) controller = OsrmController( settings.OSRM_DATA_DIR, server_ids=[server_id], port_bindings={server_id: 10000}, process_ids={server_id: (1, 1.0)}, ) traffic_data = "traffic info" with patch( "osrm.tasks.OsrmController.get_controller_from_redis", lambda: controller ), patch("osrm.tasks.OsrmController.restore_osrm_data") as restore_osrm_data: contract(server_name=server_id.name, traffic_data=traffic_data) controller_contract.assert_called_with(server_id, traffic_data=traffic_data) restore_osrm_data.assert_called_once_with(server_id)
def test_to_dict(): """Test to_dict method""" controller = OsrmController( data_dir=settings.OSRM_DATA_DIR, server_ids=[server_id], port_bindings={server_id: 10_000}, process_ids={server_id: (1, 1.0)}, ) controller_dict = { "data_dir": settings.OSRM_DATA_DIR, "server_ids": [server_id], "port_bindings": [(server_id, 10_000)], } assert controller.to_dict() == controller_dict
def restart(server_name: str): """ Task to restart OSRM server. """ osrm_controller = OsrmController.get_controller_from_redis() server_id = osrm_controller.get_server_id(server_name) osrm_controller.restart_osrm_server(server_id)
def test_restart_osrm_server(): """Test restart_osrm_server method""" controller = OsrmController( data_dir=settings.OSRM_DATA_DIR, server_ids=[server_id], port_bindings={server_id: 10_000}, process_ids={server_id: (1, 1.0)}, ) with patch("osrm.osrmcontroller.OsrmController.kill_osrm_server" ) as kill_osrm_server: with patch("osrm.osrmcontroller.OsrmController.start_osrm_server" ) as start_osrm_server: controller.restart_osrm_server(server_id) kill_osrm_server.assert_called_with(server_id) start_osrm_server.assert_called_with(server_id)
def extract(server_name: str): """ Task to extract OSRM data. """ osrm_controller = OsrmController.get_controller_from_redis() server_id = osrm_controller.get_server_id(server_name) osrm_controller.extract_osm_data(server_id) osrm_controller.save_osrm_data(server_id)
def contract(server_name: str, traffic_data: Optional[str] = None): """ Task to contract with traffic data. :param traffic_data: Contents of the traffic CSV file (as defined by Project OSRM) """ osrm_controller = OsrmController.get_controller_from_redis() server_id = osrm_controller.get_server_id(server_name) osrm_controller.restore_osrm_data(server_id) osrm_controller.contract_osm_data(server_id, traffic_data=traffic_data)
def test_kill_osrm_server(): """ Test kill_osrm_server method starts OSRM server. """ controller = OsrmController( data_dir=settings.OSRM_DATA_DIR, server_ids=[server_id], port_bindings={server_id: 10_000}, process_ids={server_id: (1, 1.0)}, ) with patch( "osrm.osrmcontroller.OsrmController.update_server_pid_in_redis"): controller.start_osrm_server(server_id) pid = None for proc in psutil.process_iter(): if proc.cmdline(): if proc.cmdline()[0] == "osrm-routed": pid = proc.pid assert pid is not None with patch( "osrm.osrmcontroller.redis.Redis.delete") as delete_key_in_redis: controller.kill_osrm_server(server_id) delete_key_in_redis.assert_called_with(settings.REDIS_SERVER_KEY_TEMPLATE + server_id.name) with pytest.raises(psutil.NoSuchProcess): psutil.Process(pid)
def test_init_server(controller_init_server): """ Test init server task. """ server_id = OsrmServerId( name="Car", data="/data/Car/lj-graph-for-tests.osm.pbf", profile="'/data/Car/car.lua'", ) controller = OsrmController( settings.OSRM_DATA_DIR, server_ids=[server_id], port_bindings={server_id: 10000}, process_ids={server_id: (1, 1.0)}, ) with patch( "osrm.tasks.OsrmController.get_controller_from_redis", lambda: controller ): init_server(server_name=server_id.name) controller_init_server.assert_called_with(server_id)
import io import requests from osrm import settings from osrm.osrmcontroller import OsrmController, OsrmServerId API_ADDRESS = f"http://localhost:{settings.MANAGER_LISTEN_PORT}" # pylint: disable=invalid-name server_id = OsrmServerId( name="Car", data="lj-graph-for-tests.osm.pbf", profile="car.lua" ) controller = OsrmController( data_dir=settings.OSRM_DATA_DIR, server_ids=[server_id], port_bindings={server_id: 10_000}, process_ids={server_id: (1, 1.0)}, ) def test_status_api(): """ Check status API call works. """ result = requests.get(f"{API_ADDRESS}/status") assert result.status_code == 200 try: assert result.json() == { "Car": {"port": 15000, "status": "Sleeping", "busy": False}, "Bicycle": {"port": 15001, "status": "Sleeping", "busy": False},
def status(): """Status of OSRM servers""" # need to load controller from Redis as server process IDs could have changed # if OSRM server was restarted since creation of API recent_osrm_controller = OsrmController.get_controller_from_redis() return recent_osrm_controller.status()
chain = server_init_task else: chain = chain | server_init_task if settings.OSRM_INITIAL_CONTRACT: chain = chain | osrm_initial_contract_chain(server_id) if chain is not None: chain.delay() # pylint: disable=invalid-name # ^ Not all variables are constants! logging.basicConfig(level=logging.DEBUG) controller = OsrmController.init_from_data_location(settings.OSRM_DATA_DIR) app = api_factory(controller) CORS(app) def init_controller(): """ Initialize the OSRM controller. """ if settings.INIT_PARALLEL: init_parallel(controller) else: init_serial(controller)