@@ -22,15 +22,15 @@ public function setUp()
2222 public function testPassByResolverIfGivenIp ()
2323 {
2424 $ this ->resolver ->expects ($ this ->never ())->method ('resolve ' );
25- $ this ->tcp ->expects ($ this ->once ())->method ('create ' )->with ($ this ->equalTo ('127.0.0.1 ' ), $ this ->equalTo (80 ));
25+ $ this ->tcp ->expects ($ this ->once ())->method ('create ' )->with ($ this ->equalTo ('127.0.0.1 ' ), $ this ->equalTo (80 ))-> will ( $ this -> returnValue ( Promise \reject ())) ;
2626
2727 $ this ->connector ->create ('127.0.0.1 ' , 80 );
2828 }
2929
3030 public function testPassThroughResolverIfGivenHost ()
3131 {
3232 $ this ->resolver ->expects ($ this ->once ())->method ('resolve ' )->with ($ this ->equalTo ('google.com ' ))->will ($ this ->returnValue (Promise \resolve ('1.2.3.4 ' )));
33- $ this ->tcp ->expects ($ this ->once ())->method ('create ' )->with ($ this ->equalTo ('1.2.3.4 ' ), $ this ->equalTo (80 ));
33+ $ this ->tcp ->expects ($ this ->once ())->method ('create ' )->with ($ this ->equalTo ('1.2.3.4 ' ), $ this ->equalTo (80 ))-> will ( $ this -> returnValue ( Promise \reject ())) ;
3434
3535 $ this ->connector ->create ('google.com ' , 80 );
3636 }
@@ -42,4 +42,46 @@ public function testSkipConnectionIfDnsFails()
4242
4343 $ this ->connector ->create ('example.invalid ' , 80 );
4444 }
45+
46+ public function testCancelDuringDnsCancelsDnsAndDoesNotStartTcpConnection ()
47+ {
48+ $ pending = new Promise \Promise (function () { }, $ this ->expectCallableOnce ());
49+ $ this ->resolver ->expects ($ this ->once ())->method ('resolve ' )->with ($ this ->equalTo ('example.com ' ))->will ($ this ->returnValue ($ pending ));
50+ $ this ->tcp ->expects ($ this ->never ())->method ('resolve ' );
51+
52+ $ promise = $ this ->connector ->create ('example.com ' , 80 );
53+ $ promise ->cancel ();
54+
55+ $ promise ->then ($ this ->expectCallableNever (), $ this ->expectCallableOnce ());
56+ }
57+
58+ public function testCancelDuringTcpConnectionCancelsTcpConnection ()
59+ {
60+ $ pending = new Promise \Promise (function () { }, $ this ->expectCallableOnce ());
61+ $ this ->resolver ->expects ($ this ->once ())->method ('resolve ' )->with ($ this ->equalTo ('example.com ' ))->will ($ this ->returnValue (Promise \resolve ('1.2.3.4 ' )));
62+ $ this ->tcp ->expects ($ this ->once ())->method ('create ' )->with ($ this ->equalTo ('1.2.3.4 ' ), $ this ->equalTo (80 ))->will ($ this ->returnValue ($ pending ));
63+
64+ $ promise = $ this ->connector ->create ('example.com ' , 80 );
65+ $ promise ->cancel ();
66+
67+ $ promise ->then ($ this ->expectCallableNever (), $ this ->expectCallableOnce ());
68+ }
69+
70+ public function testCancelClosesStreamIfTcpResolvesDespiteCancellation ()
71+ {
72+ $ stream = $ this ->getMockBuilder ('React\Stream\Stream ' )->disableOriginalConstructor ()->setMethods (array ('close ' ))->getMock ();
73+ $ stream ->expects ($ this ->once ())->method ('close ' );
74+
75+ $ pending = new Promise \Promise (function () { }, function ($ resolve ) use ($ stream ) {
76+ $ resolve ($ stream );
77+ });
78+
79+ $ this ->resolver ->expects ($ this ->once ())->method ('resolve ' )->with ($ this ->equalTo ('example.com ' ))->will ($ this ->returnValue (Promise \resolve ('1.2.3.4 ' )));
80+ $ this ->tcp ->expects ($ this ->once ())->method ('create ' )->with ($ this ->equalTo ('1.2.3.4 ' ), $ this ->equalTo (80 ))->will ($ this ->returnValue ($ pending ));
81+
82+ $ promise = $ this ->connector ->create ('example.com ' , 80 );
83+ $ promise ->cancel ();
84+
85+ $ promise ->then ($ this ->expectCallableNever (), $ this ->expectCallableOnce ());
86+ }
4587}
0 commit comments