2121HELLO_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+
2431def 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:
599604def 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