|
| 1 | +"""Tests for logging_subprocess pipe handling (issue #519).""" |
| 2 | + |
| 3 | +import logging |
| 4 | +import sys |
| 5 | +import threading |
| 6 | + |
| 7 | +import pytest |
| 8 | + |
| 9 | +from github_backup import github_backup |
| 10 | + |
| 11 | + |
| 12 | +class TestLoggingSubprocess: |
| 13 | + """Test suite for logging_subprocess deadlock and logging behavior.""" |
| 14 | + |
| 15 | + def test_large_stderr_output_does_not_deadlock(self): |
| 16 | + """Child output larger than the OS pipe buffer must not hang. |
| 17 | +
|
| 18 | + Regression test for issue #519: on Windows the pipes were never |
| 19 | + drained, so the child blocked once its output exceeded the pipe |
| 20 | + buffer (~8KB) and the parent spun forever on poll(). |
| 21 | + """ |
| 22 | + # Write 256KB to stderr, far past any platform's pipe buffer |
| 23 | + child_code = ( |
| 24 | + "import sys\n" |
| 25 | + "for _ in range(3200):\n" |
| 26 | + " sys.stderr.write('x' * 79 + '\\n')\n" |
| 27 | + ) |
| 28 | + result = {} |
| 29 | + |
| 30 | + def run(): |
| 31 | + result["rc"] = github_backup.logging_subprocess( |
| 32 | + [sys.executable, "-c", child_code] |
| 33 | + ) |
| 34 | + |
| 35 | + thread = threading.Thread(target=run, daemon=True) |
| 36 | + thread.start() |
| 37 | + thread.join(timeout=30) |
| 38 | + |
| 39 | + assert not thread.is_alive(), "logging_subprocess deadlocked on large output" |
| 40 | + assert result["rc"] == 0 |
| 41 | + |
| 42 | + def test_stdout_logged_at_debug_stderr_at_error(self, caplog): |
| 43 | + """stdout lines log at DEBUG and stderr lines at ERROR.""" |
| 44 | + child_code = ( |
| 45 | + "import sys\n" |
| 46 | + "print('to stdout')\n" |
| 47 | + "print('to stderr', file=sys.stderr)\n" |
| 48 | + ) |
| 49 | + |
| 50 | + with caplog.at_level(logging.DEBUG, logger="github_backup.github_backup"): |
| 51 | + rc = github_backup.logging_subprocess([sys.executable, "-c", child_code]) |
| 52 | + |
| 53 | + assert rc == 0 |
| 54 | + records = [ |
| 55 | + (r.levelno, r.getMessage()) |
| 56 | + for r in caplog.records |
| 57 | + if r.name == "github_backup.github_backup" |
| 58 | + ] |
| 59 | + assert (logging.DEBUG, str(b"to stdout")) in records |
| 60 | + assert (logging.ERROR, str(b"to stderr")) in records |
| 61 | + |
| 62 | + def test_trailing_newlines_stripped(self, caplog): |
| 63 | + """Logged lines have trailing \\r\\n stripped, including Windows CRLF.""" |
| 64 | + child_code = ( |
| 65 | + "import sys\n" |
| 66 | + "sys.stdout.buffer.write(b'crlf line\\r\\n')\n" |
| 67 | + "sys.stdout.buffer.write(b'lf line\\n')\n" |
| 68 | + ) |
| 69 | + |
| 70 | + with caplog.at_level(logging.DEBUG, logger="github_backup.github_backup"): |
| 71 | + rc = github_backup.logging_subprocess([sys.executable, "-c", child_code]) |
| 72 | + |
| 73 | + assert rc == 0 |
| 74 | + messages = [ |
| 75 | + r.getMessage() |
| 76 | + for r in caplog.records |
| 77 | + if r.name == "github_backup.github_backup" |
| 78 | + ] |
| 79 | + assert str(b"crlf line") in messages |
| 80 | + assert str(b"lf line") in messages |
| 81 | + |
| 82 | + def test_final_line_without_newline_not_truncated(self, caplog): |
| 83 | + """A final line with no trailing newline keeps its last character. |
| 84 | +
|
| 85 | + The old code stripped the newline with line[:-1], which chopped the |
| 86 | + last character off any line that did not end with a newline. |
| 87 | + """ |
| 88 | + child_code = "import sys\nsys.stdout.write('no newline')\n" |
| 89 | + |
| 90 | + with caplog.at_level(logging.DEBUG, logger="github_backup.github_backup"): |
| 91 | + rc = github_backup.logging_subprocess([sys.executable, "-c", child_code]) |
| 92 | + |
| 93 | + assert rc == 0 |
| 94 | + messages = [ |
| 95 | + r.getMessage() |
| 96 | + for r in caplog.records |
| 97 | + if r.name == "github_backup.github_backup" |
| 98 | + ] |
| 99 | + assert str(b"no newline") in messages |
| 100 | + |
| 101 | + def test_returns_child_exit_code(self, capsys): |
| 102 | + """Non-zero child exit codes are returned and summarized on stderr.""" |
| 103 | + rc = github_backup.logging_subprocess( |
| 104 | + [sys.executable, "-c", "import sys; sys.exit(3)"] |
| 105 | + ) |
| 106 | + |
| 107 | + assert rc == 3 |
| 108 | + captured = capsys.readouterr() |
| 109 | + assert "returned 3" in captured.err |
| 110 | + |
| 111 | + |
| 112 | +if __name__ == "__main__": |
| 113 | + pytest.main([__file__, "-v"]) |
0 commit comments