Skip to content

Commit

Permalink
Change logic around calling post launch delay
Browse files Browse the repository at this point in the history
  • Loading branch information
tysmith committed May 27, 2024
1 parent 0816053 commit 0378e21
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 114 deletions.
69 changes: 33 additions & 36 deletions grizzly/common/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ def initial(self) -> bool:
"""
return self._tests_run < 2

def post_launch(self, delay: int = -1) -> None:
def post_launch(self, delay: int = 0) -> None:
"""Perform actions after launching browser before loading test cases.
Args:
Expand All @@ -257,41 +257,38 @@ def post_launch(self, delay: int = -1) -> None:
Returns:
None
"""
if delay >= 0 and not self.startup_failure:
with TestCase("post_launch_delay.html", "None") as content:
content.add_from_file(
Path(__file__).parent / "post_launch_delay.html",
file_name=content.entry_point,
copy=True,
)
srv_map = ServerMap()
srv_map.set_redirect("grz_start", content.entry_point, required=False)
srv_map.set_redirect("grz_continue", "grz_start", required=True)
# temporarily override server timeout
org_timeout = self._server.timeout
# add time buffer to redirect delay
# in practice this should take a few seconds (~10s)
# in extreme cases ~40s (slow build + debugger)
self._server.timeout = delay + 180
if delay > 0:
LOG.info("Browser launched, continuing in %ds...", delay)
# serve prompt page
server_status, _ = self._server.serve_path(
content.root,
continue_cb=self._target.monitor.is_healthy,
server_map=srv_map,
)
# restore server timeout
self._server.timeout = org_timeout
if server_status != Served.ALL:
self.startup_failure = True
if server_status == Served.TIMEOUT:
# this should never happen with a correctly functioning build
LOG.warning("Target hung after launch")

if self.startup_failure:
# TODO: we need a better way to handle delayed startup failures
LOG.warning("Post launch check failed!")
assert delay >= 0
with TestCase("post_launch_delay.html", "None") as content:
content.add_from_file(
Path(__file__).parent / "post_launch_delay.html",
file_name=content.entry_point,
copy=True,
)
srv_map = ServerMap()
srv_map.set_redirect("grz_start", content.entry_point, required=False)
srv_map.set_redirect("grz_continue", "grz_start", required=True)
# temporarily override server timeout
org_timeout = self._server.timeout
# add time buffer to redirect delay
# in practice this should take a few seconds (~10s)
# in extreme cases ~40s (slow build + debugger)
self._server.timeout = delay + 180
if delay > 0:
LOG.info("Browser launched, continuing in %ds...", delay)
# serve prompt page
server_status, _ = self._server.serve_path(
content.root,
continue_cb=self._target.monitor.is_healthy,
server_map=srv_map,
)
# restore server timeout
self._server.timeout = org_timeout
if server_status != Served.ALL:
self.startup_failure = True
if server_status == Served.TIMEOUT:
# this should never happen with a correctly functioning build
LOG.warning("Target hung after launch")
LOG.warning("Post launch check failed!")

def run(
self,
Expand Down
25 changes: 10 additions & 15 deletions grizzly/common/test_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def test_runner_01(mocker, coverage, scheme):
testcase.add_from_bytes(b"", testcase.entry_point)
serv_files = {"a.bin": testcase.root / "a.bin"}
server.serve_path.return_value = (Served.ALL, serv_files)
result = runner.run([], serv_map, testcase, coverage=coverage)
result = runner.run(set(), serv_map, testcase, coverage=coverage)
assert testcase.https == (scheme == "https")
assert runner.initial
assert runner._tests_run == 1
Expand Down Expand Up @@ -81,7 +81,7 @@ def test_runner_02(mocker):
runner = Runner(server, target, relaunch=1)
assert runner._relaunch == 1
smap = ServerMap()
result = runner.run([], smap, testcase)
result = runner.run(set(), smap, testcase)
assert runner.initial
assert result.attempted
assert target.close.call_count == 1
Expand All @@ -100,7 +100,7 @@ def test_runner_02(mocker):
target.monitor.is_idle.return_value = True
runner = Runner(server, target, relaunch=1)
assert runner._relaunch == 1
result = runner.run([], ServerMap(), testcase)
result = runner.run(set(), ServerMap(), testcase)
assert result.attempted
assert target.close.call_count == 1
assert target.monitor.is_healthy.call_count > 0
Expand All @@ -111,7 +111,7 @@ def test_runner_02(mocker):
target.monitor.is_healthy.return_value = False
for _ in range(2):
smap = ServerMap()
result = runner.run([], smap, testcase)
result = runner.run(set(), smap, testcase)
assert result.attempted
assert target.close.call_count == 0
assert target.monitor.is_healthy.call_count == 0
Expand Down Expand Up @@ -154,7 +154,7 @@ def test_runner_03(mocker, srv_result, served):
target.check_result.return_value = Result.NONE
test = mocker.Mock(spec_set=TestCase, entry_point="x", required=["x"])
runner = Runner(server, target)
result = runner.run([], ServerMap(), test)
result = runner.run(set(), ServerMap(), test)
assert runner.initial
assert runner.startup_failure
assert result
Expand Down Expand Up @@ -224,7 +224,7 @@ def test_runner_05(mocker, served, attempted, target_result, status):
testcase = mocker.Mock(spec_set=TestCase, entry_point="a.bin", required=["a.bin"])
runner = Runner(server, target)
runner.launch("http://a/")
result = runner.run([], ServerMap(), testcase)
result = runner.run(set(), ServerMap(), testcase)
assert result.attempted == attempted
assert result.status == status
assert not result.timeout
Expand All @@ -242,7 +242,7 @@ def test_runner_06(mocker):
runner = Runner(server, target, idle_threshold=0.01, idle_delay=0.01, relaunch=10)
assert runner._idle is not None
result = runner.run(
[],
set(),
ServerMap(),
mocker.Mock(spec_set=TestCase, entry_point="a.bin", required=tuple(serv_files)),
)
Expand Down Expand Up @@ -357,7 +357,7 @@ def test_runner_10(mocker, tmp_path):
"test/inc_file3.txt": inc3,
}
server.serve_path.return_value = (Served.ALL, serv_files)
result = runner.run([], smap, test)
result = runner.run(set(), smap, test)
assert result.attempted
assert result.status == Result.NONE
assert "inc_file.bin" in test
Expand Down Expand Up @@ -386,7 +386,7 @@ def test_runner_11(mocker):
"extra.js": test.root / "extra.js",
},
)
result = runner.run([], ServerMap(), test)
result = runner.run(set(), ServerMap(), test)
assert result.attempted
assert result.status == Result.NONE
assert "test.html" in test
Expand All @@ -401,8 +401,6 @@ def test_runner_11(mocker):
(10, (Served.ALL, None), False),
# continue immediately
(0, (Served.ALL, None), False),
# skip post launch delay page
(-1, None, False),
# startup failure
(0, (Served.NONE, None), True),
# target hang while loading content
Expand All @@ -412,10 +410,7 @@ def test_runner_11(mocker):
def test_runner_12(mocker, delay, srv_result, startup_failure):
"""test Runner.post_launch()"""
srv_timeout = 1
server = mocker.Mock(
spec_set=Sapphire,
timeout=srv_timeout,
)
server = mocker.Mock(spec_set=Sapphire, timeout=srv_timeout)
server.serve_path.return_value = srv_result
runner = Runner(server, mocker.Mock(spec_set=Target, launch_timeout=30))
runner.launch("http://a/")
Expand Down
7 changes: 4 additions & 3 deletions grizzly/replay/replay.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ def run(
idle_threshold: int = 0,
launch_attempts: int = 3,
on_iteration_cb: Optional[Callable[[], None]] = None,
post_launch_delay: int = 0,
post_launch_delay: int = -1,
) -> List[ReplayResult]:
"""Run testcase replay.
Expand All @@ -313,7 +313,7 @@ def run(
launch_attempts: Number of attempts to launch the browser.
on_iteration_cb: Called every time a single iteration is run.
post_launch_delay: Number of seconds to wait before continuing after the
browser is launched.
browser is launched. A negative number skips redirect.
Returns:
ReplayResults that were found running provided testcases.
Expand Down Expand Up @@ -379,7 +379,8 @@ def harness_fn(_: str) -> bytes: # pragma: no cover
time_limit=time_limit if self._harness else None,
)
runner.launch(location, max_retries=launch_attempts)
runner.post_launch(delay=post_launch_delay)
if post_launch_delay >= 0 and not runner.startup_failure:
runner.post_launch(delay=post_launch_delay)
# TODO: avoid running test case if runner.startup_failure is True
# run tests
durations: List[float] = []
Expand Down
Loading

0 comments on commit 0378e21

Please sign in to comment.