(self, script_code, python_args=None, env=None,
prologue='',
script_path=os_helper.TESTFN + '_remote.py')
| 1993 | test.support.reap_children() |
| 1994 | |
| 1995 | def _run_remote_exec_test(self, script_code, python_args=None, env=None, |
| 1996 | prologue='', |
| 1997 | script_path=os_helper.TESTFN + '_remote.py'): |
| 1998 | # Create the script that will be remotely executed |
| 1999 | self.addCleanup(os_helper.unlink, script_path) |
| 2000 | |
| 2001 | with open(script_path, 'w') as f: |
| 2002 | f.write(script_code) |
| 2003 | |
| 2004 | # Create and run the target process |
| 2005 | target = os_helper.TESTFN + '_target.py' |
| 2006 | self.addCleanup(os_helper.unlink, target) |
| 2007 | |
| 2008 | port = find_unused_port() |
| 2009 | |
| 2010 | with open(target, 'w') as f: |
| 2011 | f.write(f''' |
| 2012 | import sys |
| 2013 | import time |
| 2014 | import socket |
| 2015 | |
| 2016 | # Connect to the test process |
| 2017 | sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 2018 | sock.connect(('localhost', {port})) |
| 2019 | |
| 2020 | {prologue} |
| 2021 | |
| 2022 | # Signal that the process is ready |
| 2023 | sock.sendall(b"ready") |
| 2024 | |
| 2025 | print("Target process running...") |
| 2026 | |
| 2027 | # Wait for remote script to be executed |
| 2028 | # (the execution will happen as the following |
| 2029 | # code is processed as soon as the recv call |
| 2030 | # unblocks) |
| 2031 | sock.recv(1024) |
| 2032 | |
| 2033 | # Do a bunch of work to give the remote script time to run |
| 2034 | x = 0 |
| 2035 | for i in range(100): |
| 2036 | x += i |
| 2037 | |
| 2038 | # Write confirmation back |
| 2039 | sock.sendall(b"executed") |
| 2040 | sock.close() |
| 2041 | ''') |
| 2042 | |
| 2043 | # Start the target process and capture its output |
| 2044 | cmd = [sys.executable] |
| 2045 | if python_args: |
| 2046 | cmd.extend(python_args) |
| 2047 | cmd.append(target) |
| 2048 | |
| 2049 | # Create a socket server to communicate with the target process |
| 2050 | server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 2051 | server_socket.bind(('localhost', port)) |
| 2052 | server_socket.settimeout(SHORT_TIMEOUT) |
no test coverage detected