Skip to content

Commit 5a82cd9

Browse files
committed
Merge pull request #67 from async-interop/defer-callback
Put Loop::execute() $callback into Driver::defer()
2 parents 3b338b5 + b197b2c commit 5a82cd9

File tree

4 files changed

+53
-8
lines changed

4 files changed

+53
-8
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
},
1717
"autoload-dev": {
1818
"psr-4": {
19-
"Interop\\Async\\Loop\\": "test"
19+
"Interop\\Async\\Loop\\Test\\": "test"
2020
}
2121
}
2222
}

src/Loop.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,7 @@ public static function execute(callable $callback, Driver $driver = null)
5959
self::$level++;
6060

6161
try {
62-
$callback();
63-
62+
self::$driver->defer($callback);
6463
self::$driver->run();
6564
} finally {
6665
self::$driver = $previousDriver;

test/DummyDriver.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
namespace Interop\Async\Loop\Test;
4+
5+
class DummyDriver implements \Interop\Async\Loop\Driver
6+
{
7+
public $defers;
8+
public $handler;
9+
public static $id = "a";
10+
11+
public function run() {
12+
while (list($defer, $data) = array_shift($this->defers)) {
13+
try {
14+
$defer(self::$id++, $data);
15+
} catch (Exception $e) {
16+
if ($handler = $this->handler) {
17+
$handler($e);
18+
} else {
19+
throw $e;
20+
}
21+
}
22+
}
23+
}
24+
25+
public function defer(callable $callback, $data = null) {
26+
$this->defers[] = [$callback, $data];
27+
}
28+
29+
public function setErrorHandler(callable $callback = null) {
30+
$this->handler = $callback;
31+
}
32+
33+
public function stop() {}
34+
public function delay($delay, callable $callback, $data = null) { return self::$id++; }
35+
public function repeat($interval, callable $callback, $data = null) { return self::$id++; }
36+
public function onReadable($stream, callable $callback, $data = null) { return self::$id++; }
37+
public function onWritable($stream, callable $callback, $data = null) { return self::$id++; }
38+
public function onSignal($signo, callable $callback, $data = null) { return self::$id++; }
39+
public function enable($watcherId) {}
40+
public function disable($watcherId) {}
41+
public function cancel($watcherId) {}
42+
public function reference($watcherId) {}
43+
public function unreference($watcherId) {}
44+
public function info() {}
45+
public function getHandle() {}
46+
}

test/LoopTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Interop\Async\Loop;
3+
namespace Interop\Async\Loop\Test;
44

55
use Interop\Async\Loop;
66

@@ -16,9 +16,9 @@ protected function setUp() {
1616
* @expectedExceptionMessage new factory while running isn't allowed
1717
*/
1818
public function setFactoryFailsIfRunning() {
19-
$driver = $this->getMockBuilder(Driver::class)->getMock();
19+
$driver = new DummyDriver;
2020

21-
$factory = $this->getMockBuilder(DriverFactory::class)->getMock();
21+
$factory = $this->getMockBuilder(Loop\DriverFactory::class)->getMock();
2222
$factory->method("create")->willReturn($driver);
2323

2424
Loop::setFactory($factory);
@@ -30,8 +30,8 @@ public function setFactoryFailsIfRunning() {
3030

3131
/** @test */
3232
public function executeStackReturnsScopedDriver() {
33-
$driver1 = $this->getMockBuilder(Driver::class)->getMock();
34-
$driver2 = $this->getMockBuilder(Driver::class)->getMock();
33+
$driver1 = new DummyDriver;
34+
$driver2 = new DummyDriver;
3535

3636
Loop::execute(function () use ($driver1, $driver2) {
3737
$this->assertSame($driver1, Loop::get());

0 commit comments

Comments
 (0)