File tree Expand file tree Collapse file tree 4 files changed +67
-6
lines changed Expand file tree Collapse file tree 4 files changed +67
-6
lines changed Original file line number Diff line number Diff line change 16
16
},
17
17
"autoload-dev" : {
18
18
"psr-4" : {
19
- "Interop\\ Async\\ " : " test"
19
+ "Interop\\ Async\\ Loop " : " test"
20
20
}
21
21
}
22
22
}
Original file line number Diff line number Diff line change @@ -22,15 +22,26 @@ final class Loop
22
22
private static $ level = 0 ;
23
23
24
24
/**
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.
28
32
*/
29
33
public static function setFactory (LoopDriverFactory $ factory = null )
30
34
{
35
+ if (self ::$ level > 0 ) {
36
+ throw new \RuntimeException ("Setting a new factory while running isn't allowed! " );
37
+ }
38
+
31
39
self ::$ factory = $ factory ;
32
40
33
- if (self ::$ level === 0 ) {
41
+ if ($ factory === null ) {
42
+ self ::$ driver = null ;
43
+ self ::$ registry = null ;
44
+ } else {
34
45
self ::$ driver = self ::createDriver ();
35
46
self ::$ registry = [];
36
47
}
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change 1
1
<?php
2
2
3
- namespace Interop \Async ;
3
+ namespace Interop \Async \Loop ;
4
+
5
+ use Interop \Async \Registry ;
4
6
5
7
class RegistryTest extends \PHPUnit_Framework_TestCase
6
8
{
You can’t perform that action at this time.
0 commit comments