Skip to content

Commit 202f512

Browse files
committed
tenacity for RunOptimization
1 parent 92e204a commit 202f512

File tree

1 file changed

+18
-16
lines changed

1 file changed

+18
-16
lines changed

tests/e2e-playwright/tests/tip/test_ti_plan.py

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import json
1010
import logging
1111
import re
12-
import time
1312
from collections.abc import Callable
1413
from dataclasses import dataclass
1514
from pathlib import Path
@@ -26,6 +25,7 @@
2625
expected_service_running,
2726
wait_for_service_running,
2827
)
28+
from tenacity import RetryError, retry, stop_after_delay, wait_fixed
2929

3030
_GET_NODE_OUTPUTS_REQUEST_PATTERN: Final[re.Pattern[str]] = re.compile(
3131
r"/storage/locations/[^/]+/files"
@@ -90,6 +90,17 @@ def __call__(self, message: str) -> bool:
9090
return False
9191

9292

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+
93104
def test_classic_ti_plan( # noqa: PLR0915
94105
page: Page,
95106
log_in_and_out: RobustWebSocket,
@@ -228,21 +239,12 @@ def test_classic_ti_plan( # noqa: PLR0915
228239
with log_context(logging.INFO, "Run optimization") as ctx:
229240
run_button = ti_iframe.get_by_role("button", name="Run Optimization")
230241
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}")
246248

247249
with log_context(logging.INFO, "Create report"):
248250
with log_context(

0 commit comments

Comments
 (0)