bpo-40692: Run more test_concurrent_futures tests (GH-20239)

In the case of multiprocessing.synchronize() being missing, the
test_concurrent_futures test suite now skips only the tests that
require multiprocessing.synchronize().

Validate that multiprocessing.synchronize exists as part of
_check_system_limits(), allowing ProcessPoolExecutor to raise
NotImplementedError during __init__, rather than crashing with
ImportError during __init__ when creating a lock imported from
multiprocessing.synchronize.

Use _check_system_limits() to disable tests of
ProcessPoolExecutor on systems without multiprocessing.synchronize.

Running the test suite without multiprocessing.synchronize reveals
that Lib/compileall.py crashes when it uses a ProcessPoolExecutor.
Therefore, change Lib/compileall.py to call _check_system_limits()
before creating the ProcessPoolExecutor.

Note that both Lib/compileall.py and Lib/test/test_compileall.py
were attempting to sanity-check ProcessPoolExecutor by expecting
ImportError. In multiprocessing.resource_tracker, sem_unlink() is also absent
on platforms where POSIX semaphores aren't available. Avoid using
sem_unlink() if it, too, does not exist.

Co-authored-by: Pablo Galindo <Pablogsal@gmail.com>
This commit is contained in:
Asheesh Laroia 2021-02-07 19:15:51 -08:00 committed by GitHub
parent 30a8b28396
commit bf2e7e55d7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 49 additions and 10 deletions

View file

@ -16,10 +16,14 @@ import time
import unittest
from unittest import mock, skipUnless
from concurrent.futures import ProcessPoolExecutor
try:
from concurrent.futures import ProcessPoolExecutor
# compileall relies on ProcessPoolExecutor if ProcessPoolExecutor exists
# and it can function.
from concurrent.futures.process import _check_system_limits
_check_system_limits()
_have_multiprocessing = True
except ImportError:
except NotImplementedError:
_have_multiprocessing = False
from test import support
@ -188,6 +192,7 @@ class CompileallTestsBase:
self.assertRegex(line, r'Listing ([^WindowsPath|PosixPath].*)')
self.assertTrue(os.path.isfile(self.bc_path))
@skipUnless(_have_multiprocessing, "requires multiprocessing")
@mock.patch('concurrent.futures.ProcessPoolExecutor')
def test_compile_pool_called(self, pool_mock):
compileall.compile_dir(self.directory, quiet=True, workers=5)
@ -198,11 +203,13 @@ class CompileallTestsBase:
"workers must be greater or equal to 0"):
compileall.compile_dir(self.directory, workers=-1)
@skipUnless(_have_multiprocessing, "requires multiprocessing")
@mock.patch('concurrent.futures.ProcessPoolExecutor')
def test_compile_workers_cpu_count(self, pool_mock):
compileall.compile_dir(self.directory, quiet=True, workers=0)
self.assertEqual(pool_mock.call_args[1]['max_workers'], None)
@skipUnless(_have_multiprocessing, "requires multiprocessing")
@mock.patch('concurrent.futures.ProcessPoolExecutor')
@mock.patch('compileall.compile_file')
def test_compile_one_worker(self, compile_file_mock, pool_mock):