diff --git a/qemu/tests/qmp.py b/qemu/tests/qmp.py index 7f4c51d..e73dba8 100755 --- a/qemu/tests/qmp.py +++ b/qemu/tests/qmp.py @@ -154,7 +154,13 @@ def _load_imgtools(): AXIS_MAX = 32767 TAP_HOLD_S = 0.35 -TAP_OBSERVE_TIMEOUT_S = 2.0 +# How long the runner waits for the guest to count an injected press or +# release. A behaviour check, not a budget: a loaded runner (two TCG VMs on a +# shared host) has taken longer than 2 s to count the release that opens the +# drawer (flare-edge#1138), so the wait is a hang backstop. A wait longer than +# INPUT_SLOW_NOTE_S still passes and says so in the step's detail. +TAP_OBSERVE_TIMEOUT_S = 20.0 +INPUT_SLOW_NOTE_S = 2.0 # Bounds every blocking read on the QMP socket -- the greeting banner, the # qmp_capabilities handshake, and every screendump/tap/quit round trip -- @@ -223,19 +229,36 @@ def do_tap(s, f, ax, ay, hold=TAP_HOLD_S): def wait_input_count(ctx, field, before): - """Wait until one guest input counter advances.""" + """Wait until one guest input counter advances. + + Records the longest wait of the current verb in ctx.input_wait_s, so a + slow guest shows in the results without failing the step.""" if before is None: time.sleep(0.05) return True - deadline = time.monotonic() + TAP_OBSERVE_TIMEOUT_S + start = time.monotonic() + deadline = start + TAP_OBSERVE_TIMEOUT_S while time.monotonic() < deadline: now = parse_stats(ctx.ctl.send("stats")).get(field) if now is not None and now != before: + waited = time.monotonic() - start + ctx.input_wait_s = max(getattr(ctx, "input_wait_s", 0.0), waited) return True time.sleep(0.02) return False +def input_verdict(ctx, observed): + """The (status, detail) of a tap, swipe or fling from its observation.""" + if not observed: + return ("fail", "input press or release was not consumed within " + f"{TAP_OBSERVE_TIMEOUT_S:g} seconds") + waited = getattr(ctx, "input_wait_s", 0.0) + if waited > INPUT_SLOW_NOTE_S: + return "ok", f"slow guest: input consumed after {waited:.1f} s" + return "ok", "" + + def do_observed_tap(ctx, ax, ay): """Wait until the guest consumes both halves of a tap.""" before = parse_stats(ctx.ctl.send("stats")) @@ -733,6 +756,8 @@ class Ctx: # console size when the step before this one began (flare-edge#233). self.next_cmd = None self.prev_step_console = 0 + # The longest input-count wait of the current tap, swipe or fling. + self.input_wait_s = 0.0 def need_ctl(self, lineno, cmd): if self.ctl is None: @@ -792,6 +817,7 @@ def verb_shot(ctx, lineno, cmd, args, line): def verb_tap(ctx, lineno, cmd, args, line): x, y = int(args[0]), int(args[1]) ctx.tap_sent = False + ctx.input_wait_s = 0.0 try: observed = do_observed_tap(ctx, to_axis(x, ctx.size), to_axis(y, ctx.size)) except (RuntimeError, OSError): @@ -804,23 +830,22 @@ def verb_tap(ctx, lineno, cmd, args, line): if ctx.next_cmd == "expect_reboot" and ctx.tap_sent: return "ok", "tap sent; the control channel closed as the guest went down" raise - return (("ok", "") if observed else - ("fail", "input press or release was not consumed within 2 seconds")) + return input_verdict(ctx, observed) def verb_swipe(ctx, lineno, cmd, args, line): ms = int(args[4]) if len(args) > 4 else 400 + ctx.input_wait_s = 0.0 observed = do_observed_swipe( ctx, int(args[0]), int(args[1]), int(args[2]), int(args[3]), ms) - return (("ok", "") if observed else - ("fail", "input press or release was not consumed within 2 seconds")) + return input_verdict(ctx, observed) def verb_fling(ctx, lineno, cmd, args, line): + ctx.input_wait_s = 0.0 observed = do_observed_swipe( ctx, int(args[0]), int(args[1]), int(args[2]), int(args[3]), ms=120) - return (("ok", "") if observed else - ("fail", "input press or release was not consumed within 2 seconds")) + return input_verdict(ctx, observed) def verb_sleep(ctx, lineno, cmd, args, line): diff --git a/qemu/tests/test_qmp_drive.py b/qemu/tests/test_qmp_drive.py index ae2fafa..a2dfeae 100755 --- a/qemu/tests/test_qmp_drive.py +++ b/qemu/tests/test_qmp_drive.py @@ -339,6 +339,52 @@ class PureHelpers(unittest.TestCase): self.assertEqual(len(calls), 2) self.assertEqual(calls[-1], [qmp.btn_ev(False)]) + def _swipe_on_fake_clock(self, release_at_s): + """verb_swipe against a guest that counts the press at once and the + release only once release_at_s of fake time has passed (None: never). + Returns the verdict and the fake time the verb took.""" + clock = [0.0] + replies = iter(["presses: 0\nreleases: 0"]) # the baseline read + + def send(cmd): + late = release_at_s is not None and clock[0] >= release_at_s + return next(replies, f"presses: 1\nreleases: {1 if late else 0}") + + ctx = SimpleNamespace(s=None, f=None, size=720, + ctl=SimpleNamespace(send=send), input_wait_s=0.0) + + def sleep(seconds): + clock[0] += seconds + + saved = qmp.send_events, qmp.time.sleep, qmp.time.monotonic + qmp.send_events = lambda _s, _f, _events: None + qmp.time.sleep = sleep + qmp.time.monotonic = lambda: clock[0] + try: + verdict = qmp.verb_swipe(ctx, 1, "swipe", ["2", "350", "520", "352", "3000"], "") + finally: + qmp.send_events, qmp.time.sleep, qmp.time.monotonic = saved + return verdict, clock[0] + + def test_swipe_release_counted_late_on_a_loaded_guest_passes(self): + # flare-edge#1138: the release that opens the drawer was counted + # after more than 2 s on a loaded runner; the old 2 s bound failed + # the swipe although the guest handled it. It passes, with a note. + (status, detail), _ = self._swipe_on_fake_clock(release_at_s=3.0 + 2.5) + self.assertEqual(status, "ok") + self.assertIn("slow guest: input consumed after 2.5 s", detail) + + def test_swipe_release_counted_promptly_has_no_note(self): + (status, detail), _ = self._swipe_on_fake_clock(release_at_s=3.0 + 0.5) + self.assertEqual((status, detail), ("ok", "")) + + def test_swipe_release_never_counted_fails_at_the_backstop(self): + (status, detail), took = self._swipe_on_fake_clock(release_at_s=None) + self.assertEqual(status, "fail") + self.assertIn(f"within {qmp.TAP_OBSERVE_TIMEOUT_S:g} seconds", detail) + # Bounded: the 3 s drag plus the backstop, and not much more. + self.assertLess(took, 3.0 + qmp.TAP_OBSERVE_TIMEOUT_S + 1.0) + def test_every_pixel_round_trips_through_lvgl_calibration(self): # lv_evdev.c _evdev_calibrate: px = axis * (width - 1) / AXIS_MAX, # integer division. A tap requested at px must land at px, for every