HappyEyeBallsConnectorTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. <?php
  2. namespace React\Tests\Socket;
  3. use React\Dns\Model\Message;
  4. use React\EventLoop\StreamSelectLoop;
  5. use React\Promise;
  6. use React\Promise\Deferred;
  7. use React\Socket\HappyEyeBallsConnector;
  8. class HappyEyeBallsConnectorTest extends TestCase
  9. {
  10. private $loop;
  11. private $tcp;
  12. private $resolver;
  13. private $connector;
  14. private $connection;
  15. /**
  16. * @before
  17. */
  18. public function setUpMocks()
  19. {
  20. $this->loop = new TimerSpeedUpEventLoop(new StreamSelectLoop());
  21. $this->tcp = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock();
  22. $this->resolver = $this->getMockBuilder('React\Dns\Resolver\ResolverInterface')->disableOriginalConstructor()->getMock();
  23. $this->connection = $this->getMockBuilder('React\Socket\ConnectionInterface')->getMock();
  24. $this->connector = new HappyEyeBallsConnector($this->loop, $this->tcp, $this->resolver);
  25. }
  26. public function testConstructWithoutLoopAssignsLoopAutomatically()
  27. {
  28. $connector = new HappyEyeBallsConnector(null, $this->tcp, $this->resolver);
  29. $ref = new \ReflectionProperty($connector, 'loop');
  30. $ref->setAccessible(true);
  31. $loop = $ref->getValue($connector);
  32. $this->assertInstanceOf('React\EventLoop\LoopInterface', $loop);
  33. }
  34. public function testConstructWithInvalidLoopThrows()
  35. {
  36. $this->setExpectedException('InvalidArgumentException', 'Argument #1 ($loop) expected null|React\EventLoop\LoopInterface');
  37. new HappyEyeBallsConnector('loop', $this->tcp, $this->resolver);
  38. }
  39. public function testConstructWithoutRequiredConnectorThrows()
  40. {
  41. $this->setExpectedException('InvalidArgumentException', 'Argument #2 ($connector) expected React\Socket\ConnectorInterface');
  42. new HappyEyeBallsConnector(null, null, $this->resolver);
  43. }
  44. public function testConstructWithoutRequiredResolverThrows()
  45. {
  46. $this->setExpectedException('InvalidArgumentException', 'Argument #3 ($resolver) expected React\Dns\Resolver\ResolverInterface');
  47. new HappyEyeBallsConnector(null, $this->tcp);
  48. }
  49. public function testHappyFlow()
  50. {
  51. $first = new Deferred();
  52. $this->resolver->expects($this->exactly(2))->method('resolveAll')->with($this->equalTo('example.com'), $this->anything())->willReturn($first->promise());
  53. $connection = $this->getMockBuilder('React\Socket\ConnectionInterface')->getMock();
  54. $this->tcp->expects($this->exactly(1))->method('connect')->with($this->equalTo('1.2.3.4:80?hostname=example.com'))->willReturn(Promise\resolve($connection));
  55. $promise = $this->connector->connect('example.com:80');
  56. $first->resolve(array('1.2.3.4'));
  57. $resolvedConnection = null;
  58. $promise->then(function ($value) use (&$resolvedConnection) {
  59. $resolvedConnection = $value;
  60. });
  61. self::assertSame($connection, $resolvedConnection);
  62. }
  63. public function testThatAnyOtherPendingConnectionAttemptsWillBeCanceledOnceAConnectionHasBeenEstablished()
  64. {
  65. $connection = $this->getMockBuilder('React\Socket\ConnectionInterface')->getMock();
  66. $lookupAttempts = array(
  67. Promise\reject(new \Exception('error')),
  68. Promise\resolve(array('1.2.3.4', '5.6.7.8', '9.10.11.12')),
  69. );
  70. $connectionAttempts = array(
  71. new Promise\Promise(function () {}, $this->expectCallableOnce()),
  72. Promise\resolve($connection),
  73. new Promise\Promise(function () {}, $this->expectCallableNever()),
  74. );
  75. $this->resolver->expects($this->exactly(2))->method('resolveAll')->with($this->equalTo('example.com'), $this->anything())->will($this->returnCallback(function () use (&$lookupAttempts) {
  76. return array_shift($lookupAttempts);
  77. }));
  78. $this->tcp->expects($this->exactly(2))->method('connect')->with($this->isType('string'))->will($this->returnCallback(function () use (&$connectionAttempts) {
  79. return array_shift($connectionAttempts);
  80. }));
  81. $promise = $this->connector->connect('example.com:80');
  82. $this->loop->run();
  83. $resolvedConnection = null;
  84. $promise->then(function ($value) use (&$resolvedConnection) {
  85. $resolvedConnection = $value;
  86. });
  87. self::assertSame($connection, $resolvedConnection);
  88. }
  89. public function testPassByResolverIfGivenIp()
  90. {
  91. $this->resolver->expects($this->never())->method('resolveAll');
  92. $this->tcp->expects($this->once())->method('connect')->with($this->equalTo('127.0.0.1:80'))->will($this->returnValue(Promise\resolve(null)));
  93. $this->connector->connect('127.0.0.1:80');
  94. $this->loop->run();
  95. }
  96. public function testPassByResolverIfGivenIpv6()
  97. {
  98. $this->resolver->expects($this->never())->method('resolveAll');
  99. $this->tcp->expects($this->once())->method('connect')->with($this->equalTo('[::1]:80'))->will($this->returnValue(Promise\reject(new \Exception('reject'))));
  100. $promise = $this->connector->connect('[::1]:80');
  101. $promise->then(null, $this->expectCallableOnce()); // avoid reporting unhandled rejection
  102. $this->loop->run();
  103. }
  104. public function testPassThroughResolverIfGivenHost()
  105. {
  106. $this->resolver->expects($this->exactly(2))->method('resolveAll')->with($this->equalTo('google.com'), $this->anything())->will($this->returnValue(Promise\resolve(array('1.2.3.4'))));
  107. $this->tcp->expects($this->exactly(2))->method('connect')->with($this->equalTo('1.2.3.4:80?hostname=google.com'))->will($this->returnValue(Promise\reject(new \Exception('reject'))));
  108. $promise = $this->connector->connect('google.com:80');
  109. $promise->then(null, $this->expectCallableOnce()); // avoid reporting unhandled rejection
  110. $this->loop->run();
  111. }
  112. public function testPassThroughResolverIfGivenHostWhichResolvesToIpv6()
  113. {
  114. $this->resolver->expects($this->exactly(2))->method('resolveAll')->with($this->equalTo('google.com'), $this->anything())->will($this->returnValue(Promise\resolve(array('::1'))));
  115. $this->tcp->expects($this->exactly(2))->method('connect')->with($this->equalTo('[::1]:80?hostname=google.com'))->will($this->returnValue(Promise\reject(new \Exception('reject'))));
  116. $promise = $this->connector->connect('google.com:80');
  117. $promise->then(null, $this->expectCallableOnce()); // avoid reporting unhandled rejection
  118. $this->loop->run();
  119. }
  120. public function testPassByResolverIfGivenCompleteUri()
  121. {
  122. $this->resolver->expects($this->never())->method('resolveAll');
  123. $this->tcp->expects($this->once())->method('connect')->with($this->equalTo('scheme://127.0.0.1:80/path?query#fragment'))->will($this->returnValue(Promise\reject(new \Exception('reject'))));
  124. $promise = $this->connector->connect('scheme://127.0.0.1:80/path?query#fragment');
  125. $promise->then(null, $this->expectCallableOnce()); // avoid reporting unhandled rejection
  126. $this->loop->run();
  127. }
  128. public function testPassThroughResolverIfGivenCompleteUri()
  129. {
  130. $this->resolver->expects($this->exactly(2))->method('resolveAll')->with($this->equalTo('google.com'), $this->anything())->will($this->returnValue(Promise\resolve(array('1.2.3.4'))));
  131. $this->tcp->expects($this->exactly(2))->method('connect')->with($this->equalTo('scheme://1.2.3.4:80/path?query&hostname=google.com#fragment'))->will($this->returnValue(Promise\reject(new \Exception('reject'))));
  132. $promise = $this->connector->connect('scheme://google.com:80/path?query#fragment');
  133. $promise->then(null, $this->expectCallableOnce()); // avoid reporting unhandled rejection
  134. $this->loop->run();
  135. }
  136. public function testPassThroughResolverIfGivenExplicitHost()
  137. {
  138. $this->resolver->expects($this->exactly(2))->method('resolveAll')->with($this->equalTo('google.com'), $this->anything())->will($this->returnValue(Promise\resolve(array('1.2.3.4'))));
  139. $this->tcp->expects($this->exactly(2))->method('connect')->with($this->equalTo('scheme://1.2.3.4:80/?hostname=google.de'))->will($this->returnValue(Promise\reject(new \Exception('reject'))));
  140. $promise = $this->connector->connect('scheme://google.com:80/?hostname=google.de');
  141. $promise->then(null, $this->expectCallableOnce()); // avoid reporting unhandled rejection
  142. $this->loop->run();
  143. }
  144. /**
  145. * @dataProvider provideIpvAddresses
  146. */
  147. public function testIpv6ResolvesFirstSoIsTheFirstToConnect(array $ipv6, array $ipv4)
  148. {
  149. $deferred = new Deferred();
  150. $this->resolver->expects($this->exactly(2))->method('resolveAll')->withConsecutive(
  151. array('google.com', Message::TYPE_AAAA),
  152. array('google.com', Message::TYPE_A)
  153. )->willReturnOnConsecutiveCalls(
  154. $this->returnValue(Promise\resolve($ipv6)),
  155. $this->returnValue($deferred->promise())
  156. );
  157. $this->tcp->expects($this->any())->method('connect')->with($this->stringContains(']:80/?hostname=google.com'))->will($this->returnValue(Promise\reject(new \Exception('reject'))));
  158. $this->connector->connect('scheme://google.com:80/?hostname=google.com');
  159. $this->loop->addTimer(0.07, function () use ($deferred) {
  160. $deferred->reject(new \RuntimeException());
  161. });
  162. $this->loop->run();
  163. }
  164. /**
  165. * @dataProvider provideIpvAddresses
  166. */
  167. public function testIpv6DoesntResolvesWhileIpv4DoesFirstSoIpv4Connects(array $ipv6, array $ipv4)
  168. {
  169. $deferred = new Deferred();
  170. $this->resolver->expects($this->exactly(2))->method('resolveAll')->withConsecutive(
  171. array('google.com', Message::TYPE_AAAA),
  172. array('google.com', Message::TYPE_A)
  173. )->willReturnOnConsecutiveCalls(
  174. $this->returnValue($deferred->promise()),
  175. $this->returnValue(Promise\resolve($ipv4))
  176. );
  177. $this->tcp->expects($this->any())->method('connect')->with($this->stringContains(':80/?hostname=google.com'))->will($this->returnValue(Promise\reject(new \Exception('reject'))));
  178. $this->connector->connect('scheme://google.com:80/?hostname=google.com');
  179. $this->loop->addTimer(0.07, function () use ($deferred) {
  180. $deferred->reject(new \RuntimeException());
  181. });
  182. $this->loop->run();
  183. }
  184. public function testRejectsImmediatelyIfUriIsInvalid()
  185. {
  186. $this->resolver->expects($this->never())->method('resolveAll');
  187. $this->tcp->expects($this->never())->method('connect');
  188. $promise = $this->connector->connect('////');
  189. $promise->then(null, $this->expectCallableOnceWithException(
  190. 'InvalidArgumentException',
  191. 'Given URI "////" is invalid (EINVAL)',
  192. defined('SOCKET_EINVAL') ? SOCKET_EINVAL : (defined('PCNTL_EINVAL') ? PCNTL_EINVAL : 22)
  193. ));
  194. }
  195. public function testRejectsWithTcpConnectorRejectionIfGivenIp()
  196. {
  197. $that = $this;
  198. $promise = Promise\reject(new \RuntimeException('Connection failed'));
  199. $this->resolver->expects($this->never())->method('resolveAll');
  200. $this->tcp->expects($this->once())->method('connect')->with($this->equalTo('1.2.3.4:80'))->willReturn($promise);
  201. $promise = $this->connector->connect('1.2.3.4:80');
  202. $this->loop->addTimer(0.5, function () use ($that, $promise) {
  203. $promise->cancel();
  204. $that->throwRejection($promise);
  205. });
  206. $this->setExpectedException('RuntimeException', 'Connection failed');
  207. $this->loop->run();
  208. }
  209. public function testSkipConnectionIfDnsFails()
  210. {
  211. $that = $this;
  212. $this->resolver->expects($this->exactly(2))->method('resolveAll')->with($this->equalTo('example.invalid'), $this->anything())->willReturn(Promise\reject(new \RuntimeException('DNS error')));
  213. $this->tcp->expects($this->never())->method('connect');
  214. $promise = $this->connector->connect('example.invalid:80');
  215. $this->loop->addTimer(0.5, function () use ($that, $promise) {
  216. $that->throwRejection($promise);
  217. });
  218. $this->setExpectedException('RuntimeException', 'Connection to tcp://example.invalid:80 failed during DNS lookup: DNS error');
  219. $this->loop->run();
  220. }
  221. public function testCancelDuringDnsCancelsDnsAndDoesNotStartTcpConnection()
  222. {
  223. $that = $this;
  224. $this->resolver->expects($this->exactly(2))->method('resolveAll')->with('example.com', $this->anything())->will($this->returnCallback(function () use ($that) {
  225. return new Promise\Promise(function () { }, $that->expectCallableExactly(1));
  226. }));
  227. $this->tcp->expects($this->never())->method('connect');
  228. $promise = $this->connector->connect('example.com:80');
  229. $this->loop->addTimer(0.05, function () use ($that, $promise) {
  230. $promise->cancel();
  231. $that->throwRejection($promise);
  232. });
  233. $this->setExpectedException(
  234. 'RuntimeException',
  235. 'Connection to tcp://example.com:80 cancelled during DNS lookup (ECONNABORTED)',
  236. \defined('SOCKET_ECONNABORTED') ? \SOCKET_ECONNABORTED : 103
  237. );
  238. $this->loop->run();
  239. }
  240. public function testCancelDuringTcpConnectionCancelsTcpConnectionIfGivenIp()
  241. {
  242. $pending = new Promise\Promise(function () { }, $this->expectCallableOnce());
  243. $this->resolver->expects($this->never())->method('resolveAll');
  244. $this->tcp->expects($this->once())->method('connect')->with($this->equalTo('1.2.3.4:80'))->willReturn($pending);
  245. $promise = $this->connector->connect('1.2.3.4:80');
  246. $this->loop->addTimer(0.1, function () use ($promise) {
  247. $promise->cancel();
  248. });
  249. $this->loop->run();
  250. }
  251. /**
  252. * @internal
  253. */
  254. public function throwRejection($promise)
  255. {
  256. $ex = null;
  257. $promise->then(null, function ($e) use (&$ex) {
  258. $ex = $e;
  259. });
  260. throw $ex;
  261. }
  262. public function provideIpvAddresses()
  263. {
  264. $ipv6 = array(
  265. array('1:2:3:4'),
  266. array('1:2:3:4', '5:6:7:8'),
  267. array('1:2:3:4', '5:6:7:8', '9:10:11:12'),
  268. );
  269. $ipv4 = array(
  270. array('1.2.3.4'),
  271. array('1.2.3.4', '5.6.7.8'),
  272. array('1.2.3.4', '5.6.7.8', '9.10.11.12'),
  273. );
  274. $ips = array();
  275. foreach ($ipv6 as $v6) {
  276. foreach ($ipv4 as $v4) {
  277. $ips[] = array(
  278. $v6,
  279. $v4
  280. );
  281. }
  282. }
  283. return $ips;
  284. }
  285. }