Skip to content

Commit

Permalink
Wait until all threads started by the test have finished
Browse files Browse the repository at this point in the history
  • Loading branch information
jitseniesen committed Apr 14, 2024
1 parent 609770c commit 5272878
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion spyder_unittest/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand All @@ -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()
Expand Down

0 comments on commit 5272878

Please sign in to comment.