From 3035cdc56bf46b8150bb85c42160cfc3f144cd57 Mon Sep 17 00:00:00 2001 From: varun-r-mallya Date: Sun, 1 Jun 2025 19:20:54 +0530 Subject: [PATCH] Improve performance of read from daemon test Signed-off-by: varun-r-mallya --- tests/utils/interop/process.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/tests/utils/interop/process.py b/tests/utils/interop/process.py index cce4d78e..0d9e2650 100644 --- a/tests/utils/interop/process.py +++ b/tests/utils/interop/process.py @@ -32,18 +32,25 @@ class BaseInteractiveProcess(AbstractInterativeProcess): async def wait_until_ready(self) -> None: patterns_occurred = {pat: False for pat in self.patterns} + buffers = {pat: bytearray() for pat in self.patterns} async def read_from_daemon_and_check() -> None: async for data in self.proc.stdout: - # TODO: It takes O(n^2), which is quite bad. - # But it should succeed in a few seconds. self.bytes_read.extend(data) for pat, occurred in patterns_occurred.items(): if occurred: continue - if pat in self.bytes_read: + + # Check if pattern is in new data or spans across chunks + buf = buffers[pat] + buf.extend(data) + if pat in buf: patterns_occurred[pat] = True - if all([value for value in patterns_occurred.values()]): + else: + keep = min(len(pat) - 1, len(buf)) + buffers[pat] = buf[-keep:] if keep > 0 else bytearray() + + if all(patterns_occurred.values()): return with trio.fail_after(TIMEOUT_DURATION):