-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconftest.py
More file actions
156 lines (124 loc) · 3.94 KB
/
conftest.py
File metadata and controls
156 lines (124 loc) · 3.94 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
import cbor2
import pickle
from contextlib import contextmanager, ExitStack
from typing import Generator, Callable, Any, Iterable
import multiprocessing
import subprocess
import uuid
import os
import psutil
import pytest
from dataclasses import dataclass
from pathlib import Path
import functools
from opsqueue.common import SerializationFormat, json_as_bytes
from opsqueue.consumer import Strategy
from tests.util import wait_for_server
# @pytest.hookimpl(tryfirst=True)
# def pytest_configure(config: pytest.Config) -> None:
# print("A")
# multiprocessing.set_start_method('forkserver')
PROJECT_ROOT = Path(__file__).parents[3]
@dataclass
class OpsqueueProcess:
port: int
process: psutil.Popen
@functools.cache
def opsqueue_bin_location() -> Path:
if os.environ.get("OPSQUEUE_VIA_NIX"):
deriv_path = (
subprocess.check_output(["just", "nix-build-bin"]).decode("utf-8").strip()
)
return Path(deriv_path) / "bin" / "opsqueue"
else:
subprocess.run(
["cargo", "build", "--quiet", "--bin", "opsqueue"],
cwd=PROJECT_ROOT,
check=True,
)
return PROJECT_ROOT / Path("target", "debug", "opsqueue")
@pytest.fixture
def opsqueue() -> Generator[OpsqueueProcess, None, None]:
with opsqueue_service() as opsqueue_process:
yield opsqueue_process
@contextmanager
def opsqueue_service(
*,
port: int = 0,
) -> Generator[OpsqueueProcess, None, None]:
temp_dbname = f"/tmp/opsqueue_tests-{uuid.uuid4()}.db"
command = [
str(opsqueue_bin_location()),
"--port",
str(port),
"--database-filename",
temp_dbname,
]
env = os.environ.copy() # We copy the env so e.g. RUST_LOG and other env vars are propagated from outside of the invocation of pytest
if env.get("RUST_LOG") is None:
env["RUST_LOG"] = "off"
with psutil.Popen(command, cwd=PROJECT_ROOT, env=env) as process:
_host, port = wait_for_server(process)
try:
wrapper = OpsqueueProcess(port=port, process=process)
yield wrapper
finally:
process.terminate()
def random_free_port() -> int:
import random
while True:
port = random.randrange(10_000, 60_000)
if not is_port_in_use(port):
return port
def is_port_in_use(port: int) -> bool:
import socket
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
return s.connect_ex(("localhost", port)) == 0
@contextmanager
def background_process(
function: Callable[..., None],
args: Iterable[Any] = (),
) -> Generator[multiprocessing.Process, None, None]:
proc = multiprocessing.Process(target=function, args=args)
try:
proc.daemon = True
proc.start()
yield proc
finally:
proc.terminate()
@contextmanager
def multiple_background_processes(
function: Callable[[int], None], count: int
) -> Generator[None, None, None]:
with ExitStack() as stack:
for p in range(count):
stack.enter_context(background_process(function, args=(p,)))
yield
basic_strategies = Strategy.Random(), Strategy.Newest(), Strategy.Oldest()
any_strategies = [
*basic_strategies,
*(Strategy.PreferDistinct(meta_key="id", underlying=s) for s in basic_strategies),
]
@pytest.fixture(
scope="function",
ids=lambda s: f"Strategy.{s}",
params=basic_strategies,
)
def basic_consumer_strategy(
request: pytest.FixtureRequest,
) -> Generator[Strategy, None, None]:
yield request.param
@pytest.fixture(
scope="function",
ids=lambda s: f"Strategy.{s}",
params=any_strategies,
)
def any_consumer_strategy(
request: pytest.FixtureRequest,
) -> Generator[Strategy, None, None]:
yield request.param
@pytest.fixture(scope="function", params=[json_as_bytes, cbor2, pickle])
def serialization_format(
request: pytest.FixtureRequest,
) -> Generator[SerializationFormat, None, None]:
yield request.param