1
0
mirror of https://github.com/danog/amp.git synced 2025-01-22 05:11:42 +01:00

Put Loop::execute() $callback into Driver::defer()

This also ensures that potential throwing in the callback will be handled inside a potential error handler instead of immediately falling through.
This commit is contained in:
Bob Weinand 2016-05-26 23:25:15 +02:00
parent 64494951ff
commit b197b2c07d
4 changed files with 53 additions and 8 deletions

View File

@ -16,7 +16,7 @@
}, },
"autoload-dev": { "autoload-dev": {
"psr-4": { "psr-4": {
"Interop\\Async\\Loop\\": "test" "Interop\\Async\\Loop\\Test\\": "test"
} }
} }
} }

View File

@ -72,8 +72,7 @@ final class Loop
self::$level++; self::$level++;
try { try {
$callback(); self::$driver->defer($callback);
self::$driver->run(); self::$driver->run();
} finally { } finally {
self::$driver = $previousDriver; self::$driver = $previousDriver;

46
test/DummyDriver.php Normal file
View File

@ -0,0 +1,46 @@
<?php
namespace Interop\Async\Loop\Test;
class DummyDriver implements \Interop\Async\Loop\Driver
{
public $defers;
public $handler;
public static $id = "a";
public function run() {
while (list($defer, $data) = array_shift($this->defers)) {
try {
$defer(self::$id++, $data);
} catch (Exception $e) {
if ($handler = $this->handler) {
$handler($e);
} else {
throw $e;
}
}
}
}
public function defer(callable $callback, $data = null) {
$this->defers[] = [$callback, $data];
}
public function setErrorHandler(callable $callback = null) {
$this->handler = $callback;
}
public function stop() {}
public function delay($delay, callable $callback, $data = null) { return self::$id++; }
public function repeat($interval, callable $callback, $data = null) { return self::$id++; }
public function onReadable($stream, callable $callback, $data = null) { return self::$id++; }
public function onWritable($stream, callable $callback, $data = null) { return self::$id++; }
public function onSignal($signo, callable $callback, $data = null) { return self::$id++; }
public function enable($watcherId) {}
public function disable($watcherId) {}
public function cancel($watcherId) {}
public function reference($watcherId) {}
public function unreference($watcherId) {}
public function info() {}
public function getHandle() {}
}

View File

@ -1,6 +1,6 @@
<?php <?php
namespace Interop\Async\Loop; namespace Interop\Async\Loop\Test;
use Interop\Async\Loop; use Interop\Async\Loop;
@ -16,9 +16,9 @@ class LoopTest extends \PHPUnit_Framework_TestCase
* @expectedExceptionMessage new factory while running isn't allowed * @expectedExceptionMessage new factory while running isn't allowed
*/ */
public function setFactoryFailsIfRunning() { public function setFactoryFailsIfRunning() {
$driver = $this->getMockBuilder(Driver::class)->getMock(); $driver = new DummyDriver;
$factory = $this->getMockBuilder(DriverFactory::class)->getMock(); $factory = $this->getMockBuilder(Loop\DriverFactory::class)->getMock();
$factory->method("create")->willReturn($driver); $factory->method("create")->willReturn($driver);
Loop::setFactory($factory); Loop::setFactory($factory);
@ -30,8 +30,8 @@ class LoopTest extends \PHPUnit_Framework_TestCase
/** @test */ /** @test */
public function executeStackReturnsScopedDriver() { public function executeStackReturnsScopedDriver() {
$driver1 = $this->getMockBuilder(Driver::class)->getMock(); $driver1 = new DummyDriver;
$driver2 = $this->getMockBuilder(Driver::class)->getMock(); $driver2 = new DummyDriver;
Loop::execute(function () use ($driver1, $driver2) { Loop::execute(function () use ($driver1, $driver2) {
$this->assertSame($driver1, Loop::get()); $this->assertSame($driver1, Loop::get());