From c950d88204d78746653dcacd6ce8c619a99400ac Mon Sep 17 00:00:00 2001 From: Sergey B Kirpichev Date: Wed, 25 Jul 2018 19:05:19 +0300 Subject: [PATCH 1/4] Workaround cpu detection on Travis CI Closes #316 --- changelog/316.bugfix | 1 + xdist/plugin.py | 5 +++++ 2 files changed, 6 insertions(+) create mode 100644 changelog/316.bugfix diff --git a/changelog/316.bugfix b/changelog/316.bugfix new file mode 100644 index 0000000..4685b5a --- /dev/null +++ b/changelog/316.bugfix @@ -0,0 +1 @@ +Workaround cpu detection on Travis CI diff --git a/xdist/plugin.py b/xdist/plugin.py index 278b65d..881be4a 100644 --- a/xdist/plugin.py +++ b/xdist/plugin.py @@ -1,3 +1,5 @@ +import os + import py import pytest @@ -7,6 +9,9 @@ def parse_numprocesses(s): try: from os import sched_getaffinity except ImportError: + if os.environ.get('TRAVIS') == 'true': + # workaround https://bitbucket.org/pypy/pypy/issues/2375 + return 2 try: from os import cpu_count except ImportError: From d3ef33ab321d362a7a73fb2e3f3d1ee59476e9ab Mon Sep 17 00:00:00 2001 From: Sergey B Kirpichev Date: Thu, 26 Jul 2018 12:52:18 +0300 Subject: [PATCH 2/4] Refactor automatic cpu detection code to auto_detect_cpus() --- xdist/plugin.py | 42 +++++++++++++++++++++++------------------- 1 file changed, 23 insertions(+), 19 deletions(-) diff --git a/xdist/plugin.py b/xdist/plugin.py index 881be4a..1ad8a63 100644 --- a/xdist/plugin.py +++ b/xdist/plugin.py @@ -4,27 +4,31 @@ import py import pytest +def auto_detect_cpus(): + try: + from os import sched_getaffinity + except ImportError: + if os.environ.get('TRAVIS') == 'true': + # workaround https://bitbucket.org/pypy/pypy/issues/2375 + return 2 + try: + from os import cpu_count + except ImportError: + from multiprocessing import cpu_count + else: + def cpu_count(): + return len(sched_getaffinity(0)) + + try: + n = cpu_count() + except NotImplementedError: + return 1 + return n if n else 1 + + def parse_numprocesses(s): if s == 'auto': - try: - from os import sched_getaffinity - except ImportError: - if os.environ.get('TRAVIS') == 'true': - # workaround https://bitbucket.org/pypy/pypy/issues/2375 - return 2 - try: - from os import cpu_count - except ImportError: - from multiprocessing import cpu_count - else: - def cpu_count(): - return len(sched_getaffinity(0)) - - try: - n = cpu_count() - except NotImplementedError: - return 1 - return n if n else 1 + return auto_detect_cpus() else: return int(s) From 5c59a302775b1ae40afc6510fb30e72b95eb8092 Mon Sep 17 00:00:00 2001 From: Sergey B Kirpichev Date: Thu, 26 Jul 2018 12:56:10 +0300 Subject: [PATCH 3/4] Add coverage test for Travis CI workaround --- testing/test_plugin.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/testing/test_plugin.py b/testing/test_plugin.py index d5c32ba..77a6ca4 100644 --- a/testing/test_plugin.py +++ b/testing/test_plugin.py @@ -45,6 +45,11 @@ def test_auto_detect_cpus(testdir, monkeypatch): config = testdir.parseconfigure("-nauto") assert config.getoption('numprocesses') == 99 + if not hasattr(os, 'sched_getaffinity'): + monkeypatch.setenv('TRAVIS', 'true') + config = testdir.parseconfigure("-nauto") + assert config.getoption('numprocesses') == 2 + def test_boxed_with_collect_only(testdir): from xdist.plugin import pytest_cmdline_main as check_options From ee0754bc35628b754da3ca02f7520ef04d59c3e0 Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Fri, 27 Jul 2018 08:24:34 -0300 Subject: [PATCH 4/4] Improve test for Travis fallback --- testing/test_plugin.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/testing/test_plugin.py b/testing/test_plugin.py index 77a6ca4..7a4ac46 100644 --- a/testing/test_plugin.py +++ b/testing/test_plugin.py @@ -45,10 +45,10 @@ def test_auto_detect_cpus(testdir, monkeypatch): config = testdir.parseconfigure("-nauto") assert config.getoption('numprocesses') == 99 - if not hasattr(os, 'sched_getaffinity'): - monkeypatch.setenv('TRAVIS', 'true') - config = testdir.parseconfigure("-nauto") - assert config.getoption('numprocesses') == 2 + monkeypatch.delattr(os, 'sched_getaffinity', raising=False) + monkeypatch.setenv('TRAVIS', 'true') + config = testdir.parseconfigure("-nauto") + assert config.getoption('numprocesses') == 2 def test_boxed_with_collect_only(testdir):