@@ -225,6 +225,26 @@ function __c3_send_file($filename)
225225 return __c3_exit ();
226226 }
227227
228+ /**
229+ * Keep track of the number of running tests
230+ * @param bool $decrease default false. Whether to increase or decrease the counter
231+ */
232+ function __c3_testcounter ($ decrease = false )
233+ {
234+ $ blockfilename = realpath (C3_CODECOVERAGE_MEDIATE_STORAGE ) . DIRECTORY_SEPARATOR . 'block_report ' ;
235+ $ file = fopen ($ blockfilename , 'c+ ' );
236+ if (flock ($ file , LOCK_EX )){
237+ // 24 bytes is enough to hold largest integer supported in 64 bit systems
238+ $ testcounter = intval (fread ($ file , 24 )) + ($ decrease ? -1 : 1 );
239+ ftruncate ($ file , 0 );
240+ rewind ($ file );
241+ fwrite ($ file , $ testcounter );
242+ } else {
243+ __c3_error ("Failed to acquire write-lock for $ blockfilename " );
244+ }
245+ fclose ($ file );
246+ }
247+
228248 /**
229249 * @param $filename
230250 * @param bool $lock Lock the file for writing?
@@ -242,6 +262,13 @@ function __c3_factory($filename, $lock = false)
242262 __c3_error ("Failed to acquire write-lock for $ filename " );
243263 }
244264 } else {
265+ // wait until serialized coverage data of all tests is written to file
266+ $ blockfilename = realpath (C3_CODECOVERAGE_MEDIATE_STORAGE ) . DIRECTORY_SEPARATOR . 'block_report ' ;
267+ if (file_exists ($ blockfilename ) && filesize ($ blockfilename ) !== 0 ) {
268+ while (file_get_contents ($ blockfilename ) !== '0 ' ) {
269+ usleep (250000 ); // 0.25 sec
270+ }
271+ }
245272 $ phpCoverage = unserialize (file_get_contents ($ filename ));
246273 }
247274
@@ -383,6 +410,7 @@ function __c3_clear()
383410 }
384411} else {
385412 list ($ codeCoverage , ) = __c3_factory (null );
413+ __c3_testcounter ();
386414 $ codeCoverage ->start (C3_CODECOVERAGE_TESTNAME );
387415 if (!array_key_exists ('HTTP_X_CODECEPTION_CODECOVERAGE_DEBUG ' , $ _SERVER )) {
388416 register_shutdown_function (
@@ -419,6 +447,7 @@ function () use ($codeCoverage, $currentReport) {
419447 flock ($ file , LOCK_UN );
420448 fclose ($ file );
421449 }
450+ __c3_testcounter (true );
422451 }
423452 );
424453 }
0 commit comments