Skip to content

Commit 4316cd1

Browse files
committed
Add redis to docker-compose and config, conftest
1 parent 3afd5d5 commit 4316cd1

File tree

5 files changed

+44
-3
lines changed

5 files changed

+44
-3
lines changed

docker-compose.yml

+6
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

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ ignore_missing_imports = False
33
mypy_path = ./src
44
check_untyped_defs = True
55

6-
[mypy-pytest.*,sqlalchemy.*]
6+
[mypy-pytest.*,sqlalchemy.*,redis.*]
77
ignore_missing_imports = True

requirements.txt

+2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22
sqlalchemy
33
flask
44
psycopg2-binary
5+
redis
56

67
# tests
78
pytest
89
pytest-icdiff
910
mypy
1011
requests
12+
tenacity

src/allocation/config.py

+6
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,9 @@ def get_api_url():
1313
host = os.environ.get("API_HOST", "localhost")
1414
port = 5005 if host == "localhost" else 80
1515
return f"http://{host}:{port}"
16+
17+
18+
def get_redis_host_and_port():
19+
host = os.environ.get("REDIS_HOST", "localhost")
20+
port = 63791 if host == "localhost" else 6379
21+
return dict(host=host, port=port)

tests/conftest.py

+29-2
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
@@ -48,11 +52,22 @@ def wait_for_webapp_to_come_up():
4852
while time.time() < deadline:
4953
try:
5054
return requests.get(url)
51-
except ConnectionError:
55+
except RequestException:
5256
time.sleep(0.5)
5357
pytest.fail("API never came up")
5458

5559

60+
def wait_for_redis_to_come_up():
61+
deadline = time.time() + 5
62+
r = redis.Redis(**config.get_redis_host_and_port())
63+
while time.time() < deadline:
64+
try:
65+
return r.ping()
66+
except RedisError:
67+
time.sleep(0.5)
68+
pytest.fail("Redis never came up")
69+
70+
5671
@pytest.fixture(scope="session")
5772
def postgres_db():
5873
engine = create_engine(config.get_postgres_uri())
@@ -78,3 +93,15 @@ def restart_api():
7893
(Path(__file__).parent / "../src/allocation/entrypoints/flask_app.py").touch()
7994
time.sleep(0.5)
8095
wait_for_webapp_to_come_up()
96+
97+
98+
@pytest.fixture
99+
def restart_redis_pubsub():
100+
wait_for_redis_to_come_up()
101+
if not shutil.which("docker-compose"):
102+
print("skipping restart, assumes running in container")
103+
return
104+
subprocess.run(
105+
["docker-compose", "restart", "-t", "0", "redis_pubsub"],
106+
check=True,
107+
)

0 commit comments

Comments
 (0)