STAR
Cal STAR — firmware, DAQ server, and engine design
Loading...
Searching...
No Matches
test_layerx_capture.py
Go to the documentation of this file.
1"""Capturing a flight's prints must not capture, or lose, anyone else's (engine/layerx/capture.py).
2
3Two burns fly at once in the API. With ``contextlib.redirect_stdout`` the second restored the
4first's buffer and the server printed into it for ever; this is that scene.
5"""
6
7from __future__ import annotations
8
9import sys
10import threading
11import time
12
13from engine.layerx.capture import thread_stdout
14
15
17 got = {}
18 barrier = threading.Barrier(2)
19
20 def fly(name: str, hold: float) -> None:
21 with thread_stdout() as buf:
22 barrier.wait()
23 for _ in range(5):
24 print(f"[flight_sim] {name}")
25 time.sleep(hold)
26 got[name] = buf.getvalue()
27
28 a = threading.Thread(target=fly, args=("A", 0.01))
29 b = threading.Thread(target=fly, args=("B", 0.002))
30 a.start(); b.start()
31 print("main thread line") # not captured by either
32 a.join(); b.join()
33 assert got["A"].count("A") == 5 and "B" not in got["A"]
34 assert got["B"].count("B") == 5 and "A" not in got["B"]
35 # After both exit, this thread writes to the real stream again, not to a buffer left behind.
36 assert sys.stdout._target() is sys.stdout.real
37 with thread_stdout() as again:
38 print("x")
39 assert again.getvalue() == "x\n"
40 assert "main thread line" not in got["A"] + got["B"]
static void fly(world_t *w)
Definition flight_e2e.c:369
size_type count(const key_type &key) const
Definition json.hpp:19264
test_two_threads_capture_only_their_own_and_the_stream_comes_back()