| 1 |
from __future__ import annotations |
| 2 |
from pathlib import Path |
| 3 |
import subprocess |
| 4 |
import tempfile |
| 5 |
from unittest.mock import patch |
| 6 |
import pytest |
| 7 |
from utils import MockStream |
| 8 |
from con_duct._output import TailPipe |
| 9 |
|
| 10 |
|
| 11 |
FIXTURE_LIST = [f"ten_{i}" for i in range(1, 8)] |
| 12 |
|
| 13 |
|
| 14 |
@pytest.fixture(scope="module", params=FIXTURE_LIST) |
| 15 |
def fixture_path( |
| 16 |
request: pytest.FixtureRequest, tmp_path_factory: pytest.TempPathFactory |
| 17 |
) -> str: |
| 18 |
num_lines_exponent = int(request.param.split("_")[1]) |
| 19 |
base_temp_dir = tmp_path_factory.mktemp("fixture_data") |
| 20 |
file_path = base_temp_dir / f"{request.param}.txt" |
| 21 |
with open(file_path, "w") as f: |
| 22 |
for i in range(10**num_lines_exponent): |
| 23 |
f.write(f"{i}\n") |
| 24 |
|
| 25 |
|
| 26 |
return str(file_path) |
| 27 |
|
| 28 |
|
| 29 |
@patch("sys.stdout", new_callable=MockStream) |
| 30 |
def test_high_throughput_stdout(mock_stdout: MockStream, fixture_path: str) -> None: |
| 31 |
with tempfile.NamedTemporaryFile(mode="wb") as tmpfile: |
| 32 |
process = subprocess.Popen( |
| 33 |
["cat", fixture_path], |
| 34 |
stdout=tmpfile, |
| 35 |
) |
| 36 |
stream = TailPipe(tmpfile.name, mock_stdout.buffer) |
| 37 |
stream.start() |
| 38 |
process.wait() |
| 39 |
stream.close() |
| 40 |
|
| 41 |
assert process.returncode == 0 |
| 42 |
with open(fixture_path, "rb") as fixture: |
| 43 |
expected = fixture.read() |
| 44 |
assert mock_stdout.getvalue() == expected |
| 45 |
|
| 46 |
|
| 47 |
@patch("sys.stderr", new_callable=MockStream) |
| 48 |
def test_high_throughput_stderr(mock_stderr: MockStream, fixture_path: str) -> None: |
| 49 |
with tempfile.NamedTemporaryFile(mode="wb") as tmpfile: |
| 50 |
process = subprocess.Popen( |
| 51 |
[Path(__file__).parent.parent / "data" / "cat_to_err.py", fixture_path], |
| 52 |
stdout=subprocess.DEVNULL, |
| 53 |
stderr=tmpfile, |
| 54 |
) |
| 55 |
stream = TailPipe(tmpfile.name, mock_stderr.buffer) |
| 56 |
stream.start() |
| 57 |
process.wait() |
| 58 |
stream.close() |
| 59 |
|
| 60 |
assert process.returncode == 0 |
| 61 |
with open(fixture_path, "rb") as fixture: |
| 62 |
expected = fixture.read() |
| 63 |
assert mock_stderr.getvalue() == expected |
| 64 |
|
| 65 |
|
| 66 |
@patch("sys.stdout", new_callable=MockStream) |
| 67 |
def test_close(mock_stdout: MockStream) -> None: |
| 68 |
with tempfile.NamedTemporaryFile(mode="wb") as tmpfile: |
| 69 |
stream = TailPipe(tmpfile.name, mock_stdout.buffer) |
| 70 |
stream.start() |
| 71 |
stream.close() |
| 72 |
assert stream.infile is not None |
| 73 |
assert stream.infile.closed |