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-09-26 06:41:15 +02:00
|
|
|
|
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-09-26 06:41:15 +02:00
|
|
|
|
2015-07-10 22:15:42 +02:00
|
|
|
$mock->expects($this->exactly($count))
|
|
|
|
->method('__invoke');
|
|
|
|
|
|
|
|
return $mock;
|
|
|
|
}
|
2015-09-26 06:41:15 +02:00
|
|
|
|
2015-07-10 22:15:42 +02:00
|
|
|
/**
|
|
|
|
* Asserts that the given callback takes no more than $maxRunTime to run.
|
|
|
|
*
|
2015-09-26 06:41:15 +02:00
|
|
|
* @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-09-26 06:41:15 +02:00
|
|
|
|
2015-07-10 22:15:42 +02:00
|
|
|
/**
|
|
|
|
* Asserts that the given callback takes more than $minRunTime to run.
|
|
|
|
*
|
2015-09-26 06:41:15 +02:00
|
|
|
* @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-09-26 06:41:15 +02:00
|
|
|
|
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.
|
|
|
|
*
|
2015-09-26 06:41:15 +02:00
|
|
|
* @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-09-26 06:41:15 +02:00
|
|
|
|
2015-07-10 22:15:42 +02:00
|
|
|
call_user_func_array($callback, $args ?: []);
|
2015-09-26 06:41:15 +02:00
|
|
|
|
2015-07-10 22:15:42 +02:00
|
|
|
$runTime = round(microtime(true) - $start, self::RUNTIME_PRECISION);
|
2015-09-26 06:41:15 +02:00
|
|
|
|
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-09-26 06:41:15 +02:00
|
|
|
|
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)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2015-09-26 06:41:15 +02:00
|
|
|
|
|
|
|
final protected function doInFork(callable $function)
|
|
|
|
{
|
2015-09-27 07:20:06 +02:00
|
|
|
switch ($pid = pcntl_fork()) {
|
2015-09-26 06:41:15 +02:00
|
|
|
case -1:
|
|
|
|
$this->fail('Failed to fork process.');
|
|
|
|
break;
|
|
|
|
case 0:
|
2015-09-27 07:20:06 +02:00
|
|
|
$status = (int) $function();
|
|
|
|
exit($status);
|
2015-09-26 06:41:15 +02:00
|
|
|
default:
|
2015-09-27 07:20:06 +02:00
|
|
|
if (pcntl_waitpid($pid, $status) === -1) {
|
2015-09-26 08:30:44 +02:00
|
|
|
$this->fail('Failed to fork process.');
|
|
|
|
}
|
2015-09-26 06:41:15 +02:00
|
|
|
return $status;
|
|
|
|
}
|
|
|
|
}
|
2015-07-10 22:15:42 +02:00
|
|
|
}
|