Coverage for tests/unit_tests/managers/test_downloader.py: 100%
11 statements
« prev ^ index » next coverage.py v7.15.4, created at 2026-08-09 01:55 +0000
« prev ^ index » next coverage.py v7.15.4, created at 2026-08-09 01:55 +0000
1"""
2Tests of managers/downloader.py
3"""
5import gc
6import requests
7from pytest import MonkeyPatch, raises
8from apio.managers.downloader import FileDownloader
11def test_failed_construction_safe_finalizer(monkeypatch: MonkeyPatch):
12 """Tests that a FileDownloader whose construction failed, e.g. on a
13 connection timeout, does not raise from its __del__ finalizer. It used
14 to raise an AttributeError at garbage collection time, at an arbitrary
15 point of the program (pytest reports it as an 'unraisable exception'
16 error on an arbitrary test)."""
18 # -- Make requests.get raise a connect timeout, as when the server is
19 # -- unreachable. We mock it to avoid a dependency on real networking.
20 def mock_get(*_args, **_kwargs):
21 raise requests.exceptions.ConnectTimeout("test timeout")
23 monkeypatch.setattr(requests, "get", mock_get)
25 # -- Construct a downloader. The construction should fail before the
26 # -- request field is populated.
27 with raises(requests.exceptions.ConnectTimeout):
28 FileDownloader("http://localhost/no-such-file.tgz")
30 # -- Force the collection of the partially constructed object. With a
31 # -- broken finalizer, pytest flags an 'unraisable exception' error here.
32 gc.collect()