From 6cec972cc7b3f51ecc22a6de8f4b636383f0f099 Mon Sep 17 00:00:00 2001 From: Benjamin Schubert Date: Wed, 16 Sep 2020 09:59:57 +0000 Subject: utils.py: remove 'is_single_threaded' method, we use it only in tests This moves it to tests with a simplified usage, since we don't use it anywhere else --- src/buildstream/testing/_fixtures.py | 28 +++++++++++++++++++++++----- src/buildstream/utils.py | 35 ----------------------------------- 2 files changed, 23 insertions(+), 40 deletions(-) (limited to 'src') diff --git a/src/buildstream/testing/_fixtures.py b/src/buildstream/testing/_fixtures.py index 520f68587..095a5f6f1 100644 --- a/src/buildstream/testing/_fixtures.py +++ b/src/buildstream/testing/_fixtures.py @@ -16,26 +16,44 @@ # pylint: disable=redefined-outer-name +import time + import psutil import pytest -from buildstream import node, utils, DownloadableFileSource +from buildstream import node, DownloadableFileSource + + +# Number of seconds to wait for background threads to exit. +_AWAIT_THREADS_TIMEOUT_SECONDS = 5 + + +def has_no_unexpected_background_threads(expected_num_threads): + # Use psutil as threading.active_count() doesn't include gRPC threads. + process = psutil.Process() + + wait = 0.1 + for _ in range(0, int(_AWAIT_THREADS_TIMEOUT_SECONDS / wait)): + if process.num_threads() == expected_num_threads: + return True + time.sleep(wait) + + return False @pytest.fixture(autouse=True, scope="session") def default_thread_number(): # xdist/execnet has its own helper thread. - # Ignore that for `utils._is_single_threaded` checks. - utils._INITIAL_NUM_THREADS_IN_MAIN_PROCESS = psutil.Process().num_threads() + return psutil.Process().num_threads() # Catch tests that don't shut down background threads, which could then lead # to other tests hanging when BuildStream uses fork(). @pytest.fixture(autouse=True) def thread_check(default_thread_number): - assert utils._is_single_threaded() + assert has_no_unexpected_background_threads(default_thread_number) yield - assert utils._is_single_threaded() + assert has_no_unexpected_background_threads(default_thread_number) # Reset global state in node.pyx to improve test isolation diff --git a/src/buildstream/utils.py b/src/buildstream/utils.py index 8e6a51273..04bbf261c 100644 --- a/src/buildstream/utils.py +++ b/src/buildstream/utils.py @@ -35,7 +35,6 @@ import subprocess from subprocess import TimeoutExpired import tempfile import threading -import time import datetime import itertools from contextlib import contextmanager @@ -62,16 +61,6 @@ BST_ARBITRARY_TIMESTAMP = calendar.timegm((2011, 11, 11, 11, 11, 11)) _ALIAS_SEPARATOR = ":" _URI_SCHEMES = ["http", "https", "ftp", "file", "git", "sftp", "ssh"] -# Main process pid -_MAIN_PID = os.getpid() - -# The number of threads in the main process at startup. -# This is 1 except for certain test environments (xdist/execnet). -_INITIAL_NUM_THREADS_IN_MAIN_PROCESS = 1 - -# Number of seconds to wait for background threads to exit. -_AWAIT_THREADS_TIMEOUT_SECONDS = 5 - # The process's file mode creation mask. # Impossible to retrieve without temporarily changing it on POSIX. _UMASK = os.umask(0o777) @@ -1600,30 +1589,6 @@ def _get_compression(tar): return "" -# _is_single_threaded() -# -# Return whether the current Process is single-threaded. Don't count threads -# in the main process that were created by a test environment (xdist/execnet) -# before BuildStream was executed. -# -def _is_single_threaded(): - # Use psutil as threading.active_count() doesn't include gRPC threads. - process = psutil.Process() - - if process.pid == _MAIN_PID: - expected_num_threads = _INITIAL_NUM_THREADS_IN_MAIN_PROCESS - else: - expected_num_threads = 1 - - # gRPC threads are not joined when shut down. Wait for them to exit. - wait = 0.1 - for _ in range(0, int(_AWAIT_THREADS_TIMEOUT_SECONDS / wait)): - if process.num_threads() == expected_num_threads: - return True - time.sleep(wait) - return False - - # _parse_version(): # # Args: -- cgit v1.2.1