From 76f6eb741dc88e07a671e4b9b472b5a27fd5b3fa Mon Sep 17 00:00:00 2001 From: Alex Willmer Date: Fri, 15 Aug 2025 17:34:46 +0100 Subject: [PATCH] tests: Count bytes written in stdio_test.StdIOTest This is mainly for peace of mind. With all this non-blocking IO investigation I'm getting a bit paranoid wrt file objects. refs #712 --- docs/changelog.rst | 1 + tests/data/stdio_checks.py | 19 +++++++++++++++++-- tests/stdio_test.py | 4 ++-- 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/docs/changelog.rst b/docs/changelog.rst index efa01375..dbf653dc 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -24,6 +24,7 @@ In progress (unreleased) * :gh:issue:`1306` CI: Report sudo version on Ansible targets * :gh:issue:`1306` CI: Move sudo test users defaults into ``/etc/sudoers.d`` * :gh:issue:`1306` preamble_size: Fix variability of measured command size +* :gh:issue:`1306` tests: Count bytes written in ``stdio_test.StdIOTest`` v0.3.27 (2025-08-20) diff --git a/tests/data/stdio_checks.py b/tests/data/stdio_checks.py index 5bfe8063..a67e2689 100644 --- a/tests/data/stdio_checks.py +++ b/tests/data/stdio_checks.py @@ -3,9 +3,24 @@ import os import sys +def _shout_stdout_py3(size): + nwritten = sys.stdout.write('A' * size) + return nwritten + + +def _shout_stdout_py2(size): + shout = 'A' * size + nwritten = 0 + while nwritten < size: + nwritten += os.write(sys.stdout.fileno(), shout[-nwritten:]) + return nwritten + + def shout_stdout(size): - sys.stdout.write('A' * size) - return 'success' + if sys.version_info > (3, 0): + return _shout_stdout_py3(size) + else: + return _shout_stdout_py2(size) def file_is_blocking(fobj): diff --git a/tests/stdio_test.py b/tests/stdio_test.py index da8edd8e..3c0e91d1 100644 --- a/tests/stdio_test.py +++ b/tests/stdio_test.py @@ -15,8 +15,8 @@ class StdIOTest(testlib.RouterMixin, testlib.TestCase): """ size = 1 * 2**20 context = self.router.local() - result = context.call(stdio_checks.shout_stdout, size) - self.assertEqual('success', result) + nwritten = context.call(stdio_checks.shout_stdout, size) + self.assertEqual(nwritten, size) def test_stdio_is_blocking(self): context = self.router.local()