From 6a6cd5a8b66e9a68ee3dcf00cba62f496326b50d Mon Sep 17 00:00:00 2001
From: Millian Poquet <millian.poquet@inria.fr>
Date: Mon, 27 May 2019 19:27:17 +0200
Subject: [PATCH] [test] run redis from pytest

---
 test/conftest.py | 24 +++++++++++++++++++++++-
 1 file changed, 23 insertions(+), 1 deletion(-)

diff --git a/test/conftest.py b/test/conftest.py
index a7e3527..7ffdfe8 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)
-- 
GitLab