diff --git a/changelog/316.bugfix b/changelog/316.bugfix new file mode 100644 index 00000000..4685b5a5 --- /dev/null +++ b/changelog/316.bugfix @@ -0,0 +1 @@ +Workaround cpu detection on Travis CI diff --git a/testing/test_plugin.py b/testing/test_plugin.py index d5c32ba1..7a4ac46f 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 + 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): from xdist.plugin import pytest_cmdline_main as check_options diff --git a/xdist/plugin.py b/xdist/plugin.py index 278b65dc..1ad8a637 100644 --- a/xdist/plugin.py +++ b/xdist/plugin.py @@ -1,25 +1,34 @@ +import os + import py import pytest -def parse_numprocesses(s): - if s == 'auto': +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 sched_getaffinity + from os import cpu_count except ImportError: - try: - from os import cpu_count - except ImportError: - from multiprocessing import cpu_count - else: - def cpu_count(): - return len(sched_getaffinity(0)) + 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 + try: + n = cpu_count() + except NotImplementedError: + return 1 + return n if n else 1 + + +def parse_numprocesses(s): + if s == 'auto': + return auto_detect_cpus() else: return int(s)