Repository navigation
Remove and rewrite the core, executor, reader and init filesystem tests (GH-1328) #1430
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
b4e2bbd
57f2814
eb64d92
0f5f031
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,6 +19,18 @@ class _DummyFileSystem: | |
| pass | ||
|
|
||
|
|
||
| def _warnings(caplog): | ||
| """Return the captured records of level ``WARNING`` or higher. | ||
|
|
||
| Args: | ||
| caplog: The pytest caplog fixture. | ||
|
|
||
| Returns: | ||
| The records, of any logger. | ||
| """ | ||
| return [record for record in caplog.records if record.levelno >= logging.WARNING] | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def registry_state(): | ||
| registry = dict(_registry) | ||
|
|
@@ -41,15 +53,19 @@ def test_register_s3_filesystem(registry_state): | |
|
|
||
| def test_register_s3_filesystem_overwrites_existing_registration(registry_state, caplog): | ||
| fsspec.register_implementation("s3", _DummyFileSystem, clobber=True) | ||
| # Only the s3 registration is replaced. | ||
| fsspec.register_implementation("s3a", S3FileSystem, clobber=True) | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Independent review, finding 5 (medium) — fixed in part. With |
||
|
|
||
| with caplog.at_level(logging.WARNING, logger="pyathena.filesystem"): | ||
| register_s3_filesystem() | ||
| # An explicitly registered filesystem class is overwritten with a warning. | ||
| assert fsspec.registry["s3"] is S3FileSystem | ||
| assert "will be overwritten" in caplog.text | ||
| assert [(r.name, r.levelno) for r in _warnings(caplog)] == [ | ||
| ("pyathena.filesystem", logging.WARNING) | ||
| ] | ||
|
|
||
| # Re-registering PyAthena's own filesystem does not warn. | ||
| caplog.clear() | ||
| with caplog.at_level(logging.WARNING, logger="pyathena.filesystem"): | ||
| register_s3_filesystem() | ||
| assert not caplog.text | ||
| assert not _warnings(caplog) | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Round 2: the body now states what both assertions check (one |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -28,12 +28,18 @@ def __init__(self, data, short=False, error_at=None, gate=None): | |
| self.gated_read_started = threading.Event() | ||
| self.reads = [] | ||
| self.closes = 0 | ||
| # The offsets of the reads in the order they ended, and "close" when closed. | ||
| self.events = [] | ||
| # Set when the file is closed, and by signal_close_wait() when close() | ||
| # starts to wait for the reads. | ||
| self.close_progressed = threading.Event() | ||
|
|
||
| def read_range(self, offset, size): | ||
| self.reads.append((offset, size)) | ||
| if self._gate is not None and offset > 0: | ||
| self.gated_read_started.set() | ||
| assert self._gate.wait(EVENT_TIMEOUT) | ||
| self.events.append(offset) | ||
| if offset == self._error_at: | ||
| raise OSError("read failed") | ||
| return self._data[offset : offset + size - (1 if self._short else 0)] | ||
|
|
@@ -43,7 +49,59 @@ def closed(self): | |
| return self.closes > 0 | ||
|
|
||
| def close(self): | ||
| self.events.append("close") | ||
| self.closes += 1 | ||
| self.close_progressed.set() | ||
|
|
||
| def assert_closed_after_reads(self, stream): | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Self-review round 1 (implementation behavior, public contracts, simplicity, regression coverage), plus FINDINGS, repaired in
Re-checked at
Deferred, with reasons:
|
||
| """Check that closing the stream while the gated reads block closes the file last. | ||
|
|
||
| The stream is closed in a thread, and the reads are released once the | ||
| close waits for them (see :meth:`signal_close_wait`) or, if it does not | ||
| wait, has closed the file, which then precedes their end. | ||
|
|
||
| Args: | ||
| stream: The stream over the file. | ||
| """ | ||
| errors = [] | ||
|
|
||
| def close(): | ||
| try: | ||
| stream.close() | ||
| except BaseException as e: | ||
| errors.append(e) | ||
|
|
||
| closer = threading.Thread(target=close, daemon=True) | ||
| closer.start() | ||
| assert self.close_progressed.wait(EVENT_TIMEOUT), ( | ||
| "close() neither waited for the reads nor closed the file" | ||
| ) | ||
| self._gate.set() | ||
| closer.join(EVENT_TIMEOUT) | ||
| assert not closer.is_alive() | ||
| assert not errors | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Independent review (relayed): Codex CLI 0.169.0, model Finding 4 (medium) — fixed. An exception of the closing thread left the test green (only a thread warning). Confirmed: with
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Independent follow-up (relayed): Codex CLI 0.169.0, model |
||
| # Every read that started ended before the file was closed. | ||
| assert self.events[-1] == "close" | ||
| assert len(self.events) == len(self.reads) + 1 | ||
|
|
||
| def signal_close_wait(self, monkeypatch, interrupts=0): | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Round 2: the reader claims were measured at |
||
| """Set ``close_progressed`` when ``ReadAheadStream.close()`` starts to wait for the reads. | ||
|
|
||
| Args: | ||
| monkeypatch: The pytest monkeypatch fixture. | ||
| interrupts: The number of first waits that raise ``KeyboardInterrupt``. | ||
| """ | ||
| remaining = interrupts | ||
|
|
||
| def signalling_wait(futures, *args, **kwargs): | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Independent review, finding 1 (medium) — rejected. When Finding 3 (medium) — deferred, as the PR body states. A correct |
||
| nonlocal remaining | ||
| if remaining: | ||
| remaining -= 1 | ||
| raise KeyboardInterrupt | ||
| self.close_progressed.set() | ||
| return wait(futures, *args, **kwargs) | ||
|
|
||
| monkeypatch.setattr("pyathena.filesystem.reader.wait", signalling_wait) | ||
|
|
||
| def open_stream(self, **kwargs): | ||
| """Open a stream over the file that closes it.""" | ||
|
|
@@ -79,6 +137,18 @@ def _new_future(self): | |
| return _InterruptedFuture() | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def gate(): | ||
| """An event that blocks the gated reads of a ``_RecordingRangeFile`` until set. | ||
|
|
||
| It is set at teardown, so that a test that fails before releasing the reads | ||
| does not leave them blocked. | ||
| """ | ||
| gate = threading.Event() | ||
| yield gate | ||
| gate.set() | ||
|
|
||
|
|
||
| class TestReadAheadStream: | ||
| """Tests for reading a file sequentially with parallel range reads.""" | ||
|
|
||
|
|
@@ -178,96 +248,47 @@ def test_interrupted_wait(self, monkeypatch): | |
| assert sorted(file.reads) == [(0, 10), (10, 10), (20, 10)] | ||
| stream.close() | ||
|
|
||
| def test_interrupted_close(self, monkeypatch): | ||
| def test_interrupted_close(self, monkeypatch, gate): | ||
| """An interrupted close leaves the file open, and closing again waits for the | ||
| running reads before closing the file.""" | ||
| gate = threading.Event() | ||
| file = _RecordingRangeFile(bytes(100), gate=gate) | ||
| stream = file.open_stream(part_size=10, max_parts=2) | ||
| interrupts = [KeyboardInterrupt()] | ||
|
|
||
| def interrupted_wait(futures, *args, **kwargs): | ||
| if interrupts: | ||
| raise interrupts.pop() | ||
| return wait(futures, *args, **kwargs) | ||
|
|
||
| monkeypatch.setattr("pyathena.filesystem.reader.wait", interrupted_wait) | ||
| closer = threading.Thread(target=stream.close, daemon=True) | ||
| try: | ||
| assert stream.read(10) == bytes(10) | ||
| assert file.gated_read_started.wait(EVENT_TIMEOUT) | ||
| with pytest.raises(KeyboardInterrupt): | ||
| stream.close() | ||
| assert not file.closed | ||
| assert not stream.closed | ||
| # The stream reads nothing more once closing started. | ||
| with pytest.raises(ValueError, match="I/O operation on closed file"): | ||
| stream.read(10) | ||
| closer.start() | ||
| closer.join(0.2) | ||
| # A read of a later part is still blocked, so the retried close has not | ||
| # closed the file. | ||
| assert closer.is_alive() | ||
| assert not file.closed | ||
| finally: | ||
| gate.set() | ||
| if closer.is_alive(): | ||
| closer.join(EVENT_TIMEOUT) | ||
| assert file.closed | ||
| file.signal_close_wait(monkeypatch, interrupts=1) | ||
| assert stream.read(10) == bytes(10) | ||
| assert file.gated_read_started.wait(EVENT_TIMEOUT) | ||
| with pytest.raises(KeyboardInterrupt): | ||
| stream.close() | ||
| assert not file.closed | ||
| assert not stream.closed | ||
| # The stream reads nothing more once closing started. | ||
| with pytest.raises(ValueError, match="I/O operation on closed file"): | ||
| stream.read(10) | ||
| file.assert_closed_after_reads(stream) | ||
| assert stream.closed | ||
|
|
||
| def test_close_with_queued_reads(self, monkeypatch): | ||
| def test_close_with_queued_reads(self, monkeypatch, gate): | ||
| """Closing cancels a part that no worker has started and does not wait for it.""" | ||
| shut_down = threading.Event() | ||
|
|
||
| class Executor(ThreadPoolExecutor): | ||
| def shutdown(self, wait=True, *, cancel_futures=False): | ||
| super().shutdown(wait=wait, cancel_futures=cancel_futures) | ||
| shut_down.set() | ||
|
|
||
| monkeypatch.setattr( | ||
| "pyathena.filesystem.reader.ThreadPoolExecutor", | ||
| lambda max_workers: Executor(max_workers=1), | ||
| lambda max_workers: ThreadPoolExecutor(max_workers=1), | ||
| ) | ||
| gate = threading.Event() | ||
| file = _RecordingRangeFile(bytes(100), gate=gate) | ||
| stream = file.open_stream(part_size=10, max_parts=2) | ||
| closer = threading.Thread(target=stream.close, daemon=True) | ||
| try: | ||
| # The one worker blocks in the read of the second part, and the third waits. | ||
| assert stream.read(10) == bytes(10) | ||
| assert file.gated_read_started.wait(EVENT_TIMEOUT) | ||
| closer.start() | ||
| # The queued third part is cancelled, and close() waits for the second. | ||
| assert shut_down.wait(EVENT_TIMEOUT) | ||
| closer.join(0.2) | ||
| assert closer.is_alive() | ||
| finally: | ||
| gate.set() | ||
| closer.join(EVENT_TIMEOUT) | ||
| assert not closer.is_alive() | ||
| assert file.closed | ||
| file.signal_close_wait(monkeypatch) | ||
| # The one worker blocks in the read of the second part, and the third waits. | ||
| assert stream.read(10) == bytes(10) | ||
| assert file.gated_read_started.wait(EVENT_TIMEOUT) | ||
| file.assert_closed_after_reads(stream) | ||
| assert (20, 10) not in file.reads | ||
|
|
||
| def test_close_waits_for_running_reads(self): | ||
| def test_close_waits_for_running_reads(self, monkeypatch, gate): | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Independent review, finding 2 (medium) — deferred, as the PR body already states. A close that waits for only some reads can pass. Measured: Finding 9 (low) — kept. |
||
| """Closing waits for the reads in flight before closing the file.""" | ||
| gate = threading.Event() | ||
| file = _RecordingRangeFile(bytes(100), gate=gate) | ||
| stream = file.open_stream(part_size=10, max_parts=2) | ||
| closer = threading.Thread(target=stream.close) | ||
| try: | ||
| assert stream.read(10) == bytes(10) | ||
| assert file.gated_read_started.wait(EVENT_TIMEOUT) | ||
| closer.start() | ||
| closer.join(0.2) | ||
| # A read of a later part is still blocked, so close() has not returned. | ||
| assert closer.is_alive() | ||
| assert not file.closed | ||
| finally: | ||
| gate.set() | ||
| if closer.is_alive(): | ||
| closer.join(EVENT_TIMEOUT) | ||
| assert file.closed | ||
| file.signal_close_wait(monkeypatch) | ||
| assert stream.read(10) == bytes(10) | ||
| assert file.gated_read_started.wait(EVENT_TIMEOUT) | ||
| file.assert_closed_after_reads(stream) | ||
|
|
||
| def test_csv(self): | ||
| """pyarrow reads a CSV through the stream with its own block size.""" | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Round 1 (continued): the registration test now uses one filter for both checks. The first registration produces exactly one record, a
WARNINGofpyathena.filesystem. Re-registering produces noWARNINGor higher from any logger.Mutations at
eb64d92d:info), a warning also for PyAthena's own class, and the warning frompyathena.filesystem.s3.