Debian Codegraph @

Lib/test/test_concurrent_futures/test_init.py

raw
1 import contextlib
2 import logging
3 import queue
4 import time
5 import unittest
6 import sys
7 import io
8 from concurrent.futures._base import BrokenExecutor
9 from concurrent.futures.process import _check_system_limits
10
11 from logging.handlers import QueueHandler
12
13 from test import support
14
15 from .util import ExecutorMixin, create_executor_tests, setup_module
16
17
18 INITIALIZER_STATUS = 'uninitialized'
19
20 def init(x):
21 global INITIALIZER_STATUS
22 INITIALIZER_STATUS = x
23 # InterpreterPoolInitializerTest.test_initializer fails
24 # if we don't have a LOAD_GLOBAL. (It could be any global.)
25 # We will address this separately.
26 INITIALIZER_STATUS
27
28 def get_init_status():
29 return INITIALIZER_STATUS
30
31 def init_fail(log_queue=None):
32 if log_queue is not None:
33 logger = logging.getLogger('concurrent.futures')
34 logger.addHandler(QueueHandler(log_queue))
35 logger.setLevel('CRITICAL')
36 logger.propagate = False
37 time.sleep(0.1) # let some futures be scheduled
38 raise ValueError('error in initializer')
39
40
41 class InitializerMixin(ExecutorMixin):
42 worker_count = 2
43
44 def setUp(self):
45 global INITIALIZER_STATUS
46 INITIALIZER_STATUS = 'uninitialized'
47 self.executor_kwargs = dict(initializer=init,
48 initargs=('initialized',))
49 super().setUp()
50
51 def test_initializer(self):
52 futures = [self.executor.submit(get_init_status)
53 for _ in range(self.worker_count)]
54
55 for f in futures:
56 self.assertEqual(f.result(), 'initialized')
57
58
59 class FailingInitializerMixin(ExecutorMixin):
60 worker_count = 2
61
62 def setUp(self):
63 if hasattr(self, "ctx"):
64 # Pass a queue to redirect the child's logging output
65 self.mp_context = self.get_context()
66 self.log_queue = self.mp_context.Queue()
67 self.executor_kwargs = dict(initializer=init_fail,
68 initargs=(self.log_queue,))
69 else:
70 # In a thread pool, the child shares our logging setup
71 # (see _assert_logged())
72 self.mp_context = None
73 self.log_queue = None
74 self.executor_kwargs = dict(initializer=init_fail)
75 super().setUp()
76
77 def test_initializer(self):
78 with self._assert_logged('ValueError: error in initializer'):
79 try:
80 future = self.executor.submit(get_init_status)
81 except BrokenExecutor:
82 # Perhaps the executor is already broken
83 pass
84 else:
85 with self.assertRaises(BrokenExecutor):
86 future.result()
87
88 # At some point, the executor should break
89 for _ in support.sleeping_retry(support.SHORT_TIMEOUT,
90 "executor not broken"):
91 if self.executor._broken:
92 break
93
94 # ... and from this point submit() is guaranteed to fail
95 with self.assertRaises(BrokenExecutor):
96 self.executor.submit(get_init_status)
97
98 @contextlib.contextmanager
99 def _assert_logged(self, msg):
100 if self.log_queue is not None:
101 yield
102 output = []
103 try:
104 while True:
105 output.append(self.log_queue.get_nowait().getMessage())
106 except queue.Empty:
107 pass
108 else:
109 with self.assertLogs('concurrent.futures', 'CRITICAL') as cm:
110 yield
111 output = cm.output
112 self.assertTrue(any(msg in line for line in output),
113 output)
114
115
116 create_executor_tests(globals(), InitializerMixin)
117 create_executor_tests(globals(), FailingInitializerMixin)
118
119
120 @unittest.skipIf(sys.platform == "win32", "Resource Tracker doesn't run on Windows")
121 class FailingInitializerResourcesTest(unittest.TestCase):
122 """
123 Source: https://github.com/python/cpython/issues/104090
124 """
125
126 def _test(self, test_class):
127 try:
128 _check_system_limits()
129 except NotImplementedError:
130 self.skipTest("ProcessPoolExecutor unavailable on this system")
131
132 runner = unittest.TextTestRunner(stream=io.StringIO())
133 runner.run(test_class('test_initializer'))
134
135 # GH-104090:
136 # Stop resource tracker manually now, so we can verify there are not leaked resources by checking
137 # the process exit code
138 from multiprocessing.resource_tracker import _resource_tracker
139 _resource_tracker._stop()
140
141 self.assertEqual(_resource_tracker._exitcode, 0)
142
143 def test_spawn(self):
144 self._test(ProcessPoolSpawnFailingInitializerTest)
145
146 @support.skip_if_sanitizer("TSAN doesn't support threads after fork", thread=True)
147 def test_forkserver(self):
148 self._test(ProcessPoolForkserverFailingInitializerTest)
149
150
151 def setUpModule():
152 setup_module()
153
154
155 if __name__ == "__main__":
156 unittest.main()