|
9 | 9 | import json |
10 | 10 | import logging |
11 | 11 | import re |
12 | | -import time |
13 | 12 | from collections.abc import Callable |
14 | 13 | from dataclasses import dataclass |
15 | 14 | from pathlib import Path |
|
26 | 25 | expected_service_running, |
27 | 26 | wait_for_service_running, |
28 | 27 | ) |
| 28 | +from tenacity import RetryError, retry, stop_after_delay, wait_fixed |
29 | 29 |
|
30 | 30 | _GET_NODE_OUTPUTS_REQUEST_PATTERN: Final[re.Pattern[str]] = re.compile( |
31 | 31 | r"/storage/locations/[^/]+/files" |
@@ -90,6 +90,17 @@ def __call__(self, message: str) -> bool: |
90 | 90 | return False |
91 | 91 |
|
92 | 92 |
|
| 93 | +@retry( |
| 94 | + stop=stop_after_delay(_JLAB_RUN_OPTIMIZATION_MAX_TIME / 1000), # seconds |
| 95 | + wait=wait_fixed(2), |
| 96 | + reraise=True, |
| 97 | +) |
| 98 | +def _wait_for_optimization_complete(run_button): |
| 99 | + bg_color = run_button.evaluate("el => getComputedStyle(el).backgroundColor") |
| 100 | + if bg_color != "rgb(0, 128, 0)": |
| 101 | + raise ValueError("Optimization not finished yet: {bg_color=}, {run_button=}") |
| 102 | + |
| 103 | + |
93 | 104 | def test_classic_ti_plan( # noqa: PLR0915 |
94 | 105 | page: Page, |
95 | 106 | log_in_and_out: RobustWebSocket, |
@@ -228,21 +239,12 @@ def test_classic_ti_plan( # noqa: PLR0915 |
228 | 239 | with log_context(logging.INFO, "Run optimization") as ctx: |
229 | 240 | run_button = ti_iframe.get_by_role("button", name="Run Optimization") |
230 | 241 | run_button.click(timeout=_JLAB_RUN_OPTIMIZATION_APPEARANCE_TIME) |
231 | | - start = time.time() |
232 | | - success = False |
233 | | - while ( |
234 | | - time.time() - start < _JLAB_RUN_OPTIMIZATION_MAX_TIME / 1000 |
235 | | - ): # Convert ms to seconds |
236 | | - bg_color = run_button.evaluate( |
237 | | - "el => getComputedStyle(el).backgroundColor" |
238 | | - ) |
239 | | - if bg_color == "rgb(0, 128, 0)": |
240 | | - ctx.logger.info("Optimization finished!") |
241 | | - success = True |
242 | | - break |
243 | | - time.sleep(2) |
244 | | - if not success: |
245 | | - ctx.logger.info("Optimization did not finish in time.") |
| 242 | + try: |
| 243 | + _wait_for_optimization_complete(run_button) |
| 244 | + ctx.logger.info("Optimization finished!") |
| 245 | + except RetryError as e: |
| 246 | + last_exc = e.last_attempt.exception() |
| 247 | + ctx.logger.warning(f"Optimization did not finish in time: {last_exc}") |
246 | 248 |
|
247 | 249 | with log_context(logging.INFO, "Create report"): |
248 | 250 | with log_context( |
|
0 commit comments