11# pylint: disable=redefined-outer-name
2+ import shutil
3+ import subprocess
24import time
35from pathlib import Path
46
57import pytest
8+ import redis
69import requests
7- from requests .exceptions import ConnectionError
10+ from requests .exceptions import RequestException
11+ from redis .exceptions import RedisError
812from sqlalchemy .exc import OperationalError
913from sqlalchemy import create_engine
1014from sqlalchemy .orm import sessionmaker , clear_mappers
@@ -46,11 +50,21 @@ def wait_for_webapp_to_come_up():
4650 while time .time () < deadline :
4751 try :
4852 return requests .get (url )
49- except ConnectionError :
53+ except RequestException :
5054 time .sleep (0.5 )
5155 pytest .fail ('API never came up' )
5256
5357
58+ def wait_for_redis_to_come_up ():
59+ deadline = time .time () + 5
60+ r = redis .Redis (** config .get_redis_host_and_port ())
61+ while time .time () < deadline :
62+ try :
63+ return r .ping ()
64+ except RedisError :
65+ time .sleep (0.5 )
66+ pytest .fail ('Redis never came up' )
67+
5468
5569@pytest .fixture (scope = 'session' )
5670def postgres_db ():
@@ -76,3 +90,11 @@ def restart_api():
7690 time .sleep (0.5 )
7791 wait_for_webapp_to_come_up ()
7892
93+ @pytest .fixture
94+ def restart_redis_pubsub ():
95+ wait_for_redis_to_come_up ()
96+ if not shutil .which ('docker-compose' ):
97+ print ('skipping restart, assumes running in container' )
98+ return
99+ subprocess .run (['docker-compose' , 'restart' , '-t' , '0' , 'redis_pubsub' ])
100+
0 commit comments