Skip to content

Commit

Permalink
Merge 9f95589 into 008759d
Browse files Browse the repository at this point in the history
  • Loading branch information
sj-i authored Apr 14, 2022
2 parents 008759d + 9f95589 commit aa09c6f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 29 deletions.
18 changes: 13 additions & 5 deletions src/Lib/Loop/AsyncLoopMiddleware/NanoSleepMiddlewareAsync.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@

use PhpProfiler\Lib\Loop\AsyncLoopMiddlewareInterface;

use function hrtime;
use function time_nanosleep;

final class NanoSleepMiddlewareAsync implements AsyncLoopMiddlewareInterface
{
/** @param positive-int $sleep_nano_seconds */
Expand All @@ -26,11 +29,16 @@ public function __construct(

public function invoke(): \Generator
{
/**
* @psalm-suppress UnusedFunctionCall
* @psalm-suppress InvalidArgument
*/
time_nanosleep(0, $this->sleep_nano_seconds);
$start = hrtime(true);
yield from $this->chain->invoke();

$wait = $this->sleep_nano_seconds - hrtime(true) - $start;
if ($wait > 0) {
/**
* @psalm-suppress UnusedFunctionCall
* @psalm-suppress InvalidArgument
*/
time_nanosleep(0, $wait);
}
}
}
17 changes: 12 additions & 5 deletions src/Lib/Loop/LoopMiddleware/NanoSleepMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@

use PhpProfiler\Lib\Loop\LoopMiddlewareInterface;

use function hrtime;
use function time_nanosleep;

final class NanoSleepMiddleware implements LoopMiddlewareInterface
{
/** @param positive-int $sleep_nano_seconds */
Expand All @@ -26,14 +29,18 @@ public function __construct(

public function invoke(): bool
{
/**
* @psalm-suppress UnusedFunctionCall
* @psalm-suppress InvalidArgument
*/
time_nanosleep(0, $this->sleep_nano_seconds);
$start = hrtime(true);
if (!$this->chain->invoke()) {
return false;
}
$wait = $this->sleep_nano_seconds - hrtime(true) - $start;
if ($wait > 0) {
/**
* @psalm-suppress UnusedFunctionCall
* @psalm-suppress InvalidArgument
*/
time_nanosleep(0, $wait);
}
return true;
}
}
19 changes: 0 additions & 19 deletions tests/Lib/Loop/LoopMiddleware/NanoSleepLoopTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

namespace PhpProfiler\Lib\Loop\LoopMiddleware;

use LogicException;
use PHPUnit\Framework\TestCase;

class NanoSleepLoopTest extends TestCase
Expand All @@ -28,24 +27,6 @@ public function testReturnFalseIfChainFailed(): void
$this->assertFalse($nano_sleep_loop->invoke());
}

public function testSleepBeforeChainInvoked(): void
{
$nano_sleep_loop = new NanoSleepMiddleware(
1000 * 1000 * 1000,
new CallableMiddleware(function () {
throw new LogicException('should not be thrown');
})
);
if (version_compare('8.0.0', phpversion(), '<=')) {
$this->expectException(\ValueError::class);
$this->expectExceptionMessageMatches('/Nanoseconds was not in the range 0 to 999 999 999/');
} else {
$this->expectWarning();
$this->expectWarningMessageMatches('/nanoseconds was not in the range 0 to 999 999 999/');
}
$nano_sleep_loop->invoke();
}

public function testReturnTrueIfChainSucceed(): void
{
$nano_sleep_loop = new NanoSleepMiddleware(
Expand Down

0 comments on commit aa09c6f

Please sign in to comment.