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
#!/usr/bin/env python3
"""Offline process receipt for Jetstream V2 log level and format semantics."""
from __future__ import annotations
import json
import signal
import socket
import subprocess
import tempfile
import time
import urllib.request
BIN = "./zig-out/bin/stream"
def unused_port() -> int:
sock = socket.socket()
sock.bind(("127.0.0.1", 0))
port = sock.getsockname()[1]
sock.close()
return port
def start(root: str, port: int, prefix: list[str]) -> subprocess.Popen[str]:
return subprocess.Popen(
[
BIN,
*prefix,
f"--addr=127.0.0.1:{port}",
f"--data-dir={root}",
"--upstream=ws://127.0.0.1:17990",
"--relay-http=http://127.0.0.1:17990",
"--plc-url=http://127.0.0.1:17990",
"--compaction-interval=0",
"--retry-interval=0",
"--no-verify",
],
stdout=subprocess.DEVNULL,
stderr=subprocess.PIPE,
text=True,
)
def wait_ready(process: subprocess.Popen[str], port: int) -> None:
# listener-first startup: the listener answers from bind time, so wait
# for /status to leave "starting" (storage published, pipeline starting)
deadline = time.monotonic() + 10
while time.monotonic() < deadline:
if process.poll() is not None:
stderr = process.stderr.read() if process.stderr else ""
raise AssertionError(f"stream exited with {process.returncode}:\n{stderr}")
try:
body = urllib.request.urlopen(f"http://127.0.0.1:{port}/status", timeout=0.2).read()
if b"state starting" not in body:
return
time.sleep(0.025)
except OSError:
time.sleep(0.025)
raise AssertionError(f"listener {port} did not start")
def stop(process: subprocess.Popen[str]) -> list[str]:
process.send_signal(signal.SIGTERM)
process.wait(timeout=5)
assert process.returncode == 0, process.stderr.read() if process.stderr else ""
return [line for line in process.stderr.read().splitlines() if line]
def default_json_case(root: str) -> None:
port = unused_port()
process = start(root, port, ["serve"])
wait_ready(process, port)
records = [json.loads(line) for line in stop(process)]
assert records
assert all(set(("time", "level", "msg", "component")) <= record.keys() for record in records)
assert any(record["level"] == "INFO" for record in records)
assert not any(record["level"] == "DEBUG" for record in records)
def warn_json_case(root: str) -> None:
port = unused_port()
process = start(root, port, ["serve", "--log-level=warning"])
wait_ready(process, port)
# the case needs at least one failed upstream dial (the ERROR record)
# before shutdown; with listener-first startup the listener answers
# before the pipeline's first dial, so wait for the record itself
# instead of sleeping a fixed slice
import threading
lines: list[str] = []
got_error = threading.Event()
def pump() -> None:
assert process.stderr is not None
for line in process.stderr:
if line.strip():
lines.append(line)
try:
if json.loads(line)["level"] == "ERROR":
got_error.set()
except ValueError:
pass
reader = threading.Thread(target=pump, daemon=True)
reader.start()
assert got_error.wait(10), f"no ERROR record before deadline: {lines!r}"
process.send_signal(signal.SIGTERM)
process.wait(timeout=5)
assert process.returncode == 0
reader.join(timeout=5)
records = [json.loads(line) for line in lines]
assert records
assert any(record["level"] == "ERROR" for record in records)
assert all(record["level"] in ("WARN", "ERROR") for record in records)
def root_flags_text_debug_case(root: str) -> None:
port = unused_port()
process = start(
root,
port,
["--log-level=DBG", "--log-format=text", "serve"],
)
wait_ready(process, port)
lines = stop(process)
assert lines
assert all(line.startswith("time=") for line in lines)
assert any(" level=DEBUG " in line for line in lines)
assert any('msg="logging configured: level=DBG format=text"' in line for line in lines)
def invalid_config_case() -> None:
for flag, expected in (
("--log-level=loud", "InvalidLogLevel"),
("--log-format=xml", "InvalidLogFormat"),
):
result = subprocess.run(
[BIN, "serve", flag],
stdout=subprocess.DEVNULL,
stderr=subprocess.PIPE,
text=True,
timeout=3,
check=False,
)
assert result.returncode != 0
assert expected in result.stderr, result.stderr
def persistent_command_flags_case() -> None:
for args in (
["--log-level", "debug", "--log-format", "text", "version"],
["version", "--log-level=debug", "--log-format=text"],
):
result = subprocess.run(
[BIN, *args],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
timeout=3,
check=False,
)
assert result.returncode == 0, result.stderr
assert result.stdout.startswith("jetstream version "), result.stdout
def main() -> None:
with tempfile.TemporaryDirectory() as root:
default_json_case(f"{root}/default")
warn_json_case(f"{root}/warn")
root_flags_text_debug_case(f"{root}/debug")
persistent_command_flags_case()
invalid_config_case()
print("logging contract: json-default warn-filter text-debug persistent-flags invalid-config passed")
if __name__ == "__main__":
main()