From 2d9e90acf914fe838adf062222dba748a31b7035 Mon Sep 17 00:00:00 2001 From: Marc Hartmayer Date: Mon, 15 Dec 2025 10:00:17 +0000 Subject: [PATCH] parent_test: Refactor `wait_for_child` Signed-off-by: Marc Hartmayer --- tests/parent_test.py | 22 ++-------------------- tests/testlib.py | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 20 deletions(-) diff --git a/tests/parent_test.py b/tests/parent_test.py index 558a89b6..dfacffb1 100644 --- a/tests/parent_test.py +++ b/tests/parent_test.py @@ -2,7 +2,6 @@ import errno import fcntl import os import signal -import sys import time import unittest @@ -21,23 +20,6 @@ except NameError: from io import FileIO as file -def wait_for_child(pid, timeout=1.0): - deadline = mitogen.core.now() + timeout - while timeout < mitogen.core.now(): - try: - target_pid, status = os.waitpid(pid, os.WNOHANG) - if target_pid == pid: - return - except OSError: - e = sys.exc_info()[1] - if e.args[0] == errno.ECHILD: - return - - time.sleep(0.05) - - assert False, "wait_for_child() timed out" - - @mitogen.core.takes_econtext def call_func_in_sibling(ctx, econtext, sync_sender): recv = ctx.call_async(time.sleep, 99999) @@ -106,7 +88,7 @@ class ReapChildTest(testlib.RouterMixin, testlib.TestCase): self.assertRaises(mitogen.core.TimeoutError, lambda: conn.connect(context=mitogen.core.Context(None, 1234)) ) - wait_for_child(conn.proc.pid) + testlib.wait_for_child(conn.proc.pid) e = self.assertRaises(OSError, lambda: os.kill(conn.proc.pid, 0) ) @@ -165,7 +147,7 @@ class ContextTest(testlib.RouterMixin, testlib.TestCase): local = self.router.local() pid = local.call(os.getpid) local.shutdown(wait=True) - wait_for_child(pid) + testlib.wait_for_child(pid) self.assertRaises(OSError, lambda: os.kill(pid, 0)) diff --git a/tests/testlib.py b/tests/testlib.py index e9104000..868aaa84 100644 --- a/tests/testlib.py +++ b/tests/testlib.py @@ -86,6 +86,23 @@ if faulthandler is not None: mitogen.core.LOG.propagate = True +def wait_for_child(pid, timeout=1.0): + deadline = mitogen.core.now() + timeout + while timeout < mitogen.core.now(): + try: + target_pid, status = os.waitpid(pid, os.WNOHANG) + if target_pid == pid: + return + except OSError: + e = sys.exc_info()[1] + if e.args[0] == errno.ECHILD: + return + + time.sleep(0.05) + + assert False, "wait_for_child() timed out" + + def base_executable(executable=None): '''Return the path of the Python executable used to create the virtualenv. '''