Skip to content
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

Add tracker of running test to avoid incomplete coverage results #90

Merged
merged 2 commits into from
Oct 15, 2023
Merged
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
29 changes: 29 additions & 0 deletions c3.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,26 @@ function __c3_send_file($filename)
return __c3_exit();
}

/**
* Keep track of the number of running tests
* @param bool $decrease default false. Whether to increase or decrease the counter
*/
function __c3_testcounter($decrease = false)
{
$blockfilename = realpath(C3_CODECOVERAGE_MEDIATE_STORAGE) . DIRECTORY_SEPARATOR . 'block_report';
$file = fopen($blockfilename, 'c+');
if (flock($file, LOCK_EX)){
// 24 bytes is enough to hold largest integer supported in 64 bit systems
$testcounter = intval(fread($file, 24)) + ($decrease ? -1 : 1);
ftruncate($file, 0);
rewind($file);
fwrite($file, $testcounter);
} else {
__c3_error("Failed to acquire write-lock for $blockfilename");
}
fclose($file);
}

/**
* @param $filename
* @param bool $lock Lock the file for writing?
Expand All @@ -242,6 +262,13 @@ function __c3_factory($filename, $lock = false)
__c3_error("Failed to acquire write-lock for $filename");
}
} else {
// wait until serialized coverage data of all tests is written to file
$blockfilename = realpath(C3_CODECOVERAGE_MEDIATE_STORAGE) . DIRECTORY_SEPARATOR . 'block_report';
if (file_exists($blockfilename) && filesize($blockfilename) !== 0) {
while(file_get_contents($blockfilename) !== '0') {
usleep(250000); // 0.25 sec
}
}
$phpCoverage = unserialize(file_get_contents($filename));
}

Expand Down Expand Up @@ -383,6 +410,7 @@ function __c3_clear()
}
} else {
list($codeCoverage, ) = __c3_factory(null);
__c3_testcounter();
$codeCoverage->start(C3_CODECOVERAGE_TESTNAME);
if (!array_key_exists('HTTP_X_CODECEPTION_CODECOVERAGE_DEBUG', $_SERVER)) {
register_shutdown_function(
Expand Down Expand Up @@ -419,6 +447,7 @@ function () use ($codeCoverage, $currentReport) {
flock($file, LOCK_UN);
fclose($file);
}
__c3_testcounter(true);
}
);
}
Expand Down