@@ -156,8 +156,10 @@ function main(): void
156156 $ test_files , $ test_idx , $ test_results , $ testfile ,
157157 $ valgrind , $ sum_results , $ shuffle , $ file_cache , $ num_repeats ,
158158 $ show_progress ;
159- // Parallel testing
160- global $ workers , $ workerID ;
159+
160+ global $ workerContext ;
161+ $ workerContext = new WorkerContext ();
162+
161163 global $ context_line_count ;
162164
163165 // Temporary for the duration of refactoring
@@ -166,9 +168,9 @@ function main(): void
166168
167169 define ('IS_WINDOWS ' , substr (PHP_OS , 0 , 3 ) == "WIN " );
168170
169- $ workerID = 0 ;
171+ $ workerContext -> workerID = 0 ;
170172 if (getenv ("TEST_PHP_WORKER " )) {
171- $ workerID = intval (getenv ("TEST_PHP_WORKER " ));
173+ $ workerContext -> workerID = intval (getenv ("TEST_PHP_WORKER " ));
172174 run_worker ();
173175 return ;
174176 }
@@ -251,7 +253,7 @@ function main(): void
251253 $ DETAILED = 0 ;
252254 }
253255
254- $ junit = new JUnit ($ environment , $ workerID );
256+ $ junit = new JUnit ($ environment , $ workerContext -> workerID );
255257
256258 if (getenv ('SHOW_ONLY_GROUPS ' )) {
257259 $ SHOW_ONLY_GROUPS = explode (", " , getenv ('SHOW_ONLY_GROUPS ' ));
@@ -350,7 +352,7 @@ function main(): void
350352 $ file_cache = null ;
351353 $ shuffle = false ;
352354 $ bless = false ;
353- $ workers = null ;
355+ $ workerContext -> workers = null ;
354356 $ context_line_count = 3 ;
355357 $ num_repeats = 1 ;
356358 $ show_progress = true ;
@@ -421,6 +423,7 @@ function main(): void
421423 if ($ workers === 1 ) {
422424 $ workers = null ;
423425 }
426+ $ workerContext ->workers = $ workers ;
424427 break ;
425428 case 'r ' :
426429 case 'l ' :
@@ -1229,13 +1232,15 @@ function system_with_timeout(
12291232 proc_close ($ proc );
12301233 return $ data ;
12311234}
1232-
12331235function run_all_tests (array $ test_files , array $ env , ?string $ redir_tested = null ): void
12341236{
12351237 global $ test_results , $ failed_tests_file , $ result_tests_file , $ php , $ test_idx , $ file_cache ;
12361238 global $ preload ;
12371239 // Parallel testing
1238- global $ PHP_FAILED_TESTS , $ workers , $ workerID , $ workerSock ;
1240+ global $ PHP_FAILED_TESTS ;
1241+
1242+ /** @var WorkerContext $workerContext */
1243+ global $ workerContext ;
12391244
12401245 if ($ file_cache !== null || $ preload ) {
12411246 /* Automatically skip opcache tests in --file-cache and --preload mode,
@@ -1255,7 +1260,7 @@ function run_all_tests(array $test_files, array $env, ?string $redir_tested = nu
12551260 }
12561261
12571262 /* Ignore -jN if there is only one file to analyze. */
1258- if ($ workers !== null && count ($ test_files ) > 1 && !$ workerID ) {
1263+ if ($ workerContext -> workers !== null && count ($ test_files ) > 1 && !$ workerContext -> workerID ) {
12591264 run_all_tests_parallel ($ test_files , $ env , $ redir_tested );
12601265 return ;
12611266 }
@@ -1274,19 +1279,19 @@ function run_all_tests(array $test_files, array $env, ?string $redir_tested = nu
12741279 }
12751280 $ test_idx ++;
12761281
1277- if ($ workerID ) {
1282+ if ($ workerContext -> workerID ) {
12781283 $ PHP_FAILED_TESTS = ['BORKED ' => [], 'FAILED ' => [], 'WARNED ' => [], 'LEAKED ' => [], 'XFAILED ' => [], 'XLEAKED ' => [], 'SLOW ' => []];
12791284 ob_start ();
12801285 }
12811286
12821287 $ result = run_test ($ php , $ name , $ env );
1283- if ($ workerID ) {
1288+ if ($ workerContext -> workerID ) {
12841289 $ resultText = ob_get_clean ();
12851290 }
12861291
12871292 if (!is_array ($ name ) && $ result != 'REDIR ' ) {
1288- if ($ workerID ) {
1289- send_message ($ workerSock , [
1293+ if ($ workerContext -> workerID ) {
1294+ send_message ($ workerContext -> workerSock , [
12901295 "type " => "test_result " ,
12911296 "name " => $ name ,
12921297 "index " => $ index ,
@@ -1310,10 +1315,13 @@ function run_all_tests(array $test_files, array $env, ?string $redir_tested = nu
13101315
13111316function run_all_tests_parallel (array $ test_files , array $ env , ?string $ redir_tested ): void
13121317{
1313- global $ workers , $ test_idx , $ test_results , $ failed_tests_file , $ result_tests_file , $ PHP_FAILED_TESTS , $ shuffle , $ valgrind , $ show_progress ;
1318+ global $ test_idx , $ test_results , $ failed_tests_file , $ result_tests_file , $ PHP_FAILED_TESTS , $ shuffle , $ valgrind , $ show_progress ;
13141319
13151320 global $ junit ;
13161321
1322+ /** @var WorkerContext $workerContext */
1323+ global $ workerContext ;
1324+
13171325 // The PHP binary running run-tests.php, and run-tests.php itself
13181326 // This PHP executable is *not* necessarily the same as the tested version
13191327 $ thisPHP = PHP_BINARY ;
@@ -1367,9 +1375,9 @@ function run_all_tests_parallel(array $test_files, array $env, ?string $redir_te
13671375 }
13681376
13691377 // Don't start more workers than test files.
1370- $ workers = max (1 , min ($ workers , count ($ test_files )));
1378+ $ workerContext -> workers = max (1 , min ($ workerContext -> workers , count ($ test_files )));
13711379
1372- echo "Spawning $ workers workers... " ;
1380+ echo "Spawning " . $ workerContext -> workers . " workers... " ;
13731381
13741382 // We use sockets rather than STDIN/STDOUT for comms because on Windows,
13751383 // those can't be non-blocking for some reason.
@@ -1386,7 +1394,7 @@ function run_all_tests_parallel(array $test_files, array $env, ?string $redir_te
13861394 $ totalFileCount = count ($ test_files );
13871395
13881396 $ startTime = microtime (true );
1389- for ($ i = 1 ; $ i <= $ workers ; $ i ++) {
1397+ for ($ i = 1 ; $ i <= $ workerContext -> workers ; $ i ++) {
13901398 $ proc = proc_open (
13911399 [$ thisPHP , $ thisScript ],
13921400 [], // Inherit our stdin, stdout and stderr
@@ -1408,7 +1416,7 @@ function run_all_tests_parallel(array $test_files, array $env, ?string $redir_te
14081416 $ workerProcs [$ i ] = $ proc ;
14091417 }
14101418
1411- for ($ i = 1 ; $ i <= $ workers ; $ i ++) {
1419+ for ($ i = 1 ; $ i <= $ workerContext -> workers ; $ i ++) {
14121420 $ workerSock = stream_socket_accept ($ listenSock , 5 );
14131421 if ($ workerSock === false ) {
14141422 kill_children ($ workerProcs );
@@ -1567,7 +1575,7 @@ function run_all_tests_parallel(array $test_files, array $env, ?string $redir_te
15671575 echo $ resultText ;
15681576
15691577 if ($ show_progress ) {
1570- show_test ($ test_idx , count ($ workerProcs ) . "/ $ workers concurrent test workers running " );
1578+ show_test ($ test_idx , count ($ workerProcs ) . "/ " . $ workerContext -> workers . " concurrent test workers running " );
15711579 }
15721580
15731581 if (!is_array ($ name ) && $ result != 'REDIR ' ) {
@@ -1662,6 +1670,9 @@ function safe_fwrite($stream, string $data)
16621670 return $ bytes_written ;
16631671}
16641672
1673+ /**
1674+ * @param resource $stream
1675+ */
16651676function send_message ($ stream , array $ message ): void
16661677{
16671678 $ blocking = stream_get_meta_data ($ stream )["blocked " ];
@@ -1681,23 +1692,24 @@ function kill_children(array $children): void
16811692
16821693function run_worker (): void
16831694{
1684- global $ workerID , $ workerSock ;
1695+ /** @var WorkerContext $workerContext */
1696+ global $ workerContext ;
16851697
16861698 global $ junit ;
16871699
16881700 $ sockUri = getenv ("TEST_PHP_URI " );
16891701
1690- $ workerSock = stream_socket_client ($ sockUri , $ _ , $ _ , 5 ) or error ("Couldn't connect to $ sockUri " );
1702+ $ workerContext -> workerSock = stream_socket_client ($ sockUri , $ _ , $ _ , 5 ) or error ("Couldn't connect to $ sockUri " );
16911703
1692- $ greeting = fgets ($ workerSock );
1704+ $ greeting = fgets ($ workerContext -> workerSock );
16931705 $ greeting = unserialize (base64_decode ($ greeting )) or die ("Could not decode greeting \n" );
16941706 if ($ greeting ["type " ] !== "hello " ) {
16951707 error ("Unexpected greeting of type $ greeting [type]" );
16961708 }
16971709
1698- set_error_handler (function (int $ errno , string $ errstr , string $ errfile , int $ errline ) use ($ workerSock ): bool {
1710+ set_error_handler (function (int $ errno , string $ errstr , string $ errfile , int $ errline ) use ($ workerContext ): bool {
16991711 if (error_reporting () & $ errno ) {
1700- send_message ($ workerSock , compact ('errno ' , 'errstr ' , 'errfile ' , 'errline ' ) + [
1712+ send_message ($ workerContext -> workerSock , compact ('errno ' , 'errstr ' , 'errfile ' , 'errline ' ) + [
17011713 'type ' => 'php_error '
17021714 ]);
17031715 }
@@ -1706,37 +1718,37 @@ function run_worker(): void
17061718 });
17071719
17081720 foreach ($ greeting ["GLOBALS " ] as $ var => $ value ) {
1709- if ($ var !== "workerID " && $ var !== " workerSock " && $ var !== "GLOBALS " ) {
1721+ if ($ var !== "workerContext " && $ var !== "GLOBALS " ) {
17101722 $ GLOBALS [$ var ] = $ value ;
17111723 }
17121724 }
17131725 foreach ($ greeting ["constants " ] as $ const => $ value ) {
17141726 define ($ const , $ value );
17151727 }
17161728
1717- send_message ($ workerSock , [
1729+ send_message ($ workerContext -> workerSock , [
17181730 "type " => "hello_reply " ,
1719- "workerID " => $ workerID
1731+ "workerID " => $ workerContext -> workerID
17201732 ]);
17211733
1722- send_message ($ workerSock , [
1734+ send_message ($ workerContext -> workerSock , [
17231735 "type " => "ready "
17241736 ]);
17251737
1726- while (($ command = fgets ($ workerSock ))) {
1738+ while (($ command = fgets ($ workerContext -> workerSock ))) {
17271739 $ command = unserialize (base64_decode ($ command ));
17281740
17291741 switch ($ command ["type " ]) {
17301742 case "run_tests " :
17311743 run_all_tests ($ command ["test_files " ], $ command ["env " ], $ command ["redir_tested " ]);
1732- send_message ($ workerSock , [
1744+ send_message ($ workerContext -> workerSock , [
17331745 "type " => "tests_finished " ,
17341746 "junit " => $ junit ->isEnabled () ? $ junit : null ,
17351747 ]);
17361748 $ junit ->clear ();
17371749 break ;
17381750 default :
1739- send_message ($ workerSock , [
1751+ send_message ($ workerContext -> workerSock , [
17401752 "type " => "error " ,
17411753 "msg " => "Unrecognised message type: $ command [type]"
17421754 ]);
@@ -1796,10 +1808,11 @@ function run_test(string $php, $file, array $env): string
17961808 global $ slow_min_ms ;
17971809 global $ preload , $ file_cache ;
17981810 global $ num_repeats ;
1799- // Parallel testing
1800- global $ workerID ;
18011811 global $ show_progress ;
18021812
1813+ /** @var WorkerContext $workerContext */
1814+ global $ workerContext ;
1815+
18031816 // Temporary
18041817 /** @var JUnit $junit */
18051818 global $ junit ;
@@ -1913,7 +1926,7 @@ function run_test(string $php, $file, array $env): string
19131926 }
19141927 }
19151928
1916- if ($ show_progress && !$ workerID ) {
1929+ if ($ show_progress && !$ workerContext -> workerID ) {
19171930 show_test ($ test_idx , $ shortname );
19181931 }
19191932
@@ -2004,7 +2017,7 @@ function run_test(string $php, $file, array $env): string
20042017 }
20052018
20062019 // Default ini settings
2007- $ ini_settings = $ workerID ? ['opcache.cache_id ' => "worker $ workerID " ] : [];
2020+ $ ini_settings = $ workerContext -> workerID ? ['opcache.cache_id ' => "worker " . $ workerContext -> workers ] : [];
20082021
20092022 // Additional required extensions
20102023 $ extensions = [];
@@ -3217,10 +3230,10 @@ function show_test(int $test_idx, string $shortname): void
32173230function clear_show_test (): void
32183231{
32193232 global $ line_length ;
3220- // Parallel testing
3221- global $ workerID ;
3233+ /** @var WorkerContext $workerContext */
3234+ global $ workerContext ;
32223235
3223- if (!$ workerID && isset ($ line_length )) {
3236+ if (!$ workerContext -> workerID && isset ($ line_length )) {
32243237 // Write over the last line to avoid random trailing chars on next echo
32253238 echo str_repeat (" " , $ line_length ), "\r" ;
32263239 }
@@ -4171,4 +4184,19 @@ public function getDiff(array $diffs): string
41714184 }
41724185}
41734186
4187+ class WorkerContext {
4188+ /**
4189+ * @var int|null number of workers to use, null for non-parallel testing.
4190+ */
4191+ public $ workers ;
4192+ /**
4193+ * @var int
4194+ */
4195+ public $ workerID ;
4196+ /**
4197+ * @var resource
4198+ */
4199+ public $ workerSock ;
4200+ }
4201+
41744202main ();
0 commit comments