From 57beeedbdff89e3d1fbe80d6445e4925c36205e4 Mon Sep 17 00:00:00 2001 From: Patrick Hilhorst Date: Wed, 27 Apr 2022 16:21:10 +0200 Subject: nixos/test-driver: allow multiple entry of polling condition --- .../test-driver/test_driver/polling_condition.py | 29 ++++++++++++++++------ 1 file changed, 22 insertions(+), 7 deletions(-) (limited to 'nixos') diff --git a/nixos/lib/test-driver/test_driver/polling_condition.py b/nixos/lib/test-driver/test_driver/polling_condition.py index 459845452fa1..02ca0a03ab3d 100644 --- a/nixos/lib/test-driver/test_driver/polling_condition.py +++ b/nixos/lib/test-driver/test_driver/polling_condition.py @@ -1,4 +1,5 @@ from typing import Callable, Optional +from math import isfinite import time from .logger import rootlog @@ -14,7 +15,7 @@ class PollingCondition: description: Optional[str] last_called: float - entered: bool + entry_count: int def __init__( self, @@ -34,14 +35,21 @@ class PollingCondition: self.description = str(description) self.last_called = float("-inf") - self.entered = False + self.entry_count = 0 - def check(self) -> bool: - if self.entered or not self.overdue: + def check(self, force: bool = False) -> bool: + if (self.entered or not self.overdue) and not force: return True with self, rootlog.nested(self.nested_message): - rootlog.info(f"Time since last: {time.monotonic() - self.last_called:.2f}s") + time_since_last = time.monotonic() - self.last_called + last_message = ( + f"Time since last: {time_since_last:.2f}s" + if isfinite(time_since_last) + else "(not called yet)" + ) + + rootlog.info(last_message) try: res = self.condition() # type: ignore except Exception: @@ -69,9 +77,16 @@ class PollingCondition: def overdue(self) -> bool: return self.last_called + self.seconds_interval < time.monotonic() + @property + def entered(self) -> bool: + # entry_count should never dip *below* zero + assert self.entry_count >= 0 + return self.entry_count > 0 + def __enter__(self) -> None: - self.entered = True + self.entry_count += 1 def __exit__(self, exc_type, exc_value, traceback) -> None: # type: ignore - self.entered = False + assert self.entered + self.entry_count -= 1 self.last_called = time.monotonic() -- cgit 1.4.1 From 1db0dcdfec9c927ce83011451c69c2ee5e78289f Mon Sep 17 00:00:00 2001 From: Patrick Hilhorst Date: Wed, 27 Apr 2022 16:23:33 +0200 Subject: nixos/test-driver: add wait_before_entry to polling_condition --- nixos/lib/test-driver/test_driver/driver.py | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'nixos') diff --git a/nixos/lib/test-driver/test_driver/driver.py b/nixos/lib/test-driver/test_driver/driver.py index e32f6810ca87..19b889759884 100644 --- a/nixos/lib/test-driver/test_driver/driver.py +++ b/nixos/lib/test-driver/test_driver/driver.py @@ -202,6 +202,7 @@ class Driver: *, seconds_interval: float = 2.0, description: Optional[str] = None, + wait_before_entry: bool = False, ) -> Union[Callable[[Callable], ContextManager], ContextManager]: driver = self @@ -212,8 +213,15 @@ class Driver: seconds_interval, description, ) + self.wait_before_entry = wait_before_entry def __enter__(self) -> None: + if self.wait_before_entry: + with rootlog.nested( + f"waiting before entering polling condition {self.condition.description}" + ): + retry(lambda x: self.condition.check(force=True)) + driver.polling_conditions.append(self.condition) def __exit__(self, a, b, c) -> None: # type: ignore -- cgit 1.4.1 From 2a3834b7eb93face07083a6d06b858af5ddca791 Mon Sep 17 00:00:00 2001 From: Patrick Hilhorst Date: Wed, 27 Apr 2022 16:24:01 +0200 Subject: nixosTests.vscodium: use new wait_before_entry --- nixos/tests/vscodium.nix | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'nixos') diff --git a/nixos/tests/vscodium.nix b/nixos/tests/vscodium.nix index 688ddfe07e3e..7ad0b797fe3b 100644 --- a/nixos/tests/vscodium.nix +++ b/nixos/tests/vscodium.nix @@ -33,7 +33,7 @@ let }; enableOCR = true; testScript = '' - @polling_condition + @polling_condition(wait_before_entry=True) def codium_running(): machine.succeed('pgrep -x codium') @@ -41,7 +41,6 @@ let start_all() machine.wait_for_unit('graphical.target') - machine.wait_until_succeeds('pgrep -x codium') with codium_running: # Wait until vscodium is visible. "File" is in the menu bar. -- cgit 1.4.1 From 976f00963fb3006616efa1d7794aa5e5ea6cb712 Mon Sep 17 00:00:00 2001 From: Patrick Hilhorst Date: Fri, 29 Apr 2022 15:36:03 +0200 Subject: nixos/test-driver: rewrite wait_before_entry to wait --- nixos/lib/test-driver/test_driver/driver.py | 22 ++++++++++++++-------- nixos/tests/vscodium.nix | 3 ++- 2 files changed, 16 insertions(+), 9 deletions(-) (limited to 'nixos') diff --git a/nixos/lib/test-driver/test_driver/driver.py b/nixos/lib/test-driver/test_driver/driver.py index 19b889759884..6542a2e2f693 100644 --- a/nixos/lib/test-driver/test_driver/driver.py +++ b/nixos/lib/test-driver/test_driver/driver.py @@ -202,7 +202,6 @@ class Driver: *, seconds_interval: float = 2.0, description: Optional[str] = None, - wait_before_entry: bool = False, ) -> Union[Callable[[Callable], ContextManager], ContextManager]: driver = self @@ -213,21 +212,28 @@ class Driver: seconds_interval, description, ) - self.wait_before_entry = wait_before_entry def __enter__(self) -> None: - if self.wait_before_entry: - with rootlog.nested( - f"waiting before entering polling condition {self.condition.description}" - ): - retry(lambda x: self.condition.check(force=True)) - driver.polling_conditions.append(self.condition) def __exit__(self, a, b, c) -> None: # type: ignore res = driver.polling_conditions.pop() assert res is self.condition + def wait(self, timeout: int = 900) -> None: + def condition(last: bool) -> bool: + if last: + rootlog.info(f"Last chance for {self.condition.description}") + ret = self.condition.check(force=True) + if not ret and not last: + rootlog.info( + f"({self.condition.description} failure not fatal yet)" + ) + return ret + + with rootlog.nested(f"waiting for {self.condition.description}"): + retry(condition, timeout=timeout) + if fun_ is None: return Poll else: diff --git a/nixos/tests/vscodium.nix b/nixos/tests/vscodium.nix index 7ad0b797fe3b..496c52b78387 100644 --- a/nixos/tests/vscodium.nix +++ b/nixos/tests/vscodium.nix @@ -33,7 +33,7 @@ let }; enableOCR = true; testScript = '' - @polling_condition(wait_before_entry=True) + @polling_condition def codium_running(): machine.succeed('pgrep -x codium') @@ -42,6 +42,7 @@ let machine.wait_for_unit('graphical.target') + codium_running.wait() with codium_running: # Wait until vscodium is visible. "File" is in the menu bar. machine.wait_for_text('Get Started') -- cgit 1.4.1