|
11 | 11 | import sys
|
12 | 12 | import threading
|
13 | 13 | import unittest
|
14 |
| -import time |
15 | 14 | from unittest import mock
|
16 | 15 | import warnings
|
17 |
| -import multiprocessing |
18 | 16 | from test.support import os_helper
|
19 | 17 | from test.support import socket_helper
|
20 |
| -from test.support import wait_process |
21 |
| -from test.support import hashlib_helper |
22 | 18 |
|
23 | 19 | if sys.platform == 'win32':
|
24 | 20 | raise unittest.SkipTest('UNIX only')
|
@@ -1871,100 +1867,5 @@ async def runner():
|
1871 | 1867 | wsock.close()
|
1872 | 1868 |
|
1873 | 1869 |
|
1874 |
| -@unittest.skipUnless(hasattr(os, 'fork'), 'requires os.fork()') |
1875 |
| -class TestFork(unittest.IsolatedAsyncioTestCase): |
1876 |
| - |
1877 |
| - async def test_fork_not_share_event_loop(self): |
1878 |
| - # The forked process should not share the event loop with the parent |
1879 |
| - loop = asyncio.get_running_loop() |
1880 |
| - r, w = os.pipe() |
1881 |
| - self.addCleanup(os.close, r) |
1882 |
| - self.addCleanup(os.close, w) |
1883 |
| - pid = os.fork() |
1884 |
| - if pid == 0: |
1885 |
| - # child |
1886 |
| - try: |
1887 |
| - loop = asyncio.get_event_loop_policy().get_event_loop() |
1888 |
| - os.write(w, str(id(loop)).encode()) |
1889 |
| - finally: |
1890 |
| - os._exit(0) |
1891 |
| - else: |
1892 |
| - # parent |
1893 |
| - child_loop = int(os.read(r, 100).decode()) |
1894 |
| - self.assertNotEqual(child_loop, id(loop)) |
1895 |
| - wait_process(pid, exitcode=0) |
1896 |
| - |
1897 |
| - @hashlib_helper.requires_hashdigest('md5') |
1898 |
| - def test_fork_signal_handling(self): |
1899 |
| - # Sending signal to the forked process should not affect the parent |
1900 |
| - # process |
1901 |
| - ctx = multiprocessing.get_context('fork') |
1902 |
| - manager = ctx.Manager() |
1903 |
| - self.addCleanup(manager.shutdown) |
1904 |
| - child_started = manager.Event() |
1905 |
| - child_handled = manager.Event() |
1906 |
| - parent_handled = manager.Event() |
1907 |
| - |
1908 |
| - def child_main(): |
1909 |
| - signal.signal(signal.SIGTERM, lambda *args: child_handled.set()) |
1910 |
| - child_started.set() |
1911 |
| - time.sleep(1) |
1912 |
| - |
1913 |
| - async def main(): |
1914 |
| - loop = asyncio.get_running_loop() |
1915 |
| - loop.add_signal_handler(signal.SIGTERM, lambda *args: parent_handled.set()) |
1916 |
| - |
1917 |
| - process = ctx.Process(target=child_main) |
1918 |
| - process.start() |
1919 |
| - child_started.wait() |
1920 |
| - os.kill(process.pid, signal.SIGTERM) |
1921 |
| - process.join() |
1922 |
| - |
1923 |
| - async def func(): |
1924 |
| - await asyncio.sleep(0.1) |
1925 |
| - return 42 |
1926 |
| - |
1927 |
| - # Test parent's loop is still functional |
1928 |
| - self.assertEqual(await asyncio.create_task(func()), 42) |
1929 |
| - |
1930 |
| - asyncio.run(main()) |
1931 |
| - |
1932 |
| - self.assertFalse(parent_handled.is_set()) |
1933 |
| - self.assertTrue(child_handled.is_set()) |
1934 |
| - |
1935 |
| - @hashlib_helper.requires_hashdigest('md5') |
1936 |
| - def test_fork_asyncio_run(self): |
1937 |
| - ctx = multiprocessing.get_context('fork') |
1938 |
| - manager = ctx.Manager() |
1939 |
| - self.addCleanup(manager.shutdown) |
1940 |
| - result = manager.Value('i', 0) |
1941 |
| - |
1942 |
| - async def child_main(): |
1943 |
| - await asyncio.sleep(0.1) |
1944 |
| - result.value = 42 |
1945 |
| - |
1946 |
| - process = ctx.Process(target=lambda: asyncio.run(child_main())) |
1947 |
| - process.start() |
1948 |
| - process.join() |
1949 |
| - |
1950 |
| - self.assertEqual(result.value, 42) |
1951 |
| - |
1952 |
| - @hashlib_helper.requires_hashdigest('md5') |
1953 |
| - def test_fork_asyncio_subprocess(self): |
1954 |
| - ctx = multiprocessing.get_context('fork') |
1955 |
| - manager = ctx.Manager() |
1956 |
| - self.addCleanup(manager.shutdown) |
1957 |
| - result = manager.Value('i', 1) |
1958 |
| - |
1959 |
| - async def child_main(): |
1960 |
| - proc = await asyncio.create_subprocess_exec(sys.executable, '-c', 'pass') |
1961 |
| - result.value = await proc.wait() |
1962 |
| - |
1963 |
| - process = ctx.Process(target=lambda: asyncio.run(child_main())) |
1964 |
| - process.start() |
1965 |
| - process.join() |
1966 |
| - |
1967 |
| - self.assertEqual(result.value, 0) |
1968 |
| - |
1969 | 1870 | if __name__ == '__main__':
|
1970 | 1871 | unittest.main()
|
0 commit comments