|
| 1 | +"""Tests for forkserver preload functionality.""" |
| 2 | + |
| 3 | +import multiprocessing |
| 4 | +import multiprocessing.forkserver |
| 5 | +import sys |
| 6 | +import unittest |
| 7 | + |
| 8 | + |
| 9 | +@unittest.skipIf(sys.platform == "win32", "forkserver not available on Windows") |
| 10 | +class TestForkserverPreload(unittest.TestCase): |
| 11 | + """Tests for forkserver preload functionality.""" |
| 12 | + |
| 13 | + def setUp(self): |
| 14 | + self.ctx = multiprocessing.get_context('forkserver') |
| 15 | + # Ensure clean state for each test |
| 16 | + multiprocessing.forkserver._forkserver._stop() |
| 17 | + |
| 18 | + def tearDown(self): |
| 19 | + # Clean up after tests |
| 20 | + multiprocessing.forkserver._forkserver._stop() |
| 21 | + self.ctx.set_forkserver_preload([]) |
| 22 | + |
| 23 | + @staticmethod |
| 24 | + def _send_value(conn, value): |
| 25 | + """Helper to send a value through a connection.""" |
| 26 | + conn.send(value) |
| 27 | + |
| 28 | + def test_preload_raise_exceptions_false_default(self): |
| 29 | + """Test that invalid modules are silently ignored by default.""" |
| 30 | + # With raise_exceptions=False (default), invalid module is ignored |
| 31 | + self.ctx.set_forkserver_preload(['nonexistent_module_xyz']) |
| 32 | + |
| 33 | + # Should be able to start a process without errors |
| 34 | + r, w = self.ctx.Pipe(duplex=False) |
| 35 | + p = self.ctx.Process(target=self._send_value, args=(w, 42)) |
| 36 | + p.start() |
| 37 | + w.close() |
| 38 | + result = r.recv() |
| 39 | + r.close() |
| 40 | + p.join() |
| 41 | + |
| 42 | + self.assertEqual(result, 42) |
| 43 | + self.assertEqual(p.exitcode, 0) |
| 44 | + |
| 45 | + def test_preload_raise_exceptions_false_explicit(self): |
| 46 | + """Test that invalid modules are silently ignored with raise_exceptions=False.""" |
| 47 | + self.ctx.set_forkserver_preload(['nonexistent_module_xyz'], raise_exceptions=False) |
| 48 | + |
| 49 | + # Should be able to start a process without errors |
| 50 | + r, w = self.ctx.Pipe(duplex=False) |
| 51 | + p = self.ctx.Process(target=self._send_value, args=(w, 99)) |
| 52 | + p.start() |
| 53 | + w.close() |
| 54 | + result = r.recv() |
| 55 | + r.close() |
| 56 | + p.join() |
| 57 | + |
| 58 | + self.assertEqual(result, 99) |
| 59 | + self.assertEqual(p.exitcode, 0) |
| 60 | + |
| 61 | + def test_preload_raise_exceptions_true_breaks_context(self): |
| 62 | + """Test that invalid modules with raise_exceptions=True breaks the forkserver.""" |
| 63 | + self.ctx.set_forkserver_preload(['nonexistent_module_xyz'], raise_exceptions=True) |
| 64 | + |
| 65 | + # The forkserver should fail to start when it tries to import |
| 66 | + # The exception is raised during p.start() when trying to communicate |
| 67 | + # with the failed forkserver process |
| 68 | + r, w = self.ctx.Pipe(duplex=False) |
| 69 | + try: |
| 70 | + p = self.ctx.Process(target=self._send_value, args=(w, 42)) |
| 71 | + with self.assertRaises((EOFError, ConnectionError, BrokenPipeError)): |
| 72 | + p.start() # Exception raised here |
| 73 | + finally: |
| 74 | + # Ensure pipes are closed even if exception is raised |
| 75 | + w.close() |
| 76 | + r.close() |
| 77 | + |
| 78 | + def test_preload_valid_modules_with_raise_exceptions(self): |
| 79 | + """Test that valid modules work fine with raise_exceptions=True.""" |
| 80 | + # Valid modules should work even with raise_exceptions=True |
| 81 | + self.ctx.set_forkserver_preload(['os', 'sys'], raise_exceptions=True) |
| 82 | + |
| 83 | + r, w = self.ctx.Pipe(duplex=False) |
| 84 | + p = self.ctx.Process(target=self._send_value, args=(w, 'success')) |
| 85 | + p.start() |
| 86 | + w.close() |
| 87 | + result = r.recv() |
| 88 | + r.close() |
| 89 | + p.join() |
| 90 | + |
| 91 | + self.assertEqual(result, 'success') |
| 92 | + self.assertEqual(p.exitcode, 0) |
| 93 | + |
| 94 | + |
| 95 | +if __name__ == '__main__': |
| 96 | + unittest.main() |
0 commit comments