1818
1919pytestmark = pytest .mark .skipif (sys .platform == "win32" , reason = "POSIX job control" )
2020
21+ # Slow builders, such as emulated or heavily loaded ones, can take longer than these tests allow. Set
22+ # CMD2_TEST_TIMEOUT_SCALE to multiply how long a test waits before giving up. Pauses that check nothing happens are not scaled.
23+ TIMEOUT_SCALE = float (os .environ .get ("CMD2_TEST_TIMEOUT_SCALE" , "1" ))
24+
25+
26+ def timeout (seconds : float ) -> float :
27+ """Scale a limit on how long a test waits for something to happen."""
28+ return seconds * TIMEOUT_SCALE
29+
2130
2231def describe_processes (root : int , master : int ) -> str :
2332 """Report the terminal's foreground group and every descendant of root, for a timeout.
@@ -156,7 +165,7 @@ def test_pipeline_stops_with_cmd2_and_returns_terminal(
156165 " os.write(2, b'BUSY_READY\\ n')\n "
157166 " self.stdout.write('x' * 262144)\n "
158167 " self.stdout.flush()\n "
159- " time.sleep(30 )\n "
168+ f " time.sleep({ timeout ( 30 ) } )\n "
160169 " def do_ask(self, statement):\n "
161170 " self.poutput(self.read_input('INPUT> '))\n "
162171 " def do_direct(self, statement):\n "
@@ -233,7 +242,7 @@ def stop_background_output():
233242
234243 def wait_until (predicate ):
235244 nonlocal transcript
236- deadline = time .monotonic () + 10
245+ deadline = time .monotonic () + timeout ( 10 )
237246 while time .monotonic () < deadline :
238247 if select .select ([master ], [], [], 0.05 )[0 ]:
239248 data = decoder .decode (os .read (master , 65536 ))
@@ -307,7 +316,7 @@ def running(*pids: int) -> bool:
307316 "os.write(2, b'BUSY_READY\\ n')\n "
308317 "sys.stdout.write('x' * 262144)\n "
309318 "sys.stdout.flush()\n "
310- "time.sleep(30 )\n " ,
319+ f "time.sleep({ timeout ( 30 ) } )\n " ,
311320 encoding = "utf-8" ,
312321 )
313322 command = f"shell { shlex .quote (sys .executable )} { shlex .quote (str (busy_script ))} "
@@ -441,7 +450,7 @@ def running(*pids: int) -> bool:
441450 # terminal teardown blocked.
442451 os .close (master )
443452 process .kill ()
444- process .wait (timeout = 5 )
453+ process .wait (timeout = timeout ( 5 ) )
445454
446455
447456def test_pipeline_from_worker_thread_stays_isolated (tmp_path ) -> None :
@@ -486,7 +495,7 @@ def test_pipeline_from_worker_thread_stays_isolated(tmp_path) -> None:
486495
487496 def wait_until (predicate ):
488497 nonlocal transcript
489- deadline = time .monotonic () + 10
498+ deadline = time .monotonic () + timeout ( 10 )
490499 while time .monotonic () < deadline :
491500 if select .select ([master ], [], [], 0.05 )[0 ]:
492501 transcript += decoder .decode (os .read (master , 65536 ))
@@ -504,7 +513,7 @@ def wait_until(predicate):
504513 finally :
505514 os .close (master )
506515 process .kill ()
507- process .wait (timeout = 5 )
516+ process .wait (timeout = timeout ( 5 ) )
508517
509518
510519@pytest .mark .parametrize ("parent_delay" , [False , True ])
@@ -556,7 +565,7 @@ def test_pipeline_pager_can_set_terminal_modes_at_startup(tmp_path, parent_delay
556565 "def held_startup_wait(reader, timeout=None):\n "
557566 " utils.ProcReader._wait_for_exit = startup_wait\n "
558567 f" outcome = pathlib.Path({ str (outcome )!r} )\n "
559- " deadline = time.monotonic() + 5 \n "
568+ f " deadline = time.monotonic() + { timeout ( 5 ) } \n "
560569 " while time.monotonic() < deadline and not (outcome.exists() and outcome.read_text()):\n "
561570 " time.sleep(0.01)\n "
562571 " return startup_wait(reader, timeout)\n "
@@ -588,7 +597,7 @@ def test_pipeline_pager_can_set_terminal_modes_at_startup(tmp_path, parent_delay
588597
589598 def wait_until (predicate ):
590599 nonlocal transcript
591- deadline = time .monotonic () + 10
600+ deadline = time .monotonic () + timeout ( 10 )
592601 while time .monotonic () < deadline :
593602 if select .select ([master ], [], [], 0.05 )[0 ]:
594603 data = decoder .decode (os .read (master , 65536 ))
@@ -618,7 +627,7 @@ def wait_until(predicate):
618627 finally :
619628 os .close (master )
620629 process .kill ()
621- process .wait (timeout = 5 )
630+ process .wait (timeout = timeout ( 5 ) )
622631
623632
624633@pytest .mark .parametrize ("producer" , ["command" , "shell" ])
@@ -671,7 +680,7 @@ def test_pipeline_children_inherit_an_ordinary_signal_mask(tmp_path, producer) -
671680
672681 def wait_until (predicate ):
673682 nonlocal transcript
674- deadline = time .monotonic () + 10
683+ deadline = time .monotonic () + timeout ( 10 )
675684 while time .monotonic () < deadline :
676685 if select .select ([master ], [], [], 0.05 )[0 ]:
677686 data = decoder .decode (os .read (master , 65536 ))
@@ -697,7 +706,7 @@ def wait_until(predicate):
697706 finally :
698707 os .close (master )
699708 process .kill ()
700- process .wait (timeout = 5 )
709+ process .wait (timeout = timeout ( 5 ) )
701710
702711
703712@pytest .mark .parametrize ("suspend" , [False , True ])
@@ -749,7 +758,7 @@ def test_shell_producer_keeps_the_terminal_after_its_consumer_exits(tmp_path, su
749758
750759 def wait_until (predicate ):
751760 nonlocal transcript
752- deadline = time .monotonic () + 10
761+ deadline = time .monotonic () + timeout ( 10 )
753762 while time .monotonic () < deadline :
754763 if select .select ([master ], [], [], 0.05 )[0 ]:
755764 data = decoder .decode (os .read (master , 65536 ))
@@ -787,7 +796,7 @@ def wait_until(predicate):
787796 finally :
788797 os .close (master )
789798 process .kill ()
790- process .wait (timeout = 5 )
799+ process .wait (timeout = timeout ( 5 ) )
791800
792801
793802@pytest .mark .parametrize ("producer" , ["subprocess" , "script_shell" , "nested_pipe" ])
@@ -851,7 +860,7 @@ def test_pager_gets_the_terminal_for_output_written_to_the_descriptor(tmp_path,
851860
852861 def wait_until (predicate ):
853862 nonlocal transcript
854- deadline = time .monotonic () + 10
863+ deadline = time .monotonic () + timeout ( 10 )
855864 while time .monotonic () < deadline :
856865 if select .select ([master ], [], [], 0.05 )[0 ]:
857866 data = decoder .decode (os .read (master , 65536 ))
@@ -882,7 +891,7 @@ def wait_until(predicate):
882891 finally :
883892 os .close (master )
884893 process .kill ()
885- process .wait (timeout = 5 )
894+ process .wait (timeout = timeout ( 5 ) )
886895
887896
888897def test_ctrl_z_between_pipe_writes_stops_the_whole_pipeline (tmp_path ) -> None :
@@ -932,7 +941,7 @@ def test_ctrl_z_between_pipe_writes_stops_the_whole_pipeline(tmp_path) -> None:
932941
933942 def wait_until (predicate ):
934943 nonlocal transcript
935- deadline = time .monotonic () + 10
944+ deadline = time .monotonic () + timeout ( 10 )
936945 while time .monotonic () < deadline :
937946 if select .select ([master ], [], [], 0.05 )[0 ]:
938947 data = decoder .decode (os .read (master , 65536 ))
@@ -975,7 +984,7 @@ def tick_count() -> int:
975984 finally :
976985 os .close (master )
977986 process .kill ()
978- process .wait (timeout = 5 )
987+ process .wait (timeout = timeout ( 5 ) )
979988
980989
981990def test_pipeline_continued_in_the_background_leaves_the_terminal_to_the_shell (tmp_path ) -> None :
@@ -1032,7 +1041,7 @@ def test_pipeline_continued_in_the_background_leaves_the_terminal_to_the_shell(t
10321041
10331042 def wait_until (predicate ):
10341043 nonlocal transcript
1035- deadline = time .monotonic () + 10
1044+ deadline = time .monotonic () + timeout ( 10 )
10361045 while time .monotonic () < deadline :
10371046 if select .select ([master ], [], [], 0.05 )[0 ]:
10381047 data = decoder .decode (os .read (master , 65536 ))
@@ -1080,7 +1089,7 @@ def pause(seconds: float) -> None:
10801089 finally :
10811090 os .close (master )
10821091 process .kill ()
1083- process .wait (timeout = 5 )
1092+ process .wait (timeout = timeout ( 5 ) )
10841093
10851094
10861095def test_pipeline_when_the_application_ignores_sigchld (tmp_path ) -> None :
@@ -1122,7 +1131,7 @@ def test_pipeline_when_the_application_ignores_sigchld(tmp_path) -> None:
11221131
11231132 def wait_until (predicate ):
11241133 nonlocal transcript
1125- deadline = time .monotonic () + 10
1134+ deadline = time .monotonic () + timeout ( 10 )
11261135 while time .monotonic () < deadline :
11271136 if select .select ([master ], [], [], 0.05 )[0 ]:
11281137 data = decoder .decode (os .read (master , 65536 ))
@@ -1155,7 +1164,7 @@ def wait_until(predicate):
11551164 finally :
11561165 os .close (master )
11571166 process .kill ()
1158- process .wait (timeout = 5 )
1167+ process .wait (timeout = timeout ( 5 ) )
11591168
11601169
11611170def test_ctrl_c_ending_the_pager_does_not_interrupt_protected_code (tmp_path ) -> None :
@@ -1204,7 +1213,7 @@ def test_ctrl_c_ending_the_pager_does_not_interrupt_protected_code(tmp_path) ->
12041213
12051214 def wait_until (predicate ):
12061215 nonlocal transcript
1207- deadline = time .monotonic () + 10
1216+ deadline = time .monotonic () + timeout ( 10 )
12081217 while time .monotonic () < deadline :
12091218 if select .select ([master ], [], [], 0.05 )[0 ]:
12101219 data = decoder .decode (os .read (master , 65536 ))
@@ -1234,7 +1243,7 @@ def wait_until(predicate):
12341243 finally :
12351244 os .close (master )
12361245 process .kill ()
1237- process .wait (timeout = 5 )
1246+ process .wait (timeout = timeout ( 5 ) )
12381247
12391248
12401249@pytest .mark .parametrize ("launcher" , ["plain" , "exec" ])
@@ -1284,7 +1293,7 @@ def test_ctrl_z_with_a_shell_producer_in_the_pipeline(tmp_path, launcher, consum
12841293
12851294 def wait_until (predicate ):
12861295 nonlocal transcript
1287- deadline = time .monotonic () + 10
1296+ deadline = time .monotonic () + timeout ( 10 )
12881297 while time .monotonic () < deadline :
12891298 if select .select ([master ], [], [], 0.05 )[0 ]:
12901299 data = decoder .decode (os .read (master , 65536 ))
@@ -1321,7 +1330,7 @@ def wait_until(predicate):
13211330 finally :
13221331 os .close (master )
13231332 process .kill ()
1324- process .wait (timeout = 5 )
1333+ process .wait (timeout = timeout ( 5 ) )
13251334
13261335
13271336@pytest .mark .parametrize ("launcher" , ["plain" , "exec" ])
@@ -1383,7 +1392,7 @@ def test_command_reads_the_terminal_after_a_subprocess_wrote_to_a_slow_pipe(tmp_
13831392
13841393 def wait_until (predicate ):
13851394 nonlocal transcript
1386- deadline = time .monotonic () + 15
1395+ deadline = time .monotonic () + timeout ( 15 )
13871396 while time .monotonic () < deadline :
13881397 if select .select ([master ], [], [], 0.05 )[0 ]:
13891398 data = decoder .decode (os .read (master , 65536 ))
@@ -1420,4 +1429,4 @@ def wait_until(predicate):
14201429 os .kill (int (pid_file .read_text ()), signal .SIGKILL )
14211430 os .close (master )
14221431 process .kill ()
1423- process .wait (timeout = 5 )
1432+ process .wait (timeout = timeout ( 5 ) )
0 commit comments