diff --git a/test/conftest.py b/test/conftest.py index a7e3527a38460cdda8d28a13a0ec45470cc58bb4..7ffdfe8b8bb504047e010489a0f6821c119fd18b 100644 --- a/test/conftest.py +++ b/test/conftest.py @@ -1,6 +1,8 @@ #!/usr/bin/env python3 -from collections import namedtuple import glob +import pytest +import subprocess +from collections import namedtuple from os.path import abspath, basename Workload = namedtuple('Workload', ['name', 'filename']) @@ -60,3 +62,23 @@ def pytest_generate_tests(metafunc): if 'upper_llh_threshold' in metafunc.fixturenames: metafunc.parametrize('upper_llh_threshold', [60]) + +@pytest.fixture(scope="session", autouse=True) +def manage_redis_server(request): + print('Trying to run a redis-server...') + proc = subprocess.Popen('redis-server', stdout=subprocess.PIPE) + try: + out, _ = proc.communicate(timeout=1) + if 'Address already in use' in str(out): + print("Could not run redis-server (address already in use).") + print("Assuming that the process using the TCP port is another redis-server instance and going on.") + else: + raise Exception("Could not run redis-server (unhandled reason), aborting.") + except subprocess.TimeoutExpired: + print('redis-server has been running for 1 second.') + print('Assuming redis-server has started successfully and going on.') + + def on_finalize(): + print('Killing the spawned redis-server (if any)...') + proc.kill() + request.addfinalizer(on_finalize)