1
0
mirror of https://github.com/danog/parallel.git synced 2024-11-27 04:44:56 +01:00
parallel/tests/TestCase.php

105 lines
3.3 KiB
PHP
Raw Normal View History

2015-07-10 22:15:42 +02:00
<?php
namespace Icicle\Tests\Concurrent;
use Icicle\Tests\Concurrent\Stub\CallbackStub;
/**
* Abstract test class with methods for creating callbacks and asserting runtimes.
*/
abstract class TestCase extends \PHPUnit_Framework_TestCase
{
const RUNTIME_PRECISION = 2; // Number of decimals to use in runtime calculations/comparisons.
2015-07-10 22:15:42 +02:00
/**
* Creates a callback that must be called $count times or the test will fail.
*
* @param int $count Number of times the callback should be called.
*
* @return callable Object that is callable and expects to be called the given number of times.
*/
public function createCallback($count)
{
$mock = $this->getMock(CallbackStub::class);
2015-07-10 22:15:42 +02:00
$mock->expects($this->exactly($count))
->method('__invoke');
return $mock;
}
2015-07-10 22:15:42 +02:00
/**
* Asserts that the given callback takes no more than $maxRunTime to run.
*
* @param callable $callback
* @param float $maxRunTime
* @param mixed[]|null $args Function arguments.
2015-07-10 22:15:42 +02:00
*/
public function assertRunTimeLessThan(callable $callback, $maxRunTime, array $args = null)
{
$this->assertRunTimeBetween($callback, 0, $maxRunTime, $args);
}
2015-07-10 22:15:42 +02:00
/**
* Asserts that the given callback takes more than $minRunTime to run.
*
* @param callable $callback
* @param float $minRunTime
* @param mixed[]|null $args Function arguments.
2015-07-10 22:15:42 +02:00
*/
public function assertRunTimeGreaterThan(callable $callback, $minRunTime, array $args = null)
{
$this->assertRunTimeBetween($callback, $minRunTime, 0, $args);
}
2015-07-10 22:15:42 +02:00
/**
* Asserts that the given callback takes between $minRunTime and $maxRunTime to execute.
* Rounds to the nearest 100 ms.
*
* @param callable $callback
* @param float $minRunTime
* @param float $maxRunTime
* @param mixed[]|null $args Function arguments.
2015-07-10 22:15:42 +02:00
*/
public function assertRunTimeBetween(callable $callback, $minRunTime, $maxRunTime, array $args = null)
{
$start = microtime(true);
2015-07-10 22:15:42 +02:00
call_user_func_array($callback, $args ?: []);
2015-07-10 22:15:42 +02:00
$runTime = round(microtime(true) - $start, self::RUNTIME_PRECISION);
2015-07-10 22:15:42 +02:00
if (0 < $maxRunTime) {
$this->assertLessThanOrEqual(
$maxRunTime,
$runTime,
sprintf('The run time of %.2fs was greater than the max run time of %.2fs.', $runTime, $maxRunTime)
);
}
2015-07-10 22:15:42 +02:00
if (0 < $minRunTime) {
$this->assertGreaterThanOrEqual(
$minRunTime,
$runTime,
sprintf('The run time of %.2fs was less than the min run time of %.2fs.', $runTime, $minRunTime)
);
}
}
final protected function doInFork(callable $function)
{
switch ($pid = pcntl_fork()) {
case -1:
$this->fail('Failed to fork process.');
break;
case 0:
$status = (int) $function();
exit($status);
default:
if (pcntl_waitpid($pid, $status) === -1) {
$this->fail('Failed to fork process.');
}
return $status;
}
}
2015-07-10 22:15:42 +02:00
}