-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathconftest.py
More file actions
183 lines (144 loc) · 5.33 KB
/
conftest.py
File metadata and controls
183 lines (144 loc) · 5.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import fcntl
import logging
import os
import socket
import time
from contextlib import contextmanager
from datetime import datetime, timedelta, timezone
from functools import partial
from typing import AsyncGenerator, Callable, Generator, Iterable, cast
from unittest.mock import AsyncMock
from uuid import uuid4
import pytest
import redis.exceptions
from docker import DockerClient
from docker.models.containers import Container
from redis import ConnectionPool, Redis
from docket import Docket, Worker
REDIS_VERSION = os.environ.get("REDIS_VERSION", "7.4")
@pytest.fixture(autouse=True)
def log_level(caplog: pytest.LogCaptureFixture) -> Generator[None, None, None]:
with caplog.at_level(logging.DEBUG):
yield
@pytest.fixture
def now() -> Callable[[], datetime]:
return partial(datetime.now, timezone.utc)
@contextmanager
def _sync_redis(url: str) -> Generator[Redis, None, None]:
pool: ConnectionPool | None = None
redis = Redis.from_url(url) # type: ignore
try:
with redis:
pool = redis.connection_pool # type: ignore
yield redis
finally:
if pool: # pragma: no branch
pool.disconnect()
@contextmanager
def _adminitrative_redis(port: int) -> Generator[Redis, None, None]:
with _sync_redis(f"redis://localhost:{port}/15") as r:
yield r
def _wait_for_redis(port: int) -> None:
while True:
try:
with _adminitrative_redis(port) as r:
success = r.ping() # type: ignore
if success: # pragma: no branch
return
except redis.exceptions.ConnectionError: # pragma: no cover
time.sleep(0.1)
@pytest.fixture(scope="session")
def redis_server(testrun_uid: str, worker_id: str) -> Generator[Container, None, None]:
client = DockerClient.from_env()
container: Container | None = None
lock_file_name = f"/tmp/docket-unit-tests-{testrun_uid}-startup"
with open(lock_file_name, "w+") as lock_file:
fcntl.flock(lock_file, fcntl.LOCK_EX)
containers: Iterable[Container] = cast(
Iterable[Container],
client.containers.list( # type: ignore
all=True,
filters={"label": "source=docket-unit-tests"},
),
)
for c in containers:
if c.labels.get("testrun_uid") == testrun_uid: # type: ignore
container = c
else:
c.remove(force=True) # pragma: no cover
if not container:
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind(("", 0))
redis_port = s.getsockname()[1]
# Determine Docker image based on version
if REDIS_VERSION.startswith("valkey-"):
version = REDIS_VERSION.replace("valkey-", "")
image = f"valkey/valkey:{version}"
else:
image = f"redis:{REDIS_VERSION}"
container = client.containers.run(
image,
detach=True,
ports={"6379/tcp": redis_port},
labels={
"source": "docket-unit-tests",
"testrun_uid": testrun_uid,
},
auto_remove=True,
)
_wait_for_redis(redis_port)
else:
port_bindings = container.attrs["HostConfig"]["PortBindings"]["6379/tcp"]
redis_port = int(port_bindings[0]["HostPort"])
with _adminitrative_redis(redis_port) as r:
r.sadd(f"docket-unit-tests:{testrun_uid}", worker_id)
try:
yield container
finally:
with _adminitrative_redis(redis_port) as r:
with r.pipeline() as pipe: # type: ignore
pipe.srem(f"docket-unit-tests:{testrun_uid}", worker_id)
pipe.scard(f"docket-unit-tests:{testrun_uid}")
count: int
_, count = pipe.execute() # type: ignore
if count == 0:
container.stop()
os.remove(lock_file_name)
@pytest.fixture
def redis_port(redis_server: Container) -> int:
port_bindings = redis_server.attrs["HostConfig"]["PortBindings"]["6379/tcp"]
return int(port_bindings[0]["HostPort"])
@pytest.fixture(scope="session")
def redis_db(worker_id: str) -> int:
if not worker_id or "gw" not in worker_id:
return 0 # pragma: no cover
else:
return 0 + int(worker_id.replace("gw", "")) # pragma: no cover
@pytest.fixture
def redis_url(redis_port: int, redis_db: int) -> str:
url = f"redis://localhost:{redis_port}/{redis_db}"
with _sync_redis(url) as r:
r.flushdb() # type: ignore
return url
@pytest.fixture
async def docket(redis_url: str, aiolib: str) -> AsyncGenerator[Docket, None]:
async with Docket(name=f"test-docket-{uuid4()}", url=redis_url) as docket:
yield docket
@pytest.fixture
async def worker(docket: Docket) -> AsyncGenerator[Worker, None]:
async with Worker(
docket,
minimum_check_interval=timedelta(milliseconds=5),
scheduling_resolution=timedelta(milliseconds=5),
) as worker:
yield worker
@pytest.fixture
def the_task() -> AsyncMock:
task = AsyncMock()
task.__name__ = "the_task"
return task
@pytest.fixture
def another_task() -> AsyncMock:
task = AsyncMock()
task.__name__ = "another_task"
return task