1
0
mirror of https://github.com/danog/amp.git synced 2025-01-22 13:21:16 +01:00

Forbid loop nesting

This commit is contained in:
Aaron Piotrowski 2020-10-03 09:42:29 -05:00
parent 45bd189e76
commit 249aa95256
No known key found for this signature in database
GPG Key ID: ADD1EF783EDE9EEB
2 changed files with 4 additions and 37 deletions

View File

@ -61,6 +61,10 @@ abstract class Driver implements \FiberScheduler
*/
public function run(): void
{
if ($this->running) {
throw new \Error("The loop was already running");
}
$this->running = true;
try {

View File

@ -53,43 +53,6 @@ abstract class DriverTest extends TestCase
$this->loop->run();
}
// Note: The running nesting is important for being able to continue actually still running loops (i.e. running flag set, if the driver has one) inside register_shutdown_function() for example
public function testLoopRunsCanBeConsecutiveAndNested(): void
{
$this->expectOutputString("123456");
$this->start(function (Driver $loop): void {
$loop->stop();
$loop->defer(function () use (&$run) {
echo $run = 1;
});
$loop->run();
if (!$run) {
$this->fail("A loop stop before a run must not impact that run");
}
$loop->defer(function () use ($loop): void {
$loop->run();
echo 5;
$loop->defer(function () use ($loop): void {
echo 6;
$loop->stop();
$loop->defer(function () {
$this->fail("A loop stopped at all levels must not execute further defers");
});
});
$loop->run();
});
$loop->defer(function () use ($loop): void {
echo 2;
$loop->defer(function () {
echo 4;
});
});
$loop->defer(function (): void {
echo 3;
});
});
}
public function testCorrectTimeoutIfBlockingBeforeActivate(): void
{
$start = 0;