55import enum
66import json
77import os
8+ import pathlib
9+ import socket
810import sys
911import traceback
1012import unittest
1113from types import TracebackType
1214from typing import Dict , List , Optional , Tuple , Type , Union
1315
16+ script_dir = pathlib .Path (__file__ ).parent .parent
17+ sys .path .append (os .fspath (script_dir ))
18+ sys .path .append (os .fspath (script_dir / "lib" / "python" ))
19+ from testing_tools import process_json_util
20+
1421# Add the path to pythonFiles to sys.path to find testing_tools.socket_manager.
1522PYTHON_FILES = os .path .dirname (os .path .dirname (os .path .abspath (__file__ )))
1623sys .path .insert (0 , PYTHON_FILES )
2532
2633def parse_execution_cli_args (
2734 args : List [str ],
28- ) -> Tuple [int , Union [str , None ], List [ str ] ]:
35+ ) -> Tuple [int , Union [str , None ]]:
2936 """Parse command-line arguments that should be processed by the script.
3037
3138 So far this includes the port number that it needs to connect to, the uuid passed by the TS side,
@@ -39,10 +46,9 @@ def parse_execution_cli_args(
3946 arg_parser = argparse .ArgumentParser ()
4047 arg_parser .add_argument ("--port" , default = DEFAULT_PORT )
4148 arg_parser .add_argument ("--uuid" )
42- arg_parser .add_argument ("--testids" , nargs = "+" )
4349 parsed_args , _ = arg_parser .parse_known_args (args )
4450
45- return (int (parsed_args .port ), parsed_args .uuid , parsed_args . testids )
51+ return (int (parsed_args .port ), parsed_args .uuid )
4652
4753
4854ErrorType = Union [
@@ -226,11 +232,62 @@ def run_tests(
226232
227233 start_dir , pattern , top_level_dir = parse_unittest_args (argv [index + 1 :])
228234
229- # Perform test execution.
230- port , uuid , testids = parse_execution_cli_args (argv [:index ])
231- payload = run_tests (start_dir , testids , pattern , top_level_dir , uuid )
235+ run_test_ids_port = os .environ .get ("RUN_TEST_IDS_PORT" )
236+ run_test_ids_port_int = (
237+ int (run_test_ids_port ) if run_test_ids_port is not None else 0
238+ )
239+
240+ # get data from socket
241+ test_ids_from_buffer = []
242+ try :
243+ client_socket = socket .socket (socket .AF_INET , socket .SOCK_STREAM )
244+ client_socket .connect (("localhost" , run_test_ids_port_int ))
245+ print (f"CLIENT: Server listening on port { run_test_ids_port_int } ..." )
246+ buffer = b""
247+
248+ while True :
249+ # Receive the data from the client
250+ data = client_socket .recv (1024 * 1024 )
251+ if not data :
252+ break
253+
254+ # Append the received data to the buffer
255+ buffer += data
256+
257+ try :
258+ # Try to parse the buffer as JSON
259+ test_ids_from_buffer = process_json_util .process_rpc_json (
260+ buffer .decode ("utf-8" )
261+ )
262+ # Clear the buffer as complete JSON object is received
263+ buffer = b""
264+
265+ # Process the JSON data
266+ print (f"Received JSON data: { test_ids_from_buffer } " )
267+ break
268+ except json .JSONDecodeError :
269+ # JSON decoding error, the complete JSON object is not yet received
270+ continue
271+ except socket .error as e :
272+ print (f"Error: Could not connect to runTestIdsPort: { e } " )
273+ print ("Error: Could not connect to runTestIdsPort" )
274+
275+ port , uuid = parse_execution_cli_args (argv [:index ])
276+ if test_ids_from_buffer :
277+ # Perform test execution.
278+ payload = run_tests (
279+ start_dir , test_ids_from_buffer , pattern , top_level_dir , uuid
280+ )
281+ else :
282+ cwd = os .path .abspath (start_dir )
283+ status = TestExecutionStatus .error
284+ payload : PayloadDict = {
285+ "cwd" : cwd ,
286+ "status" : status ,
287+ "error" : "No test ids received from buffer" ,
288+ }
232289
233- # Build the request data (it has to be a POST request or the Node side will not process it), and send it.
290+ # Build the request data and send it.
234291 addr = ("localhost" , port )
235292 data = json .dumps (payload )
236293 request = f"""Content-Length: { len (data )}
0 commit comments