@@ -156,8 +156,10 @@ function main(): void
156
156
$ test_files , $ test_idx , $ test_results , $ testfile ,
157
157
$ valgrind , $ sum_results , $ shuffle , $ file_cache , $ num_repeats ,
158
158
$ show_progress ;
159
- // Parallel testing
160
- global $ workers , $ workerID ;
159
+
160
+ global $ workerContext ;
161
+ $ workerContext = new WorkerContext ();
162
+
161
163
global $ context_line_count ;
162
164
163
165
// Temporary for the duration of refactoring
@@ -166,9 +168,9 @@ function main(): void
166
168
167
169
define ('IS_WINDOWS ' , substr (PHP_OS , 0 , 3 ) == "WIN " );
168
170
169
- $ workerID = 0 ;
171
+ $ workerContext -> workerID = 0 ;
170
172
if (getenv ("TEST_PHP_WORKER " )) {
171
- $ workerID = intval (getenv ("TEST_PHP_WORKER " ));
173
+ $ workerContext -> workerID = intval (getenv ("TEST_PHP_WORKER " ));
172
174
run_worker ();
173
175
return ;
174
176
}
@@ -251,7 +253,7 @@ function main(): void
251
253
$ DETAILED = 0 ;
252
254
}
253
255
254
- $ junit = new JUnit ($ environment , $ workerID );
256
+ $ junit = new JUnit ($ environment , $ workerContext -> workerID );
255
257
256
258
if (getenv ('SHOW_ONLY_GROUPS ' )) {
257
259
$ SHOW_ONLY_GROUPS = explode (", " , getenv ('SHOW_ONLY_GROUPS ' ));
@@ -350,7 +352,7 @@ function main(): void
350
352
$ file_cache = null ;
351
353
$ shuffle = false ;
352
354
$ bless = false ;
353
- $ workers = null ;
355
+ $ workerContext -> workers = null ;
354
356
$ context_line_count = 3 ;
355
357
$ num_repeats = 1 ;
356
358
$ show_progress = true ;
@@ -421,6 +423,7 @@ function main(): void
421
423
if ($ workers === 1 ) {
422
424
$ workers = null ;
423
425
}
426
+ $ workerContext ->workers = $ workers ;
424
427
break ;
425
428
case 'r ' :
426
429
case 'l ' :
@@ -1229,13 +1232,14 @@ function system_with_timeout(
1229
1232
proc_close ($ proc );
1230
1233
return $ data ;
1231
1234
}
1232
-
1233
1235
function run_all_tests (array $ test_files , array $ env , ?string $ redir_tested = null ): void
1234
1236
{
1235
1237
global $ test_results , $ failed_tests_file , $ result_tests_file , $ php , $ test_idx , $ file_cache ;
1236
1238
global $ preload ;
1237
- // Parallel testing
1238
- global $ PHP_FAILED_TESTS , $ workers , $ workerID , $ workerSock ;
1239
+ global $ PHP_FAILED_TESTS ;
1240
+
1241
+ /** @var WorkerContext $workerContext */
1242
+ global $ workerContext ;
1239
1243
1240
1244
if ($ file_cache !== null || $ preload ) {
1241
1245
/* Automatically skip opcache tests in --file-cache and --preload mode,
@@ -1255,7 +1259,7 @@ function run_all_tests(array $test_files, array $env, ?string $redir_tested = nu
1255
1259
}
1256
1260
1257
1261
/* Ignore -jN if there is only one file to analyze. */
1258
- if ($ workers !== null && count ($ test_files ) > 1 && !$ workerID ) {
1262
+ if ($ workerContext -> workers !== null && count ($ test_files ) > 1 && !$ workerContext -> workerID ) {
1259
1263
run_all_tests_parallel ($ test_files , $ env , $ redir_tested );
1260
1264
return ;
1261
1265
}
@@ -1274,19 +1278,19 @@ function run_all_tests(array $test_files, array $env, ?string $redir_tested = nu
1274
1278
}
1275
1279
$ test_idx ++;
1276
1280
1277
- if ($ workerID ) {
1281
+ if ($ workerContext -> workerID ) {
1278
1282
$ PHP_FAILED_TESTS = ['BORKED ' => [], 'FAILED ' => [], 'WARNED ' => [], 'LEAKED ' => [], 'XFAILED ' => [], 'XLEAKED ' => [], 'SLOW ' => []];
1279
1283
ob_start ();
1280
1284
}
1281
1285
1282
1286
$ result = run_test ($ php , $ name , $ env );
1283
- if ($ workerID ) {
1287
+ if ($ workerContext -> workerID ) {
1284
1288
$ resultText = ob_get_clean ();
1285
1289
}
1286
1290
1287
1291
if (!is_array ($ name ) && $ result != 'REDIR ' ) {
1288
- if ($ workerID ) {
1289
- send_message ($ workerSock , [
1292
+ if ($ workerContext -> workerID ) {
1293
+ send_message ($ workerContext -> workerSock , [
1290
1294
"type " => "test_result " ,
1291
1295
"name " => $ name ,
1292
1296
"index " => $ index ,
@@ -1310,10 +1314,13 @@ function run_all_tests(array $test_files, array $env, ?string $redir_tested = nu
1310
1314
1311
1315
function run_all_tests_parallel (array $ test_files , array $ env , ?string $ redir_tested ): void
1312
1316
{
1313
- global $ workers , $ test_idx , $ test_results , $ failed_tests_file , $ result_tests_file , $ PHP_FAILED_TESTS , $ shuffle , $ valgrind , $ show_progress ;
1317
+ global $ test_idx , $ test_results , $ failed_tests_file , $ result_tests_file , $ PHP_FAILED_TESTS , $ shuffle , $ valgrind , $ show_progress ;
1314
1318
1315
1319
global $ junit ;
1316
1320
1321
+ /** @var WorkerContext $workerContext */
1322
+ global $ workerContext ;
1323
+
1317
1324
// The PHP binary running run-tests.php, and run-tests.php itself
1318
1325
// This PHP executable is *not* necessarily the same as the tested version
1319
1326
$ thisPHP = PHP_BINARY ;
@@ -1367,9 +1374,9 @@ function run_all_tests_parallel(array $test_files, array $env, ?string $redir_te
1367
1374
}
1368
1375
1369
1376
// Don't start more workers than test files.
1370
- $ workers = max (1 , min ($ workers , count ($ test_files )));
1377
+ $ workerContext -> workers = max (1 , min ($ workerContext -> workers , count ($ test_files )));
1371
1378
1372
- echo "Spawning $ workers workers... " ;
1379
+ echo "Spawning " . $ workerContext -> workers . " workers... " ;
1373
1380
1374
1381
// We use sockets rather than STDIN/STDOUT for comms because on Windows,
1375
1382
// those can't be non-blocking for some reason.
@@ -1386,7 +1393,7 @@ function run_all_tests_parallel(array $test_files, array $env, ?string $redir_te
1386
1393
$ totalFileCount = count ($ test_files );
1387
1394
1388
1395
$ startTime = microtime (true );
1389
- for ($ i = 1 ; $ i <= $ workers ; $ i ++) {
1396
+ for ($ i = 1 ; $ i <= $ workerContext -> workers ; $ i ++) {
1390
1397
$ proc = proc_open (
1391
1398
[$ thisPHP , $ thisScript ],
1392
1399
[], // Inherit our stdin, stdout and stderr
@@ -1408,7 +1415,7 @@ function run_all_tests_parallel(array $test_files, array $env, ?string $redir_te
1408
1415
$ workerProcs [$ i ] = $ proc ;
1409
1416
}
1410
1417
1411
- for ($ i = 1 ; $ i <= $ workers ; $ i ++) {
1418
+ for ($ i = 1 ; $ i <= $ workerContext -> workers ; $ i ++) {
1412
1419
$ workerSock = stream_socket_accept ($ listenSock , 5 );
1413
1420
if ($ workerSock === false ) {
1414
1421
kill_children ($ workerProcs );
@@ -1567,7 +1574,7 @@ function run_all_tests_parallel(array $test_files, array $env, ?string $redir_te
1567
1574
echo $ resultText ;
1568
1575
1569
1576
if ($ show_progress ) {
1570
- show_test ($ test_idx , count ($ workerProcs ) . "/ $ workers concurrent test workers running " );
1577
+ show_test ($ test_idx , count ($ workerProcs ) . "/ " . $ workerContext -> workers . " concurrent test workers running " );
1571
1578
}
1572
1579
1573
1580
if (!is_array ($ name ) && $ result != 'REDIR ' ) {
@@ -1662,6 +1669,9 @@ function safe_fwrite($stream, string $data)
1662
1669
return $ bytes_written ;
1663
1670
}
1664
1671
1672
+ /**
1673
+ * @param resource $stream
1674
+ */
1665
1675
function send_message ($ stream , array $ message ): void
1666
1676
{
1667
1677
$ blocking = stream_get_meta_data ($ stream )["blocked " ];
@@ -1681,23 +1691,24 @@ function kill_children(array $children): void
1681
1691
1682
1692
function run_worker (): void
1683
1693
{
1684
- global $ workerID , $ workerSock ;
1694
+ /** @var WorkerContext $workerContext */
1695
+ global $ workerContext ;
1685
1696
1686
1697
global $ junit ;
1687
1698
1688
1699
$ sockUri = getenv ("TEST_PHP_URI " );
1689
1700
1690
- $ workerSock = stream_socket_client ($ sockUri , $ _ , $ _ , 5 ) or error ("Couldn't connect to $ sockUri " );
1701
+ $ workerContext -> workerSock = stream_socket_client ($ sockUri , $ _ , $ _ , 5 ) or error ("Couldn't connect to $ sockUri " );
1691
1702
1692
- $ greeting = fgets ($ workerSock );
1703
+ $ greeting = fgets ($ workerContext -> workerSock );
1693
1704
$ greeting = unserialize (base64_decode ($ greeting )) or die ("Could not decode greeting \n" );
1694
1705
if ($ greeting ["type " ] !== "hello " ) {
1695
1706
error ("Unexpected greeting of type $ greeting [type]" );
1696
1707
}
1697
1708
1698
- set_error_handler (function (int $ errno , string $ errstr , string $ errfile , int $ errline ) use ($ workerSock ): bool {
1709
+ set_error_handler (function (int $ errno , string $ errstr , string $ errfile , int $ errline ) use ($ workerContext ): bool {
1699
1710
if (error_reporting () & $ errno ) {
1700
- send_message ($ workerSock , compact ('errno ' , 'errstr ' , 'errfile ' , 'errline ' ) + [
1711
+ send_message ($ workerContext -> workerSock , compact ('errno ' , 'errstr ' , 'errfile ' , 'errline ' ) + [
1701
1712
'type ' => 'php_error '
1702
1713
]);
1703
1714
}
@@ -1706,37 +1717,37 @@ function run_worker(): void
1706
1717
});
1707
1718
1708
1719
foreach ($ greeting ["GLOBALS " ] as $ var => $ value ) {
1709
- if ($ var !== "workerID " && $ var !== " workerSock " && $ var !== "GLOBALS " ) {
1720
+ if ($ var !== "workerContext " && $ var !== "GLOBALS " ) {
1710
1721
$ GLOBALS [$ var ] = $ value ;
1711
1722
}
1712
1723
}
1713
1724
foreach ($ greeting ["constants " ] as $ const => $ value ) {
1714
1725
define ($ const , $ value );
1715
1726
}
1716
1727
1717
- send_message ($ workerSock , [
1728
+ send_message ($ workerContext -> workerSock , [
1718
1729
"type " => "hello_reply " ,
1719
- "workerID " => $ workerID
1730
+ "workerID " => $ workerContext -> workerID
1720
1731
]);
1721
1732
1722
- send_message ($ workerSock , [
1733
+ send_message ($ workerContext -> workerSock , [
1723
1734
"type " => "ready "
1724
1735
]);
1725
1736
1726
- while (($ command = fgets ($ workerSock ))) {
1737
+ while (($ command = fgets ($ workerContext -> workerSock ))) {
1727
1738
$ command = unserialize (base64_decode ($ command ));
1728
1739
1729
1740
switch ($ command ["type " ]) {
1730
1741
case "run_tests " :
1731
1742
run_all_tests ($ command ["test_files " ], $ command ["env " ], $ command ["redir_tested " ]);
1732
- send_message ($ workerSock , [
1743
+ send_message ($ workerContext -> workerSock , [
1733
1744
"type " => "tests_finished " ,
1734
1745
"junit " => $ junit ->isEnabled () ? $ junit : null ,
1735
1746
]);
1736
1747
$ junit ->clear ();
1737
1748
break ;
1738
1749
default :
1739
- send_message ($ workerSock , [
1750
+ send_message ($ workerContext -> workerSock , [
1740
1751
"type " => "error " ,
1741
1752
"msg " => "Unrecognised message type: $ command [type]"
1742
1753
]);
@@ -1796,10 +1807,11 @@ function run_test(string $php, $file, array $env): string
1796
1807
global $ slow_min_ms ;
1797
1808
global $ preload , $ file_cache ;
1798
1809
global $ num_repeats ;
1799
- // Parallel testing
1800
- global $ workerID ;
1801
1810
global $ show_progress ;
1802
1811
1812
+ /** @var WorkerContext $workerContext */
1813
+ global $ workerContext ;
1814
+
1803
1815
// Temporary
1804
1816
/** @var JUnit $junit */
1805
1817
global $ junit ;
@@ -1913,7 +1925,7 @@ function run_test(string $php, $file, array $env): string
1913
1925
}
1914
1926
}
1915
1927
1916
- if ($ show_progress && !$ workerID ) {
1928
+ if ($ show_progress && !$ workerContext -> workerID ) {
1917
1929
show_test ($ test_idx , $ shortname );
1918
1930
}
1919
1931
@@ -2004,7 +2016,7 @@ function run_test(string $php, $file, array $env): string
2004
2016
}
2005
2017
2006
2018
// Default ini settings
2007
- $ ini_settings = $ workerID ? ['opcache.cache_id ' => "worker $ workerID " ] : [];
2019
+ $ ini_settings = $ workerContext -> workerID ? ['opcache.cache_id ' => "worker " . $ workerContext -> workers ] : [];
2008
2020
2009
2021
// Additional required extensions
2010
2022
$ extensions = [];
@@ -3217,10 +3229,10 @@ function show_test(int $test_idx, string $shortname): void
3217
3229
function clear_show_test (): void
3218
3230
{
3219
3231
global $ line_length ;
3220
- // Parallel testing
3221
- global $ workerID ;
3232
+ /** @var WorkerContext $workerContext */
3233
+ global $ workerContext ;
3222
3234
3223
- if (!$ workerID && isset ($ line_length )) {
3235
+ if (!$ workerContext -> workerID && isset ($ line_length )) {
3224
3236
// Write over the last line to avoid random trailing chars on next echo
3225
3237
echo str_repeat (" " , $ line_length ), "\r" ;
3226
3238
}
@@ -4171,4 +4183,19 @@ public function getDiff(array $diffs): string
4171
4183
}
4172
4184
}
4173
4185
4186
+ class WorkerContext {
4187
+ /**
4188
+ * @var int|null number of workers to use, null for non-parallel testing.
4189
+ */
4190
+ public $ workers ;
4191
+ /**
4192
+ * @var int
4193
+ */
4194
+ public $ workerID ;
4195
+ /**
4196
+ * @var resource
4197
+ */
4198
+ public $ workerSock ;
4199
+ }
4200
+
4174
4201
main ();
0 commit comments