File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1818 "evenement/evenement" : " ~1.0|~2.0" ,
1919 "react/event-loop" : " >=0.2, <0.5" ,
2020 "react/dns" : " >=0.2, <0.5" ,
21- "react/promise" : " ~2.0 |~1.1 "
21+ "react/promise" : " ~2.1 |~1.2 "
2222 },
2323 "require-dev" : {
2424 "clue/block-react" : " ~1.0"
Original file line number Diff line number Diff line change 77use React \Promise ;
88use React \Datagram \Socket ;
99use \Exception ;
10+ use React \Promise \CancellablePromiseInterface ;
1011
1112class Factory
1213{
@@ -108,6 +109,23 @@ protected function resolveHost($host)
108109 return Promise \reject (new Exception ('No resolver given in order to get IP address for given hostname ' ));
109110 }
110111
111- return $ this ->resolver ->resolve ($ host );
112+ $ promise = $ this ->resolver ->resolve ($ host );
113+
114+ // wrap DNS lookup in order to control cancellation behavior
115+ return new Promise \Promise (
116+ function ($ resolve , $ reject ) use ($ promise ) {
117+ // forward promise resolution
118+ $ promise ->then ($ resolve , $ reject );
119+ },
120+ function ($ _ , $ reject ) use ($ promise ) {
121+ // reject with custom message once cancelled
122+ $ reject (new \RuntimeException ('Cancelled creating socket during DNS lookup ' ));
123+
124+ // (try to) cancel pending DNS lookup, otherwise ignoring its results
125+ if ($ promise instanceof CancellablePromiseInterface) {
126+ $ promise ->cancel ();
127+ }
128+ }
129+ );
112130 }
113131}
Original file line number Diff line number Diff line change @@ -119,4 +119,28 @@ public function testCreateServerWithInvalidHostnameWillReject()
119119 {
120120 Block \await ($ this ->factory ->createServer ('///// ' ), $ this ->loop );
121121 }
122+
123+ public function testCancelCreateClientWithCancellableHostnameResolver ()
124+ {
125+ $ promise = new Promise \Promise (function () { }, $ this ->expectCallableOnce ());
126+ $ this ->resolver ->expects ($ this ->once ())->method ('resolve ' )->with ('example.com ' )->willReturn ($ promise );
127+
128+ $ promise = $ this ->factory ->createClient ('example.com:0 ' );
129+ $ promise ->cancel ();
130+
131+ $ this ->setExpectedException ('RuntimeException ' );
132+ Block \await ($ promise , $ this ->loop );
133+ }
134+
135+ public function testCancelCreateClientWithUncancellableHostnameResolver ()
136+ {
137+ $ promise = $ this ->getMock ('React\Promise\PromiseInterface ' );
138+ $ this ->resolver ->expects ($ this ->once ())->method ('resolve ' )->with ('example.com ' )->willReturn ($ promise );
139+
140+ $ promise = $ this ->factory ->createClient ('example.com:0 ' );
141+ $ promise ->cancel ();
142+
143+ $ this ->setExpectedException ('RuntimeException ' );
144+ Block \await ($ promise , $ this ->loop );
145+ }
122146}
You can’t perform that action at this time.
0 commit comments