Skip to content

Commit 75c32e7

Browse files
authored
Size relay tests from pipe capacity (#1825)
Co-authored-by: sogav21 <223452947+sogav21@users.noreply.github.com>
1 parent e5811a3 commit 75c32e7

2 files changed

Lines changed: 18 additions & 9 deletions

File tree

‎cmd2/utils.py‎

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

12171217

1218+
_RELAY_CHUNK_SIZE = 65536
1219+
1220+
12181221
class _DescriptorRelay:
12191222
"""Carry output that subprocesses write to a terminal pipeline's descriptor.
12201223
@@ -1304,7 +1307,7 @@ def _relay(self) -> None:
13041307
# yet counted would look passed on to idle() and flush(), and cmd2's next write could overtake it.
13051308
incoming.poll()
13061309
with self._lock:
1307-
data = os.read(self._in_fd, 65536)
1310+
data = os.read(self._in_fd, _RELAY_CHUNK_SIZE)
13081311
self._received += len(data)
13091312
if not data:
13101313
return

‎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)