Skip to content

Commit cc1ecbb

Browse files
committed
Add redis to docker-compose and config, conftest
1 parent ec8a257 commit cc1ecbb

File tree

5 files changed

+38
-2
lines changed

5 files changed

+38
-2
lines changed

docker-compose.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ services:
77
dockerfile: Dockerfile
88
depends_on:
99
- postgres
10+
- redis
1011
environment:
1112
- DB_HOST=postgres
1213
- DB_PASSWORD=abc123
@@ -27,3 +28,8 @@ services:
2728
ports:
2829
- "54321:5432"
2930

31+
redis:
32+
image: redis:alpine
33+
ports:
34+
- "63791:6379"
35+

mypy.ini

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,5 @@ ignore_missing_imports = True
1010
[mypy-sqlalchemy.*]
1111
ignore_missing_imports = True
1212

13+
[mypy-redis.*]
14+
ignore_missing_imports = True

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ pytest-icdiff
99
mypy
1010
requests
1111

12+
redis

src/allocation/config.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,8 @@ def get_api_url():
1414
port = 5005 if host == 'localhost' else 80
1515
return f"http://{host}:{port}"
1616

17+
def get_redis_host_and_port():
18+
host = os.environ.get('REDIS_HOST', 'localhost')
19+
port = 63791 if host == 'localhost' else 6379
20+
return dict(host=host, port=port)
21+

tests/conftest.py

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
# pylint: disable=redefined-outer-name
2+
import shutil
3+
import subprocess
24
import time
35
from pathlib import Path
46

57
import pytest
8+
import redis
69
import requests
7-
from requests.exceptions import ConnectionError
10+
from requests.exceptions import RequestException
11+
from redis.exceptions import RedisError
812
from sqlalchemy.exc import OperationalError
913
from sqlalchemy import create_engine
1014
from 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')
5670
def 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

Comments
 (0)