Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

Synchronize pair replay test through subprocess pipes (#11421)

Trevor Manz37b9ba2db26e
diff --git a/tests/_cli/_pair_replay.py b/tests/_cli/_pair_replay.py
index f42f9d9c22..cccbb8e57a 100644
--- a/tests/_cli/_pair_replay.py
+++ b/tests/_cli/_pair_replay.py
@@ -7,7 +7,6 @@ import json
import socket
import sys
import threading
-import time
from pathlib import Path
from typing import TYPE_CHECKING
from unittest.mock import patch
@@ -39,7 +38,7 @@ class RecordingInbox:
def main() -> None:
directory = Path(sys.argv[1])
- config = msgspec.json.decode(sys.stdin.read(), type=ForwarderConfig)
+ config = msgspec.json.decode(sys.stdin.readline(), type=ForwarderConfig)
calls = 0
def controlled_events(
@@ -54,11 +53,8 @@ def main() -> None:
nonlocal calls
calls += 1
if calls == 2:
- (directory / "reconnecting.json").write_text(
- json.dumps({"cursor": cursor})
- )
- while not (directory / "resume").exists():
- time.sleep(0.01)
+ print(json.dumps({"cursor": cursor}), flush=True)
+ assert sys.stdin.readline() == "resume\n"
response = open_events(
url=url,
notebook=notebook,
@@ -70,8 +66,10 @@ def main() -> None:
if calls == 1:
def drop() -> None:
- while not (directory / "drop").exists():
- time.sleep(0.01)
+ command = sys.stdin.readline()
+ assert command == "drop\n", (
+ f"Expected drop command, got {command!r}"
+ )
response.socket.shutdown(socket.SHUT_RDWR)
threading.Thread(target=drop, daemon=True).start()
diff --git a/tests/_cli/test_pair_delivery_integration.py b/tests/_cli/test_pair_delivery_integration.py
index 95d60b39e5..7e7f79d309 100644
--- a/tests/_cli/test_pair_delivery_integration.py
+++ b/tests/_cli/test_pair_delivery_integration.py
@@ -252,9 +252,8 @@ def test_subprocess_reconnect_replays_messages_and_server_detach(
stderr=subprocess.PIPE,
)
assert process.stdin is not None
- process.stdin.write(msgspec.json.encode(config))
- process.stdin.close()
- process.stdin = None
+ process.stdin.write(msgspec.json.encode(config) + b"\n")
+ process.stdin.flush()
def attachment() -> str:
for event in events:
@@ -276,34 +275,44 @@ def test_subprocess_reconnect_replays_messages_and_server_detach(
with urllib.request.urlopen(request, timeout=5) as response:
assert response.status == 202
+ assert process.stdout is not None
+ output = process.stdout
+
+ def receive() -> bytes:
+ lines: queue.Queue[bytes] = queue.Queue(maxsize=1)
+ reader = threading.Thread(
+ target=lambda: lines.put(output.readline()), daemon=True
+ )
+ reader.start()
+ try:
+ line = lines.get(timeout=10)
+ except queue.Empty:
+ line = b""
+ if line:
+ return line
+ if process.poll() is None:
+ process.kill()
+ reader.join(timeout=5)
+ _, stderr = process.communicate(timeout=5)
+ raise AssertionError(
+ "Replay subprocess did not respond:\n"
+ + stderr.decode(errors="replace")
+ )
+
try:
- assert process.stdout is not None
- output = process.stdout
- startup: queue.Queue[bytes] = queue.Queue(maxsize=1)
- threading.Thread(
- target=lambda: startup.put(output.readline()), daemon=True
- ).start()
- assert startup.get(timeout=10) in (b"attached\n", b"attached\r\n")
+ assert receive() in (b"attached\n", b"attached\r\n")
first_id = attachment()
send("before drop")
wait_messages(tmp_path / "inbox.jsonl", 1)
- (tmp_path / "drop").touch()
- deadline = time.monotonic() + 10
- while not (tmp_path / "reconnecting.json").exists():
- if process.poll() is not None:
- _, stderr = process.communicate(timeout=5)
- raise AssertionError(stderr.decode())
- if time.monotonic() >= deadline:
- raise AssertionError("Forwarder did not reconnect")
- time.sleep(0.01)
- cursor = json.loads((tmp_path / "reconnecting.json").read_text())[
- "cursor"
- ]
+ process.stdin.write(b"drop\n")
+ process.stdin.flush()
+ cursor = json.loads(receive())["cursor"]
assert isinstance(cursor, str)
assert cursor
# The observer keeps the producer guard satisfied during the gap.
send("during gap")
- (tmp_path / "resume").touch()
+ process.stdin.write(b"resume\n")
+ process.stdin.flush()
second_id = attachment()
assert second_id != first_id
wait_messages(tmp_path / "inbox.jsonl", 2)