(self, script_code, python_args=None, env=None,
prologue='',
script_path=os_helper.TESTFN + '_remote.py')
| 1965 | test.support.reap_children() |
| 1966 | |
| 1967 | def _run_remote_exec_test(self, script_code, python_args=None, env=None, |
| 1968 | prologue='', |
| 1969 | script_path=os_helper.TESTFN + '_remote.py'): |
| 1970 | # Create the script that will be remotely executed |
| 1971 | self.addCleanup(os_helper.unlink, script_path) |
| 1972 | |
| 1973 | with open(script_path, 'w') as f: |
| 1974 | f.write(script_code) |
| 1975 | |
| 1976 | # Create and run the target process |
| 1977 | target = os_helper.TESTFN + '_target.py' |
| 1978 | self.addCleanup(os_helper.unlink, target) |
| 1979 | |
| 1980 | port = find_unused_port() |
| 1981 | |
| 1982 | with open(target, 'w') as f: |
| 1983 | f.write(f''' |
| 1984 | import sys |
| 1985 | import time |
| 1986 | import socket |
| 1987 | |
| 1988 | # Connect to the test process |
| 1989 | sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 1990 | sock.connect(('localhost', {port})) |
| 1991 | |
| 1992 | {prologue} |
| 1993 | |
| 1994 | # Signal that the process is ready |
| 1995 | sock.sendall(b"ready") |
| 1996 | |
| 1997 | print("Target process running...") |
| 1998 | |
| 1999 | # Wait for remote script to be executed |
| 2000 | # (the execution will happen as the following |
| 2001 | # code is processed as soon as the recv call |
| 2002 | # unblocks) |
| 2003 | sock.recv(1024) |
| 2004 | |
| 2005 | # Do a bunch of work to give the remote script time to run |
| 2006 | x = 0 |
| 2007 | for i in range(100): |
| 2008 | x += i |
| 2009 | |
| 2010 | # Write confirmation back |
| 2011 | sock.sendall(b"executed") |
| 2012 | sock.close() |
| 2013 | ''') |
| 2014 | |
| 2015 | # Start the target process and capture its output |
| 2016 | cmd = [sys.executable] |
| 2017 | if python_args: |
| 2018 | cmd.extend(python_args) |
| 2019 | cmd.append(target) |
| 2020 | |
| 2021 | # Create a socket server to communicate with the target process |
| 2022 | server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 2023 | server_socket.bind(('localhost', port)) |
| 2024 | server_socket.settimeout(SHORT_TIMEOUT) |
no test coverage detected