Skip to content

Run-Tests Cleanup / Modernization #5707

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 59 additions & 30 deletions run-tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
* Minimum required PHP version: 7.0.0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As you are using void, update this comment to say 7.1.0.

*/

function show_usage()
function show_usage(): void
{
echo <<<HELP
Synopsis:
Expand Down Expand Up @@ -146,10 +146,9 @@ function main()

define('IS_WINDOWS', substr(PHP_OS, 0, 3) == "WIN");

$workerID = 0;
if (getenv("TEST_PHP_WORKER")) {
$workerID = intval(getenv("TEST_PHP_WORKER"));
run_worker();
$workerID = get_worker_id();
if($workerID) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if($workerID) {
if ($workerID) {

run_worker($workerID);
return;
}

Expand Down Expand Up @@ -443,18 +442,11 @@ function main()

switch ($switch) {
case 'j':
$workers = substr($argv[$i], 2);
if (!preg_match('/^\d+$/', $workers) || $workers == 0) {
error("'$workers' is not a valid number of workers, try e.g. -j16 for 16 workers");
}
$workers = intval($workers, 10);
// Don't use parallel testing infrastructure if there is only one worker.
if ($workers === 1) {
$workers = null;
}
$workers = get_number_of_workers($argv[$i]);
break;
case 'r':
case 'l':
// @Dragoonis
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leftover?

$test_list = file($argv[++$i]);
if ($test_list) {
foreach ($test_list as $test) {
Expand Down Expand Up @@ -1080,7 +1072,7 @@ function test_sort($a, $b)
// Send Email to QA Team
//

function mail_qa_team($data, $status = false)
function mail_qa_team($data, $status = false): bool
{
$url_bits = parse_url(QA_SUBMISSION_PAGE);

Expand Down Expand Up @@ -1119,7 +1111,7 @@ function mail_qa_team($data, $status = false)
fwrite($fs, "\r\n\r\n");
fclose($fs);

return 1;
return true;
}

//
Expand Down Expand Up @@ -1381,7 +1373,7 @@ function run_all_tests_parallel($test_files, $env, $redir_tested)
}

// Don't start more workers than test files.
$workers = max(1, min($workers, count($test_files)));
$workers = get_max_workers_from_test_files($workers, $test_files);

echo "Spawning $workers workers... ";

Expand Down Expand Up @@ -1643,15 +1635,15 @@ function run_all_tests_parallel($test_files, $env, $redir_tested)
}
}

function send_message($stream, array $message)
function send_message($stream, array $message): void
{
$blocking = stream_get_meta_data($stream)["blocked"];
stream_set_blocking($stream, true);
fwrite($stream, base64_encode(serialize($message)) . "\n");
stream_set_blocking($stream, $blocking);
}

function kill_children(array $children)
function kill_children(array $children): void
{
foreach ($children as $child) {
if ($child) {
Expand All @@ -1660,9 +1652,9 @@ function kill_children(array $children)
}
}

function run_worker()
function run_worker($workerID): void
{
global $workerID, $workerSock;
global $workerSock;

$sockUri = getenv("TEST_PHP_URI");

Expand Down Expand Up @@ -1819,15 +1811,7 @@ function run_test($php, $file, $env)
}

// check for unknown sections
if (!in_array($section, array(
'EXPECT', 'EXPECTF', 'EXPECTREGEX', 'EXPECTREGEX_EXTERNAL', 'EXPECT_EXTERNAL', 'EXPECTF_EXTERNAL', 'EXPECTHEADERS',
'POST', 'POST_RAW', 'GZIP_POST', 'DEFLATE_POST', 'PUT', 'GET', 'COOKIE', 'ARGS',
'FILE', 'FILEEOF', 'FILE_EXTERNAL', 'REDIRECTTEST',
'CAPTURE_STDIO', 'STDIN', 'CGI', 'PHPDBG',
'INI', 'ENV', 'EXTENSIONS',
'SKIPIF', 'XFAIL', 'XLEAK', 'CLEAN',
'CREDITS', 'DESCRIPTION', 'CONFLICTS', 'WHITESPACE_SENSITIVE',
))) {
if (is_section_unknown($section)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: I would prefer if this was !is_section_known($section) or !is_known_section($section).

$bork_info = 'Unknown section "' . $section . '"';
}

Expand Down Expand Up @@ -3613,4 +3597,49 @@ function check_proc_open_function_exists()
}
}




function get_number_of_workers($workers): int
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
function get_number_of_workers($workers): int
function parse_number_of_workers(string $workers): int

maybe

{
$cleanWorkers = substr($workers, 2);
if (!preg_match('/^\d+$/', $cleanWorkers) || $cleanWorkers == 0) {
error("'$workers' is not a valid number of workers, try e.g. -j16 for 16 workers");
}
$cleanWorkers = intval($cleanWorkers, 10);
// Don't use parallel testing infrastructure if there is only one worker.
if ($cleanWorkers === 1) {
$cleanWorkers = null;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be either 0, or the return type should be ?int.

}

return $cleanWorkers;
}

function get_max_workers_from_test_files($workers, $test_files): int
{
return max(1, min($workers, count($test_files)));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code seems easier to understand if left inlined.

}

function get_worker_id(): int
{
if (!getenv("TEST_PHP_WORKER")) {
return 0;
}

return intval(getenv("TEST_PHP_WORKER"));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function can be simplified to (int) getenv("TEST_PHP_WORKER").

}

function is_section_unknown(string $section): bool
{
return !in_array($section, array(
'EXPECT', 'EXPECTF', 'EXPECTREGEX', 'EXPECTREGEX_EXTERNAL', 'EXPECT_EXTERNAL', 'EXPECTF_EXTERNAL', 'EXPECTHEADERS',
'POST', 'POST_RAW', 'GZIP_POST', 'DEFLATE_POST', 'PUT', 'GET', 'COOKIE', 'ARGS',
'FILE', 'FILEEOF', 'FILE_EXTERNAL', 'REDIRECTTEST',
'CAPTURE_STDIO', 'STDIN', 'CGI', 'PHPDBG',
'INI', 'ENV', 'EXTENSIONS',
'SKIPIF', 'XFAIL', 'XLEAK', 'CLEAN',
'CREDITS', 'DESCRIPTION', 'CONFLICTS', 'WHITESPACE_SENSITIVE',
));
}

main();