Skip to content

Commit 12102e7

Browse files
author
Aaron Piotrowski
committed
Merge pull request #59 from async-interop/fix-running
Fix running flag, factory reset, etc.
2 parents 5859044 + 52a13b6 commit 12102e7

File tree

4 files changed

+71
-10
lines changed

4 files changed

+71
-10
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\\": "test"
19+
"Interop\\Async\\Loop\\": "test"
2020
}
2121
}
2222
}

src/Loop.php

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,31 @@ final class Loop
1717
private static $driver = null;
1818

1919
/**
20-
* @var bool
20+
* @var int
2121
*/
22-
private static $running = false;
22+
private static $level = 0;
2323

2424
/**
25-
* Set the factory to be used to create a driver if none is passed to
26-
* self::execute. A default driver will be created if none is running
27-
* to support synchronous waits in traditional applications.
25+
* Set the factory to be used to create a default drivers.
26+
*
27+
* Setting a factory is only allowed as long as no loop is currently running.
28+
* Passing null will reset the default driver and remove the factory.
29+
*
30+
* The factory will be invoked if none is passed to Loop::execute. A default driver will be created to support
31+
* synchronous waits in traditional applications.
2832
*/
2933
public static function setFactory(LoopDriverFactory $factory = null)
3034
{
35+
if (self::$level > 0) {
36+
throw new \RuntimeException("Setting a new factory while running isn't allowed!");
37+
}
38+
3139
self::$factory = $factory;
3240

33-
if (!self::$running) {
41+
if ($factory === null) {
42+
self::$driver = null;
43+
self::$registry = null;
44+
} else {
3445
self::$driver = self::createDriver();
3546
self::$registry = [];
3647
}
@@ -53,7 +64,7 @@ public static function execute(callable $callback, LoopDriver $driver = null)
5364

5465
self::$driver = $driver;
5566
self::$registry = [];
56-
self::$running = true;
67+
self::$level++;
5768

5869
try {
5970
$callback();
@@ -62,7 +73,7 @@ public static function execute(callable $callback, LoopDriver $driver = null)
6273
} finally {
6374
self::$driver = $previousDriver;
6475
self::$registry = $previousRegistry;
65-
self::$running = false;
76+
self::$level--;
6677
}
6778
}
6879

test/LoopTest.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
namespace Interop\Async\Loop;
4+
5+
use Interop\Async\Loop;
6+
use Interop\Async\LoopDriver;
7+
use Interop\Async\LoopDriverFactory;
8+
9+
class LoopTest extends \PHPUnit_Framework_TestCase
10+
{
11+
protected function setUp() {
12+
Loop::setFactory(null);
13+
}
14+
15+
/**
16+
* @test
17+
* @expectedException \RuntimeException
18+
* @expectedExceptionMessage new factory while running isn't allowed
19+
*/
20+
public function setFactoryFailsIfRunning() {
21+
$driver = $this->getMockBuilder(LoopDriver::class)->getMock();
22+
23+
$factory = $this->getMockBuilder(LoopDriverFactory::class)->getMock();
24+
$factory->method("create")->willReturn($driver);
25+
26+
Loop::setFactory($factory);
27+
28+
Loop::execute(function () use ($factory) {
29+
Loop::setFactory($factory);
30+
});
31+
}
32+
33+
/** @test */
34+
public function executeStackReturnsScopedDriver() {
35+
$driver1 = $this->getMockBuilder(LoopDriver::class)->getMock();
36+
$driver2 = $this->getMockBuilder(LoopDriver::class)->getMock();
37+
38+
Loop::execute(function () use ($driver1, $driver2) {
39+
$this->assertSame($driver1, Loop::get());
40+
41+
Loop::execute(function () use ($driver2) {
42+
$this->assertSame($driver2, Loop::get());
43+
}, $driver2);
44+
45+
$this->assertSame($driver1, Loop::get());
46+
}, $driver1);
47+
}
48+
}

test/RegistryTest.php

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

3-
namespace Interop\Async;
3+
namespace Interop\Async\Loop;
4+
5+
use Interop\Async\Registry;
46

57
class RegistryTest extends \PHPUnit_Framework_TestCase
68
{

0 commit comments

Comments
 (0)