def setup_class(cls): # Startup the API on a process before all tests # NOTE process can make mocking more difficult api_port = api_settings.port = get_free_port() cls.api_url = f"http://localhost:{api_port}" cls.api_process = Process(target=run, daemon=True) cls.api_process.start() wait_for(port=api_port)
def start_api(): global _api_process if _api_process: _api_process.terminate() _api_process.join() _api_process = Process(target=run, daemon=True) _api_process.start() wait_for(port=8000)
def test_wait_for_port_not_in_use(free_port, random_timeout): """Wait for a port that is currently free. Should raise WaitForTimeoutError with host:port attributes, after the given timeout """ with expect_time(random_timeout + 0.5): with pytest.raises(WaitForTimeoutError): try: wait_for(free_port, timeout=random_timeout) except WaitForTimeoutError as error: assert error.host == "127.0.0.1" assert error.port == free_port raise error
def test_wait_for_port_not_in_use_not_raise(free_port, random_timeout): """Wait for a port that is currently free, without raising exception. Should return False after the given timeout """ with expect_time(random_timeout + 0.5): r = wait_for(free_port, timeout=random_timeout, raise_error=False) assert r is False
def test_wait_for_lazy_port_in_use_not_raise(lazy_port_in_use, random_timeout): """Wait for a port that is currently in used, but not listening; without raising exception. Should return False after the given timeout """ with expect_time(random_timeout + 0.5): r = wait_for(lazy_port_in_use, timeout=random_timeout, raise_error=False) assert r is False
def test_wait_for_port_in_use(port_in_use): """Wait for a port that is currently in use. Should run ASAP and return True """ r = wait_for(port_in_use) assert r is True