11<?php
22
3- use ConnectionManager \Extra \ ConnectionManagerReject ;
3+ namespace ConnectionManager \Tests \ Extra ;
44
5- use ConnectionManager \Extra \ConnectionManagerDelay ;
5+ use ConnectionManager \Extra \ConnectionManagerReject ;
66use ConnectionManager \Extra \ConnectionManagerTimeout ;
7+ use React \Promise \Deferred ;
78use React \Promise \Promise ;
89
910class ConnectionManagerTimeoutTest extends TestCase
1011{
1112 private $ loop ;
1213
13- public function setUp ()
14+ /**
15+ * @before
16+ */
17+ public function setUpLoop ()
1418 {
15- $ this ->loop = React \EventLoop \Factory::create ();
19+ $ this ->loop = \ React \EventLoop \Factory::create ();
1620 }
1721
1822 public function testTimeoutOkay ()
@@ -27,12 +31,13 @@ public function testTimeoutOkay()
2731 $ promise ->then ($ this ->expectCallableOnce (), $ this ->expectCallableNever ());
2832 }
2933
30- public function testTimeoutExpire ()
34+ public function testTimeoutWillRejectPromiseWhenConnectorExceedsTimeLimit ()
3135 {
32- $ will = $ this ->createConnectionManagerMock (true );
33- $ wont = new ConnectionManagerDelay ($ will , 0.2 , $ this ->loop );
36+ $ connectionPromise = new Promise (function (){});
37+ $ connector = $ this ->getMockBuilder ('React\Socket\ConnectorInterface ' )->getMock ();
38+ $ connector ->expects ($ this ->once ())->method ('connect ' )->willReturn ($ connectionPromise );
3439
35- $ cm = new ConnectionManagerTimeout ($ wont , 0.1 , $ this ->loop );
40+ $ cm = new ConnectionManagerTimeout ($ connector , 0.1 , $ this ->loop );
3641
3742 $ promise = $ cm ->connect ('www.google.com:80 ' );
3843 $ this ->assertInstanceOf ('React\Promise\PromiseInterface ' , $ promise );
@@ -41,6 +46,25 @@ public function testTimeoutExpire()
4146 $ promise ->then ($ this ->expectCallableNever (), $ this ->expectCallableOnce ());
4247 }
4348
49+ public function testTimeoutWillEndConnectiontWhenConnectorResolvesAfterTimeoutFired ()
50+ {
51+ $ connectionDeferred = new Deferred ();
52+ $ connector = $ this ->getMockBuilder ('React\Socket\ConnectorInterface ' )->getMock ();
53+ $ connector ->expects ($ this ->once ())->method ('connect ' )->willReturn ($ connectionDeferred ->promise ());
54+
55+ $ cm = new ConnectionManagerTimeout ($ connector , 0.1 , $ this ->loop );
56+
57+ $ promise = $ cm ->connect ('www.google.com:80 ' );
58+ $ this ->assertInstanceOf ('React\Promise\PromiseInterface ' , $ promise );
59+
60+ $ this ->loop ->run ();
61+ $ promise ->then ($ this ->expectCallableNever (), $ this ->expectCallableOnce ());
62+
63+ $ connection = $ this ->getMockBuilder ('React\Socket\ConnectionInterface ' )->getMock ();
64+ $ connection ->expects ($ this ->once ())->method ('end ' );
65+ $ connectionDeferred ->resolve ($ connection );
66+ }
67+
4468 public function testTimeoutAbort ()
4569 {
4670 $ wont = new ConnectionManagerReject ();
0 commit comments