"""A wait must see a condition met before its deadline, however late it runs.""" from pathlib import Path from types import SimpleNamespace import sys import unittest from unittest.mock import patch sys.path.insert(0, str(Path(__file__).resolve().parents[2] / "e2e")) import harness # noqa: E402 class WaitUntilTests(unittest.TestCase): def test_a_condition_met_while_the_waiter_was_starved_is_seen(self): # The waiter checks once, then is not scheduled again until past its # deadline; the condition came true meanwhile. now = [0.0] def starved(_seconds): now[0] += 11 clock = SimpleNamespace(monotonic=lambda: now[0], sleep=starved) with patch.object(harness, "time", clock): harness.wait_until(lambda: now[0] > 0, 10, "never seen") def test_a_condition_never_met_fails_at_the_deadline(self): now = [0.0] def sleep(seconds): now[0] += seconds clock = SimpleNamespace(monotonic=lambda: now[0], sleep=sleep) with patch.object(harness, "time", clock): with self.assertRaisesRegex(AssertionError, "never met"): harness.wait_until(lambda: False, 1, "never met") self.assertLess(now[0], 1.2) if __name__ == "__main__": unittest.main()