@@ -293,6 +293,44 @@ public function testConnectWillStartAndCancelResolutionTimerAndStartAttemptTimer
293293 $ deferred ->resolve (array ('::1 ' ));
294294 }
295295
296+ public function testConnectWillRejectWhenOnlyTcpConnectionRejectsAndCancelNextAttemptTimerImmediately ()
297+ {
298+ $ timer = $ this ->getMockBuilder ('React\EventLoop\TimerInterface ' )->getMock ();
299+ $ loop = $ this ->getMockBuilder ('React\EventLoop\LoopInterface ' )->getMock ();
300+ $ loop ->expects ($ this ->once ())->method ('addTimer ' )->with (0.1 , $ this ->anything ())->willReturn ($ timer );
301+ $ loop ->expects ($ this ->once ())->method ('cancelTimer ' )->with ($ timer );
302+
303+ $ deferred = new Deferred ();
304+ $ connector = $ this ->getMockBuilder ('React\Socket\ConnectorInterface ' )->getMock ();
305+ $ connector ->expects ($ this ->once ())->method ('connect ' )->with ('tcp://[::1]:80?hostname=reactphp.org ' )->willReturn ($ deferred ->promise ());
306+
307+ $ resolver = $ this ->getMockBuilder ('React\Dns\Resolver\ResolverInterface ' )->getMock ();
308+ $ resolver ->expects ($ this ->exactly (2 ))->method ('resolveAll ' )->withConsecutive (
309+ array ('reactphp.org ' , Message::TYPE_AAAA ),
310+ array ('reactphp.org ' , Message::TYPE_A )
311+ )->willReturnOnConsecutiveCalls (
312+ \React \Promise \resolve (array ('::1 ' )),
313+ \React \Promise \reject (new \RuntimeException ('ignored ' ))
314+ );
315+
316+ $ uri = 'tcp://reactphp.org:80 ' ;
317+ $ host = 'reactphp.org ' ;
318+ $ parts = parse_url ($ uri );
319+
320+ $ builder = new HappyEyeBallsConnectionBuilder ($ loop , $ connector , $ resolver , $ uri , $ host , $ parts );
321+
322+ $ promise = $ builder ->connect ();
323+ $ deferred ->reject (new \RuntimeException ('Connection refused ' ));
324+
325+ $ exception = null ;
326+ $ promise ->then (null , function ($ e ) use (&$ exception ) {
327+ $ exception = $ e ;
328+ });
329+
330+ $ this ->assertInstanceOf ('RuntimeException ' , $ exception );
331+ $ this ->assertEquals ('Connection to tcp://reactphp.org:80 failed: Connection refused ' , $ exception ->getMessage ());
332+ }
333+
296334 public function testCancelConnectWillRejectPromiseAndCancelBothDnsLookups ()
297335 {
298336 $ loop = $ this ->getMockBuilder ('React\EventLoop\LoopInterface ' )->getMock ();
0 commit comments