summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJürg Billeter <j@bitron.ch>2019-09-02 09:47:05 +0200
committerJürg Billeter <j@bitron.ch>2019-09-03 11:46:29 +0200
commit3221f3511cbfe6a748b542627801acf8d0fcb379 (patch)
treeb472768f0bc8fa9a678ea77c1f74dfe3ff3960ea
parentb4823df1c25ad5afd8449594b3b15b5676d6b9a6 (diff)
downloadbuildstream-juerg/fork.tar.gz
tests: Catch tests that don't shut down background threadsjuerg/fork
-rw-r--r--src/buildstream/testing/_fixtures.py31
-rw-r--r--src/buildstream/testing/_sourcetests/conftest.py17
-rwxr-xr-xtests/conftest.py1
3 files changed, 49 insertions, 0 deletions
diff --git a/src/buildstream/testing/_fixtures.py b/src/buildstream/testing/_fixtures.py
new file mode 100644
index 000000000..862cebe87
--- /dev/null
+++ b/src/buildstream/testing/_fixtures.py
@@ -0,0 +1,31 @@
+#
+# Copyright (C) 2019 Bloomberg Finance LP
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License as published by the Free Software Foundation; either
+# version 2 of the License, or (at your option) any later version.
+#
+# This library is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this library. If not, see <http://www.gnu.org/licenses/>.
+
+import psutil
+import pytest
+
+from buildstream import utils
+
+# 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():
+ # 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()
+
+ yield
+ assert utils._is_single_threaded()
diff --git a/src/buildstream/testing/_sourcetests/conftest.py b/src/buildstream/testing/_sourcetests/conftest.py
new file mode 100644
index 000000000..f16c1e9ad
--- /dev/null
+++ b/src/buildstream/testing/_sourcetests/conftest.py
@@ -0,0 +1,17 @@
+#
+# Copyright (C) 2019 Bloomberg Finance LP
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License as published by the Free Software Foundation; either
+# version 2 of the License, or (at your option) any later version.
+#
+# This library is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this library. If not, see <http://www.gnu.org/licenses/>.
+
+from .._fixtures import thread_check # pylint: disable=unused-import
diff --git a/tests/conftest.py b/tests/conftest.py
index 7728fb5c8..0e39943d3 100755
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -23,6 +23,7 @@ import os
import pytest
from buildstream.testing import register_repo_kind, sourcetests_collection_hook
+from buildstream.testing._fixtures import thread_check # pylint: disable=unused-import
from buildstream.testing._forked import forked_run_report
from buildstream.testing.integration import integration_cache # pylint: disable=unused-import