Skip to content

Commit 454a452

Browse files
committed
Add a test for correctly flushing data to disk
1 parent 4f7a683 commit 454a452

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

tests/test_gzip_ng_threaded.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import subprocess
1313
import sys
1414
import tempfile
15+
import zlib
1516
from pathlib import Path
1617

1718
import pytest
@@ -234,15 +235,29 @@ def test_threaded_program_can_exit_on_error(tmp_path, mode, threads):
234235

235236
@pytest.mark.parametrize("threads", [1, 2])
236237
def test_flush(tmp_path, threads):
238+
empty_block_end = b"\x00\x00\xff\xff"
239+
compressobj = zlib.compressobj(wbits=-15)
240+
deflate_last_block = compressobj.compress(b"") + compressobj.flush()
237241
test_file = tmp_path / "output.gz"
238242
with gzip_ng_threaded.open(test_file, "wb", threads=threads) as f:
239243
f.write(b"1")
240244
f.flush()
241-
assert gzip.decompress(test_file.read_bytes()) == b"1"
245+
data = test_file.read_bytes()
246+
assert data[-4:] == empty_block_end
247+
# Cut off gzip header and end data with an explicit last block to
248+
# test if the data was compressed correctly.
249+
deflate_block = data[10:] + deflate_last_block
250+
assert zlib.decompress(deflate_block, wbits=-15) == b"1"
242251
f.write(b"2")
243252
f.flush()
244-
assert gzip.decompress(test_file.read_bytes()) == b"12"
253+
data = test_file.read_bytes()
254+
assert data[-4:] == empty_block_end
255+
deflate_block = data[10:] + deflate_last_block
256+
assert zlib.decompress(deflate_block, wbits=-15) == b"12"
245257
f.write(b"3")
246258
f.flush()
247-
assert gzip.decompress(test_file.read_bytes()) == b"123"
259+
data = test_file.read_bytes()
260+
assert data[-4:] == empty_block_end
261+
deflate_block = data[10:] + deflate_last_block
262+
assert zlib.decompress(deflate_block, wbits=-15) == b"123"
248263
assert gzip.decompress(test_file.read_bytes()) == b"123"

0 commit comments

Comments
 (0)