diff --git a/tests/terminal/test_net_launch_waits.py b/tests/terminal/test_net_launch_waits.py index e6dff6c1fc..3085eb7162 100644 --- a/tests/terminal/test_net_launch_waits.py +++ b/tests/terminal/test_net_launch_waits.py @@ -16,7 +16,7 @@ from strata_cli.launch import cmd from strata_cli.launch.runner import Event, LaunchError, Runner, capital from strata_control import netpolicy, progress_words from tests.terminal.test_flow2_022 import Base as FlowBase -from tests.terminal.test_netpolicy import BlackHole, Server, ok_body, refused_port +from tests.terminal.test_netpolicy import AT_ONCE_S, REFUSED_WAIT_LINE_S, REFUSED_WINDOW, BlackHole, Server, ok_body, refused_port def scaled(): @@ -94,9 +94,10 @@ class SetupDownloadWaits(unittest.TestCase): def test_a_refusing_port_fails_at_once_with_no_waiting_line(self): port = refused_port() - code, out, took = self.download(f"http://127.0.0.1:{port}/m/model-00001-of-00002.gguf") + with mock.patch.object(netpolicy, "CONNECT_LIMITS", REFUSED_WINDOW), mock.patch.object(netpolicy, "WAIT_LINE_AFTER_S", REFUSED_WAIT_LINE_S): + code, out, took = self.download(f"http://127.0.0.1:{port}/m/model-00001-of-00002.gguf") self.assertEqual(code, 1) - self.assertLess(took, 0.5) + self.assertLess(took, AT_ONCE_S) self.assertNotIn("Waiting for", out) self.assertIn("[X] This computer could not connect to 127.0.0.1 (it looks offline), so the model files could not be downloaded.", out) @@ -143,7 +144,9 @@ class LaunchShowsTheWait(FlowBase): texts = [x for _t, x in seen] self.assertIn("Waiting for huggingface.co to answer (5 s so far)", texts) self.assertIn("Waiting for huggingface.co to answer (15 s so far)", texts) - self.assertLess(seen[0][0], 0.2) # the step's own sentence at once, before setup says anything + # the step's own sentence comes first, before setup has said anything (an order, not a stopwatch: a slow or loaded machine only moves both) + self.assertFalse(seen[0][1].startswith("Waiting for"), seen[0]) + self.assertLess(seen[0][0], next(t for t, x in seen if x.startswith("Waiting for"))) self.assertIn("The model could not be installed. huggingface.co did not answer within 44 seconds, so the model files could not " "be downloaded.", err.text) self.assertIn("Check that this computer can open https://huggingface.co", err.next) diff --git a/tests/terminal/test_net_transfer.py b/tests/terminal/test_net_transfer.py index 5a7b0e6c10..3e5ec53c19 100644 --- a/tests/terminal/test_net_transfer.py +++ b/tests/terminal/test_net_transfer.py @@ -16,7 +16,7 @@ from pathlib import Path from unittest import mock from strata_control import feed, netpolicy, transfer -from tests.terminal.test_netpolicy import BlackHole, Server, refused_port +from tests.terminal.test_netpolicy import AT_ONCE_S, REFUSED_WINDOW, BlackHole, Server, refused_port BODY = bytes(range(256)) * 40 # 10,240 bytes SHA = hashlib.sha256(BODY).hexdigest() @@ -94,9 +94,9 @@ class TransferRule(Scaled): def test_a_refused_port_fails_at_once(self): t0 = time.monotonic() - with self.assertRaises(transfer.Unreachable) as cm: + with mock.patch.object(netpolicy, "CONNECT_LIMITS", REFUSED_WINDOW), self.assertRaises(transfer.Unreachable) as cm: self.fetch(f"http://127.0.0.1:{refused_port()}/e.tar.gz") - self.assertLess(time.monotonic() - t0, 0.5) + self.assertLess(time.monotonic() - t0, AT_ONCE_S) self.assertEqual(cm.exception.failure.kind, "offline") def test_a_working_download(self): @@ -183,7 +183,7 @@ class FeedRule(Scaled): t0 = time.monotonic() with self.assertRaises(feed.FeedError) as cm: feed.fetch(f"http://127.0.0.1:{refused_port()}/strata-feed.json", {"k": b"x" * 32}, timeout=30) - self.assertLess(time.monotonic() - t0, 0.5) # no wait and no retry for an offline user (owner 11-01) + self.assertLess(time.monotonic() - t0, AT_ONCE_S) # no wait and no retry for an offline user (owner 11-01) self.assertEqual(str(cm.exception), "this computer could not connect to 127.0.0.1 (it looks offline), so the update check could not run. " "Strata keeps working with what is installed.") diff --git a/tests/terminal/test_netpolicy.py b/tests/terminal/test_netpolicy.py index 963faf45da..282637c82b 100644 --- a/tests/terminal/test_netpolicy.py +++ b/tests/terminal/test_netpolicy.py @@ -44,6 +44,14 @@ class BlackHole: self.sock.close() +#: "fails at once" for a refused connection. Windows itself reports a refused loopback port only after 2.03-2.04 s (measured on the owner PC: it tries the +#: connection again first), longer than the scaled test windows, so there the refused-port tests use windows of 3 / 6 s, a waiting-line delay of 3 s and +#: a limit of 4 s; on Linux and macOS the refusal is immediate (0.4 / 0.8 s windows, 0.5 s). Either way far under the rule's 12 s / 44 s windows. +AT_ONCE_S = 4.0 if os.name == "nt" else 0.5 +REFUSED_WINDOW = (3.0, 6.0) if os.name == "nt" else (0.4, 0.8) +REFUSED_WAIT_LINE_S = 3.0 if os.name == "nt" else 0.5 + + def refused_port() -> int: s = socket.socket() s.bind(("127.0.0.1", 0)) @@ -242,8 +250,8 @@ class ClearFailuresAreImmediate(Base): port = refused_port() t0 = time.monotonic() with self.assertRaises(N.NetFailure) as cm: - self.open(f"http://127.0.0.1:{port}/f", wait_after=0.5) - self.assertLess(time.monotonic() - t0, 0.5) + self.open(f"http://127.0.0.1:{port}/f", limits=REFUSED_WINDOW, wait_after=REFUSED_WAIT_LINE_S) + self.assertLess(time.monotonic() - t0, AT_ONCE_S) self.assertEqual(cm.exception.kind, "offline") self.assertFalse(self.memory.failed_recently("127.0.0.1")) self.assertIn("This computer could not connect to 127.0.0.1 (it looks offline)", cm.exception.headline("the model files"))