Skip to content

Commit 1509a7a

Browse files
committed
Parametrize tests for threaded writer so both cases are tested
1 parent 424a5b9 commit 1509a7a

File tree

2 files changed

+26
-10
lines changed

2 files changed

+26
-10
lines changed

src/isal/igzip_threaded.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ def __init__(self,
228228
self.output_worker = threading.Thread(
229229
target=self._compress_and_write)
230230
else:
231-
raise ValueError(f"threads should be 1 or greater, got {threads}")
231+
raise ValueError(f"threads should be at least 1, got {threads}")
232232
self.threads = threads
233233
self.index = 0
234234
self._crc = 0
@@ -369,6 +369,8 @@ def _compress_and_write(self):
369369
data, zdict = in_queue.get(timeout=0.05)
370370
except queue.Empty:
371371
if not self.running:
372+
self._crc = total_crc
373+
self._size = size
372374
return
373375
continue
374376
try:

tests/test_igzip_threaded.py

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import gzip
99
import io
10+
import itertools
1011
import random
1112
import tempfile
1213
from pathlib import Path
@@ -26,10 +27,11 @@ def test_threaded_read():
2627
assert thread_data == data
2728

2829

29-
@pytest.mark.parametrize("mode", ["wb", "wt"])
30-
def test_threaded_write(mode):
30+
@pytest.mark.parametrize(["mode", "threads"],
31+
itertools.product(["wb", "wt"], [1, 3]))
32+
def test_threaded_write(mode, threads):
3133
with tempfile.NamedTemporaryFile("wb", delete=False) as tmp:
32-
with igzip_threaded.open(tmp, mode, threads=-1) as out_file:
34+
with igzip_threaded.open(tmp, mode, threads=threads) as out_file:
3335
gzip_open_mode = "rb" if "b" in mode else "rt"
3436
with gzip.open(TEST_FILE, gzip_open_mode) as in_file:
3537
while True:
@@ -74,11 +76,13 @@ def test_threaded_read_error():
7476

7577

7678
@pytest.mark.timeout(5)
77-
def test_threaded_write_error():
79+
@pytest.mark.parametrize("threads", [1, 3])
80+
def test_threaded_write_error(threads):
7881
# parallel_deflate_and_crc method is called in a worker thread.
7982
with pytest.raises(OverflowError) as error:
8083
with igzip_threaded.open(
81-
io.BytesIO(), "wb", compresslevel=3) as writer:
84+
io.BytesIO(), "wb", compresslevel=3, threads=threads
85+
) as writer:
8286
writer.write(random.randbytes(1024 * 1024 * 50))
8387
error.match("Compressed output exceeds buffer size")
8488

@@ -92,8 +96,10 @@ def test_close_reader():
9296
f.close()
9397

9498

95-
def test_close_writer():
96-
f = igzip_threaded._ThreadedGzipWriter(io.BytesIO())
99+
@pytest.mark.parametrize("threads", [1, 3])
100+
def test_close_writer(threads):
101+
f = igzip_threaded._ThreadedGzipWriter(
102+
io.BytesIO(), threads=threads)
97103
f.close()
98104
assert f.closed
99105
# Make sure double closing does not raise errors
@@ -117,6 +123,13 @@ def test_writer_wrong_level():
117123
error.match("42")
118124

119125

126+
def test_writer_too_low_threads():
127+
with pytest.raises(ValueError) as error:
128+
igzip_threaded._ThreadedGzipWriter(io.BytesIO(), threads=0)
129+
error.match("threads")
130+
error.match("at least 1")
131+
132+
120133
def test_reader_read_after_close():
121134
with open(TEST_FILE, "rb") as test_f:
122135
f = igzip_threaded._ThreadedGzipReader(test_f)
@@ -126,8 +139,9 @@ def test_reader_read_after_close():
126139
error.match("closed")
127140

128141

129-
def test_writer_write_after_close():
130-
f = igzip_threaded._ThreadedGzipWriter(io.BytesIO())
142+
@pytest.mark.parametrize("threads", [1, 3])
143+
def test_writer_write_after_close(threads):
144+
f = igzip_threaded._ThreadedGzipWriter(io.BytesIO(), threads=threads)
131145
f.close()
132146
with pytest.raises(ValueError) as error:
133147
f.write(b"abc")

0 commit comments

Comments
 (0)