HappyEyeBallsConnectorTest.php 14 KB

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