Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unittest needs more flush calls #57445

Closed
petere mannequin opened this issue Oct 20, 2011 · 12 comments
Closed

unittest needs more flush calls #57445

petere mannequin opened this issue Oct 20, 2011 · 12 comments
Labels
3.9 only security fixes 3.10 only security fixes 3.11 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@petere
Copy link
Mannequin

petere mannequin commented Oct 20, 2011

BPO 13236
Nosy @pitrou, @rbtcollins, @ezio-melotti, @bitdancer, @petere, @ambv, @serhiy-storchaka, @matrixise, @miss-islington, @iritkatriel
PRs
  • bpo-13236: Flush the output stream more often in unittest #29864
  • [3.10] bpo-13236: Flush the output stream more often in unittest (GH-29864) #29929
  • [3.9] bpo-13236: Flush the output stream more often in unittest (GH-29864) (GH-29929) #30039
  • Files
  • testcase.py: test case
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2021-12-11.00:36:03.403>
    created_at = <Date 2011-10-20.19:02:03.573>
    labels = ['type-bug', 'library', '3.9', '3.10', '3.11']
    title = 'unittest needs more flush calls'
    updated_at = <Date 2021-12-11.00:36:03.402>
    user = 'https://github.com/petere'

    bugs.python.org fields:

    activity = <Date 2021-12-11.00:36:03.402>
    actor = 'lukasz.langa'
    assignee = 'none'
    closed = True
    closed_date = <Date 2021-12-11.00:36:03.403>
    closer = 'lukasz.langa'
    components = ['Library (Lib)']
    creation = <Date 2011-10-20.19:02:03.573>
    creator = 'petere'
    dependencies = []
    files = ['23527']
    hgrepos = []
    issue_num = 13236
    keywords = ['patch']
    message_count = 12.0
    messages = ['146043', '146044', '146051', '146446', '229420', '229459', '407338', '407366', '407682', '408268', '408284', '408285']
    nosy_count = 10.0
    nosy_names = ['pitrou', 'rbcollins', 'ezio.melotti', 'r.david.murray', 'petere', 'lukasz.langa', 'serhiy.storchaka', 'matrixise', 'miss-islington', 'iritkatriel']
    pr_nums = ['29864', '29929', '30039']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue13236'
    versions = ['Python 3.9', 'Python 3.10', 'Python 3.11']

    @petere
    Copy link
    Mannequin Author

    petere mannequin commented Oct 20, 2011

    I'm using the TextTestRunner class in unittest/runner.py with a special file-like object passed in as stream. Doing this loses some output, because the run() method (and some lower-level methods) don't always call flush() on the stream. There is also no obvious cleanup method in the runner class that might do this, so I assume run() should do that itself.

    Right now, it looks like it assumes that either the stream is effectively unbuffered, like stderr, or that the end of the program might flush things, but that doesn't always apply.

    It looks like the best fix would be a self.stream.flush() call at the end of run(). Another flush() call at the end of printErrorList() would also help.

    (In the meantime, I have fixed up my special file-like class to flush its internal buffers when a newline is seen, which kind of works, but a proper cleanup of this matter would still be nice.)

    @petere petere mannequin added stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Oct 20, 2011
    @bitdancer
    Copy link
    Member

    How can a lack of flushes lose output? Delay it maybe, but unless your file class is broken I don't see how data would be lost.

    @ezio-melotti
    Copy link
    Member

    Can you provide a minimal test case to reproduce the issue?

    @petere
    Copy link
    Mannequin Author

    petere mannequin commented Oct 26, 2011

    Attached is a test file. The key here is that I'm running the unittest suite inside of a long-running server process, so there is no predictable point of exit and cleanup. Therefore, the steps I show at the end of the file should be run in an interactive interpreter. Here is what I see:

    >>> import unittest                                                                                                                             
    >>> import testcase                                                                                                                             
    >>> unittest.main(module=testcase)
    .F.

    ======================================================================
    FAIL: test_three (testcase.test)
    ----------------------------------------------------------------------

    Traceback (most recent call last):
      File "testcase.py", line 35, in test_three
        self.fail("intentional failure")
    AssertionError: intentional failure

    Ran 3 tests in 0.002s

    FAILED (failures=1)

    That's good.

    But:

    >>> import unittest                                                                                                                             
    >>> import testcase                                                                                                                             
    >>> unittest.main(module=testcase, testRunner=testcase.FunnyTestRunner, exit=False)                                                             
    .
    F
    .
    <unittest.main.TestProgram object at 0xf718fd4c>

    You see, the test report at the end is missing.

    I'm happy to consider other ways of addressing this, if anyone has an idea.

    @matrixise
    Copy link
    Member

    ping about this bug, do you have the same problem with the last version of Python 3.4 and with Python 2.7.8 ?

    @pitrou
    Copy link
    Member

    pitrou commented Oct 15, 2014

    And if the issue still holds, please provide a patch adding the appropriate flush() calls.

    @iritkatriel
    Copy link
    Member

    Reproduced on 3.11.

    @iritkatriel iritkatriel added the 3.11 only security fixes label Nov 29, 2021
    @serhiy-storchaka
    Copy link
    Member

    I do not remember details, but I encountered this issue (maybe when run tests in subprocesses?).

    @serhiy-storchaka serhiy-storchaka added 3.9 only security fixes 3.10 only security fixes labels Nov 30, 2021
    @serhiy-storchaka
    Copy link
    Member

    New changeset f42a06b by Serhiy Storchaka in branch 'main':
    bpo-13236: Flush the output stream more often in unittest (GH-29864)
    f42a06b

    @ambv
    Copy link
    Contributor

    ambv commented Dec 10, 2021

    New changeset 83fa129 by Serhiy Storchaka in branch '3.10':
    [3.10] bpo-13236: Flush the output stream more often in unittest (GH-29864) (GH-29929)
    83fa129

    @ambv
    Copy link
    Contributor

    ambv commented Dec 11, 2021

    New changeset d55a03e by Miss Islington (bot) in branch '3.9':
    [3.10] bpo-13236: Flush the output stream more often in unittest (GH-29929) (GH-30039)
    d55a03e

    @ambv
    Copy link
    Contributor

    ambv commented Dec 11, 2021

    Thanks, Serhiy! ✨ 🍰 ✨

    @ambv ambv closed this as completed Dec 11, 2021
    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.9 only security fixes 3.10 only security fixes 3.11 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    7 participants