From 527287826bbe678f87de81a4d2f07d060f524ba5 Mon Sep 17 00:00:00 2001 From: Jitse Niesen Date: Sun, 14 Apr 2024 18:43:04 +0100 Subject: [PATCH] Wait until all threads started by the test have finished --- spyder_unittest/tests/conftest.py | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/spyder_unittest/tests/conftest.py b/spyder_unittest/tests/conftest.py index 3207e8a..11c954c 100644 --- a/spyder_unittest/tests/conftest.py +++ b/spyder_unittest/tests/conftest.py @@ -10,6 +10,10 @@ available for integration tests. """ +# Standard library import +import os +import threading + # Third-party imports from qtpy.QtWidgets import QApplication import pytest @@ -24,11 +28,15 @@ from spyder.app import start from spyder.config.manager import CONF +# Time to wait until the IPython console is ready to receive input +# (in milliseconds) +SHELL_TIMEOUT = 40000 if os.name == 'nt' else 20000 + SPYDER6 = spyder_version_info[0] == 6 @pytest.fixture -def main_window(monkeypatch): +def main_window(monkeypatch, qtbot): """Main Window fixture""" if not SPYDER6: @@ -42,10 +50,29 @@ def main_window(monkeypatch): # Start the window window = start.main() + qtbot.addWidget(window) QApplication.processEvents() + # Store all current threads + if os.name != 'nt': + # _DummyThread are created if current_thread() is called from them. + # They will always leak (From python doc) so we ignore them. + init_threads = [ + repr(thread) for thread in threading.enumerate() + if not isinstance(thread, threading._DummyThread)] + + # Yield so that test can run yield window + # Wait until all threads started by the test have finished + def threads_condition(): + threads = [ + thread for thread in threading.enumerate() + if not isinstance(thread, threading._DummyThread)] + return (len(init_threads) >= len(threads)) + + qtbot.waitUntil(threads_condition, timeout=SHELL_TIMEOUT) + # Close main window window.closing(close_immediately=True) window.close()