Skip to content

Commit 52dc4bf

Browse files
committed
Merge branch 'reserved_row_toolbar' into stage5-pager-investigation
2 parents 0228e7a + 6ae3cfb commit 52dc4bf

3 files changed

Lines changed: 27 additions & 12 deletions

File tree

‎cmd2/utils.py‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1215,6 +1215,11 @@ def _write_bytes(stream: StdSim | TextIO, to_write: bytes | str) -> None:
12151215
stream.buffer.write(to_write)
12161216

12171217

1218+
# How much a _DescriptorRelay reads from its pipe at once, which is also the most producer output it holds in memory. A relay
1219+
# test sizes its payload from this, so that its producer still blocks if the value changes.
1220+
_RELAY_CHUNK_SIZE = 65536
1221+
1222+
12181223
class _DescriptorRelay:
12191224
"""Carry output that subprocesses write to a terminal pipeline's descriptor.
12201225
@@ -1304,7 +1309,7 @@ def _relay(self) -> None:
13041309
# yet counted would look passed on to idle() and flush(), and cmd2's next write could overtake it.
13051310
incoming.poll()
13061311
with self._lock:
1307-
data = os.read(self._in_fd, 65536)
1312+
data = os.read(self._in_fd, _RELAY_CHUNK_SIZE)
13081313
self._received += len(data)
13091314
if not data:
13101315
return

‎tests/test_pipeline_job_control.py‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818

1919
pytestmark = pytest.mark.skipif(sys.platform == "win32", reason="POSIX job control")
2020

21+
# More output than a pipe and the relay's pipe hold, so a producer blocks until the consumer reads. Linux sizes a pipe
22+
# in pages, 16 by default, so follow the page size: 256 KiB with 4 KiB pages, 4 MiB with the 64 KiB pages of ppc64le.
23+
PIPE_FILL = 64 * (os.sysconf("SC_PAGE_SIZE") if hasattr(os, "sysconf") else 4096)
24+
2125

2226
def describe_processes(root: int, master: int) -> str:
2327
"""Report the terminal's foreground group and every descendant of root, for a timeout.
@@ -154,13 +158,13 @@ def test_pipeline_stops_with_cmd2_and_returns_terminal(
154158
"class App(Cmd):\n"
155159
" def do_busy(self, statement):\n"
156160
" os.write(2, b'BUSY_READY\\n')\n"
157-
" self.stdout.write('x' * 262144)\n"
161+
f" self.stdout.write('x' * {PIPE_FILL})\n"
158162
" self.stdout.flush()\n"
159163
" time.sleep(30)\n"
160164
" def do_ask(self, statement):\n"
161165
" self.poutput(self.read_input('INPUT> '))\n"
162166
" def do_direct(self, statement):\n"
163-
" self.stdout.buffer.write(b'x' * 262144)\n"
167+
f" self.stdout.buffer.write(b'x' * {PIPE_FILL})\n"
164168
" self.stdout.flush()\n"
165169
" assert self.select('first second', 'SELECT> ') == 'first'\n"
166170
" assert input('PLAIN> ') == 'answer'\n"
@@ -306,7 +310,7 @@ def running(*pids: int) -> bool:
306310
"import os, signal, sys, time\n"
307311
"signal.signal(signal.SIGINT, signal.SIG_DFL)\n"
308312
"os.write(2, b'BUSY_READY\\n')\n"
309-
"sys.stdout.write('x' * 262144)\n"
313+
f"sys.stdout.write('x' * {PIPE_FILL})\n"
310314
"sys.stdout.flush()\n"
311315
"time.sleep(30)\n",
312316
encoding="utf-8",

‎tests/test_utils.py‎

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@
2121
HELLO_WORLD = "Hello, world!"
2222

2323

24+
def _pipe_capacity(fd: int) -> int:
25+
"""Return the pipe capacity, falling back to the macOS/BSD maximum where it cannot be queried."""
26+
import fcntl
27+
28+
return fcntl.fcntl(fd, fcntl.F_GETPIPE_SZ) if hasattr(fcntl, "F_GETPIPE_SZ") else 65536
29+
30+
2431
def test_remove_duplicates_no_duplicates() -> None:
2532
no_dups = [5, 4, 3, 2, 1]
2633
assert cu.remove_duplicates(no_dups) == no_dups
@@ -480,18 +487,15 @@ def test_pipeline_writer_relay_lends_only_while_a_producer_waits(pauses) -> None
480487
waiting, command code gets the terminal back, whether or not the consumer has read all
481488
its output yet. That is as between cmd2's own writes.
482489
"""
483-
import fcntl
484490
import subprocess
485491
import threading
486492

487493
read_fd, write_fd = os.pipe()
488-
# Size the payload from the consumer's pipe. Linux reports its capacity, which follows the page size (16 pages by
489-
# default); macOS and the BSDs cap a pipe at 64 KiB.
490-
capacity = fcntl.fcntl(write_fd, fcntl.F_GETPIPE_SZ) if hasattr(fcntl, "F_GETPIPE_SZ") else 65536
494+
capacity = _pipe_capacity(write_fd)
491495
# More than the relay and both pipes hold, so the child blocks until the consumer reads. What remains after the consumer's
492496
# first read fits, so the child can then finish. It is also more than the consumer's pipe holds, so part of it is still
493497
# in the relay when cmd2 writes after it.
494-
payload = b"x" * (2 * capacity + 2 * 65536)
498+
payload = b"x" * (2 * capacity + 2 * cu._RELAY_CHUNK_SIZE)
495499
first_read = len(payload) - capacity - 16384 if pauses else len(payload)
496500
received = bytearray()
497501
lent = threading.Event()
@@ -559,8 +563,9 @@ def test_pipeline_writer_relay_lends_only_to_a_consumer_waiting_for_the_terminal
559563
import subprocess
560564
import threading
561565

562-
payload = b"x" * 262144
563566
read_fd, write_fd = os.pipe()
567+
capacity = _pipe_capacity(write_fd)
568+
payload = b"x" * (4 * capacity)
564569
received = bytearray()
565570
lends = []
566571

@@ -571,7 +576,7 @@ def lend_terminal():
571576

572577
def drain() -> None:
573578
# So much slower than the producer that the relay's pipe fills up and its writes stall
574-
while chunk := os.read(read_fd, 16384):
579+
while chunk := os.read(read_fd, capacity // 4):
575580
received.extend(chunk)
576581
time.sleep(0.15)
577582

@@ -599,8 +604,9 @@ def drain() -> None:
599604
def test_pipeline_writer_relay_leaves_the_terminal_after_a_producer_finishes() -> None:
600605
import subprocess
601606

602-
payload = b"x" * 70000
603607
read_fd, write_fd = os.pipe()
608+
capacity = _pipe_capacity(write_fd)
609+
payload = b"x" * (capacity + 4096)
604610
reader = mock.Mock(_lend_terminal=mock.Mock(side_effect=contextlib.nullcontext))
605611
writer = cu._PipelineWriter(write_fd, reader)
606612
try:

0 commit comments

Comments
 (0)