Skip to content
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

Fix test issue where warning was being raised but not captured #151

Merged
merged 6 commits into from
Apr 8, 2024

Conversation

samaloney
Copy link
Contributor

So if you look at the test output the warning is raised but pytest.warn doesn't seem to capture it but recwarn does?

pytest  -k test_download_out_of_main_thread                                                                                                                                                                   1 ✘  parfive 
============================================================================================================================= test session starts ==============================================================================================================================
platform darwin -- Python 3.11.7, pytest-8.1.1, pluggy-1.4.0
rootdir: /Users/sm/Projects/parfive
configfile: setup.cfg
plugins: cov-5.0.0, socket-0.7.0, asyncio-0.23.6, localserver-0.8.1
asyncio: mode=Mode.STRICT
collected 60 items / 59 deselected / 1 selected

parfive/tests/test_downloader.py F                                                                                                                                                                                                                                       [100%]

=================================================================================================================================== FAILURES ===================================================================================================================================
_______________________________________________________________________________________________________________________ test_download_out_of_main_thread _______________________________________________________________________________________________________________________

httpserver = <ContentServer(<class 'pytest_localserver.http.ContentServer'>, started 6117765120)>, tmpdir = '/private/var/folders/h1/wy_cdhsj1c9_9pr0x9shjrcr0000gp/T/pytest-of-sm/pytest-111/test_download_out_of_main_thre0'

    @skip_windows
    def test_download_out_of_main_thread(httpserver, tmpdir):
        tmpdir = str(tmpdir)
        httpserver.serve_content(
            "SIMPLE  = T", headers={"Content-Disposition": "attachment; filename=testfile.fits"}
        )
        dl = Downloader()

        dl.enqueue_file(httpserver.url, path=Path(tmpdir), max_splits=None)

        thread = CustomThread(target=dl.download)
        thread.start()

>       with pytest.warns(
            UserWarning,
            match="This download has been started in a thread*.",
        ):
E       Failed: DID NOT WARN. No warnings of type (<class 'UserWarning'>,) were emitted.
E        Emitted warnings: [].

parfive/tests/test_downloader.py:545: Failed
----------------------------------------------------------------------------------------------------------------------------- Captured stderr call -----------------------------------------------------------------------------------------------------------------------------
Files Downloaded: 100%|██████████| 1/1 [00:00<00:00, 287.03file/s]
------------------------------------------------------------------------------------------------------------------------------ Captured log call -------------------------------------------------------------------------------------------------------------------------------
INFO     werkzeug:_internal.py:96 127.0.0.1 - - [04/Apr/2024 18:20:57] "GET / HTTP/1.1" 200 -
INFO     werkzeug:_internal.py:96 127.0.0.1 - - [04/Apr/2024 18:20:57] "GET / HTTP/1.1" 200 -
=============================================================================================================================== warnings summary ===============================================================================================================================
parfive/tests/test_downloader.py::test_download_out_of_main_thread
  /Users/sm/Projects/parfive/parfive/downloader.py:230: UserWarning: This download has been started in a thread which is not the main thread. You will not be able to interrupt the download.
    warnings.warn(

-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html
=========================================================================================================================== short test summary info ============================================================================================================================
FAILED parfive/tests/test_downloader.py::test_download_out_of_main_thread - Failed: DID NOT WARN. No warnings of type (<class 'UserWarning'>,) were emitted.
================================================================================================================= 1 failed, 59 deselected, 1 warning in 0.64s ================================================================================================================

Copy link

codecov bot commented Apr 4, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 90.36%. Comparing base (3b049c5) to head (15506e0).
Report is 9 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #151      +/-   ##
==========================================
+ Coverage   90.23%   90.36%   +0.13%     
==========================================
  Files           5        5              
  Lines         635      654      +19     
==========================================
+ Hits          573      591      +18     
- Misses         62       63       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@samaloney samaloney force-pushed the bugfix-outside-thread-test branch 5 times, most recently from 3562143 to af9ffb0 Compare April 4, 2024 18:17
@samaloney
Copy link
Contributor Author

So adding try finally block seemed to surface a part of the issue, it looks as if the expected error is being clobbered by a PytestUnraisableExceptionWarning warning. I tied to filter that warning but that didn't seem to work.

=============================== warnings summary ===============================
parfive/tests/test_downloader.py::test_changed_max_conn
parfive/tests/test_downloader.py::test_download_filename
parfive/tests/test_downloader.py::test_ftp_http
  /Users/runner/work/parfive/parfive/.tox/py38/lib/python3.8/site-packages/_pytest/unraisableexception.py:80: PytestUnraisableExceptionWarning: Exception ignored in: <function BaseEventLoop.__del__ at 0x10a6878b0>
  
  Traceback (most recent call last):
    File "/Users/runner/hostedtoolcache/Python/3.8.18/x64/lib/python3.8/asyncio/base_events.py", line 656, in __del__
      self.close()
    File "/Users/runner/hostedtoolcache/Python/3.8.18/x64/lib/python3.8/asyncio/unix_events.py", line 61, in close
      self.remove_signal_handler(sig)
    File "/Users/runner/hostedtoolcache/Python/3.8.18/x64/lib/python3.8/asyncio/unix_events.py", line 150, in remove_signal_handler
      signal.signal(sig, handler)
    File "/Users/runner/hostedtoolcache/Python/3.8.18/x64/lib/python3.8/signal.py", line 47, in signal
      handler = _signal.signal(_enum_to_int(signalnum), _enum_to_int(handler))
  ValueError: signal only works in main thread
  
    warnings.warn(pytest.PytestUnraisableExceptionWarning(msg))

parfive/tests/test_downloader.py::test_download_out_of_main_thread
  /Users/runner/work/parfive/parfive/parfive/downloader.py:230: UserWarning: This download has been started in a thread which is not the main thread. You will not be able to interrupt the download.
    warnings.warn(

-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html

---------- coverage: platform darwin, python 3.8.18-final-0 ----------
Coverage XML written to file /Users/runner/work/parfive/parfive/coverage.xml

=========================== short test summary info ============================
FAILED parfive/tests/test_downloader.py::test_download_out_of_main_thread - Failed: DID NOT WARN. No warnings of type (<class 'UserWarning'>,) were emitted.
 Emitted warnings: [].
================== 1 failed, 59 passed, 4 warnings in 18.04s ===================

@Cadair Cadair force-pushed the bugfix-outside-thread-test branch from 2d2e479 to f91b351 Compare April 8, 2024 08:54
@Cadair Cadair merged commit dcbfe9f into Cadair:main Apr 8, 2024
12 of 14 checks passed
@Cadair Cadair added the bug A report of or a fix for a bug or unwanted behaviour label Apr 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A report of or a fix for a bug or unwanted behaviour
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants