Skip to content

Commit

Permalink
Fix buffering unit test failing on some platforms (#896)
Browse files Browse the repository at this point in the history
  • Loading branch information
Delgan committed Jul 9, 2023
1 parent edca423 commit 5afeed8
Showing 1 changed file with 27 additions and 9 deletions.
36 changes: 27 additions & 9 deletions tests/test_add_option_kwargs.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import io

import pytest

from loguru import logger
Expand All @@ -21,13 +19,33 @@ def test_file_mode_w(tmp_path):
assert file.read_text() == "msg\n"


def test_file_buffering(tmp_path):
file = tmp_path / "test.log"
logger.add(file, format="{message}", buffering=-1)
logger.debug("x" * (io.DEFAULT_BUFFER_SIZE // 2))
assert file.read_text() == ""
logger.debug("x" * (io.DEFAULT_BUFFER_SIZE * 2))
assert file.read_text() != ""
def test_file_auto_buffering(tmp_path):
# There doesn't seem to be a reliable way to known buffer size for text files.
# We perform a preliminary test to ensure empirically that 128 <= buffer size <= 65536.
dummy_filepath = tmp_path / "dummy.txt"
with open(str(dummy_filepath), buffering=-1, mode="w") as dummy_file:
dummy_file.write("." * 127)
if dummy_filepath.read_text() != "":
pytest.skip("Size buffer for text files is too small.")
dummy_file.write("." * (65536 - 127))
if dummy_filepath.read_text() == "":
pytest.skip("Size buffer for text files is too big.")

filepath = tmp_path / "test.log"
logger.add(filepath, format="{message}", buffering=-1)
logger.debug("A short message.")
assert filepath.read_text() == ""
logger.debug("A long message" + "." * 65536)
assert filepath.read_text() != ""


def test_file_line_buffering(tmp_path):
filepath = tmp_path / "test.log"
logger.add(filepath, format=lambda _: "{message}", buffering=1)
logger.debug("Without newline")
assert filepath.read_text() == ""
logger.debug("With newline\n")
assert filepath.read_text() != ""


def test_invalid_function_kwargs():
Expand Down

0 comments on commit 5afeed8

Please sign in to comment.