Fix Snap smoke startup race before viewport readiness (#5346)
This commit is contained in:
@@ -10,6 +10,7 @@ Reuses the frame checks of flatpak-smoke.py.
|
||||
import argparse
|
||||
import importlib.util
|
||||
import json
|
||||
import math
|
||||
import os
|
||||
from pathlib import Path
|
||||
import shutil
|
||||
@@ -51,9 +52,41 @@ def drive(app, port, frame):
|
||||
raise RuntimeError("the control port never opened") from None
|
||||
time.sleep(.2)
|
||||
with conn, conn.makefile("rw") as io:
|
||||
replies = []
|
||||
for request in REQUESTS + [{"id": 4, "method": "ui.screenshot", "params": {"path": str(frame)}},
|
||||
{"id": 5, "method": "app.quit", "params": {"force": True}}]:
|
||||
# Listening precedes the first frame, whose ui.inspect still has viewport:null.
|
||||
deadline = time.monotonic() + 60
|
||||
while True:
|
||||
if app.poll() is not None:
|
||||
raise RuntimeError(f"the app exited with {app.returncode} before creating a viewport")
|
||||
remaining = deadline - time.monotonic()
|
||||
if remaining <= 0:
|
||||
raise RuntimeError("the viewport never appeared")
|
||||
conn.settimeout(remaining)
|
||||
io.write(json.dumps(REQUESTS[0]) + "\n")
|
||||
io.flush()
|
||||
try:
|
||||
line = io.readline()
|
||||
except TimeoutError as error:
|
||||
raise RuntimeError("the viewport never appeared before control timed out") from error
|
||||
if not line:
|
||||
raise RuntimeError("the control connection closed before creating a viewport")
|
||||
reply = json.loads(line)
|
||||
if not isinstance(reply, dict) or reply.get("id") != 1 or reply.get("ok") is not True:
|
||||
raise ValueError(f"ui.inspect failed: {reply}")
|
||||
ui = reply["result"]
|
||||
if ui["renderer"] not in {"gpu", "cpu"}:
|
||||
raise ValueError("ui.inspect returned an invalid renderer")
|
||||
viewport = ui["viewport"]
|
||||
if viewport is not None:
|
||||
if (not isinstance(viewport, list) or len(viewport) != 4
|
||||
or not all(isinstance(value, (int, float)) and math.isfinite(value) for value in viewport)
|
||||
or viewport[2] <= 0 or viewport[3] <= 0):
|
||||
raise ValueError("ui.inspect returned an unusable viewport")
|
||||
break
|
||||
time.sleep(.25)
|
||||
conn.settimeout(60)
|
||||
replies = [line]
|
||||
for request in REQUESTS[1:] + [{"id": 4, "method": "ui.screenshot", "params": {"path": str(frame)}},
|
||||
{"id": 5, "method": "app.quit", "params": {"force": True}}]:
|
||||
io.write(json.dumps(request) + "\n")
|
||||
io.flush()
|
||||
replies.append(io.readline())
|
||||
|
||||
@@ -1,15 +1,20 @@
|
||||
"""Fast failure-path checks for the desktop Flatpak process probe."""
|
||||
"""Fast failure-path checks for the desktop Flatpak and snap process probes."""
|
||||
import importlib.util
|
||||
import itertools
|
||||
import json
|
||||
import struct
|
||||
import zlib
|
||||
from pathlib import Path
|
||||
import unittest
|
||||
from unittest.mock import Mock, patch
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[2]
|
||||
SPEC = importlib.util.spec_from_file_location("flatpak_smoke", ROOT / "packaging/linux/flatpak-smoke.py")
|
||||
SMOKE = importlib.util.module_from_spec(SPEC)
|
||||
SPEC.loader.exec_module(SMOKE)
|
||||
SNAP_SPEC = importlib.util.spec_from_file_location("snap_smoke", ROOT / "packaging/linux/snap-smoke.py")
|
||||
SNAP = importlib.util.module_from_spec(SNAP_SPEC)
|
||||
SNAP_SPEC.loader.exec_module(SNAP)
|
||||
|
||||
|
||||
def healthy_replies():
|
||||
@@ -37,6 +42,102 @@ def rgba_png(width, height, rows):
|
||||
+ chunk(b"IDAT", zlib.compress(rows)) + chunk(b"IEND", b""))
|
||||
|
||||
|
||||
class Control:
|
||||
"""A control connection whose first frame can arrive after the port opens."""
|
||||
def __init__(self, viewports):
|
||||
self.viewports = iter(viewports)
|
||||
self.requests = []
|
||||
self.settimeout = Mock()
|
||||
|
||||
def __enter__(self):
|
||||
return self
|
||||
|
||||
def __exit__(self, *args):
|
||||
pass
|
||||
|
||||
def makefile(self, mode):
|
||||
return self
|
||||
|
||||
def write(self, data):
|
||||
self.requests.append(json.loads(data))
|
||||
|
||||
def flush(self):
|
||||
pass
|
||||
|
||||
def readline(self):
|
||||
request = self.requests[-1]
|
||||
index = request["id"] - 1
|
||||
reply = healthy_replies()[index] if index < 3 else {"id": request["id"], "ok": True, "result": {}}
|
||||
if index == 0:
|
||||
reply["result"]["viewport"] = next(self.viewports)
|
||||
return json.dumps(reply) + "\n"
|
||||
|
||||
|
||||
class SnapStartup(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.app = Mock()
|
||||
self.app.poll.return_value = None
|
||||
self.frame = Mock()
|
||||
self.frame.read_bytes.return_value = rgba_png(2, 2, b"\x00" + bytes([255, 0, 0, 255, 0, 255, 0, 255])
|
||||
+ b"\x00" + bytes([0, 0, 255, 255, 255, 255, 255, 255]))
|
||||
|
||||
def drive(self, control, times=None):
|
||||
with patch.object(SNAP.socket, "create_connection", return_value=control), \
|
||||
patch.object(SNAP.time, "sleep"), \
|
||||
patch.object(SNAP.time, "monotonic", side_effect=times, return_value=0):
|
||||
return SNAP.drive(self.app, 18839, self.frame)
|
||||
|
||||
def test_control_opens_before_first_frame(self):
|
||||
control = Control([None, [250, 126, 1000, 700]])
|
||||
renderer, statistics = self.drive(control)
|
||||
self.assertEqual(renderer, "gpu")
|
||||
self.assertEqual(statistics["nonblack"], 4)
|
||||
self.assertEqual([request["method"] for request in control.requests], [
|
||||
"ui.inspect", "ui.inspect", "engine.execute", "document.inspect", "ui.screenshot", "app.quit"
|
||||
])
|
||||
|
||||
def test_ready_startup_does_not_wait(self):
|
||||
control = Control([[250, 126, 1000, 700]])
|
||||
self.drive(control)
|
||||
self.assertEqual(sum(r["method"] == "ui.inspect" for r in control.requests), 1)
|
||||
|
||||
def test_missing_first_frame_times_out_before_modelling(self):
|
||||
control = Control(itertools.repeat(None))
|
||||
with self.assertRaisesRegex(RuntimeError, "viewport"):
|
||||
self.drive(control, [0, 0, 0, 61])
|
||||
self.assertTrue(all(r["method"] == "ui.inspect" for r in control.requests))
|
||||
|
||||
def test_app_exit_while_waiting_for_frame_fails_before_modelling(self):
|
||||
control = Control(itertools.repeat(None))
|
||||
self.app.poll.side_effect = [None, None, 7]
|
||||
self.app.returncode = 7
|
||||
with self.assertRaisesRegex(RuntimeError, "exited"):
|
||||
self.drive(control)
|
||||
self.assertTrue(all(r["method"] == "ui.inspect" for r in control.requests))
|
||||
|
||||
def test_failed_control_response_does_not_retry_or_model(self):
|
||||
control = Control([None])
|
||||
control.readline = Mock(return_value=json.dumps({"id": 1, "ok": False, "error": "startup failure"}) + "\n")
|
||||
with self.assertRaisesRegex(ValueError, "ui.inspect"):
|
||||
self.drive(control)
|
||||
self.assertEqual([r["method"] for r in control.requests], ["ui.inspect"])
|
||||
|
||||
def test_read_timeout_uses_remaining_readiness_deadline_then_restores_operation_timeout(self):
|
||||
control = Control([None, [250, 126, 1000, 700]])
|
||||
self.drive(control, [0, 0, 20, 50])
|
||||
self.assertEqual([call.args[0] for call in control.settimeout.call_args_list], [40, 10, 60])
|
||||
|
||||
def test_closed_or_malformed_control_fails_before_modelling(self):
|
||||
for response, error in [("", RuntimeError), ("not json\n", ValueError),
|
||||
(json.dumps({"id": 9, "ok": True}) + "\n", ValueError)]:
|
||||
with self.subTest(response=response):
|
||||
control = Control([None])
|
||||
control.readline = Mock(return_value=response)
|
||||
with self.assertRaises(error):
|
||||
self.drive(control)
|
||||
self.assertEqual([r["method"] for r in control.requests], ["ui.inspect"])
|
||||
|
||||
|
||||
class DesktopProbe(unittest.TestCase):
|
||||
def test_real_presented_frame_is_required_not_just_responsive_control(self):
|
||||
black = rgba_png(2, 2, b"\x00" + bytes([0, 0, 0, 255]) * 2 + b"\x00" + bytes([0, 0, 0, 255]) * 2)
|
||||
|
||||
Reference in New Issue
Block a user