forked from reactphp/socket
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHappyEyeBallsConnectorTest.php
More file actions
491 lines (388 loc) · 21.1 KB
/
HappyEyeBallsConnectorTest.php
File metadata and controls
491 lines (388 loc) · 21.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
<?php
namespace React\Tests\Socket;
use React\Dns\Model\Message;
use React\EventLoop\StreamSelectLoop;
use React\Promise;
use React\Promise\Deferred;
use React\Socket\HappyEyeBallsConnector;
use Clue\React\Block;
class HappyEyeBallsConnectorTest extends TestCase
{
private $loop;
private $tcp;
private $resolver;
private $connector;
private $connection;
public function setUp()
{
$this->loop = new TimerSpeedUpEventLoop(new StreamSelectLoop());
$this->tcp = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock();
$this->resolver = $this->getMockBuilder('React\Dns\Resolver\ResolverInterface')->disableOriginalConstructor()->getMock();
$this->connection = $this->getMockBuilder('React\Socket\ConnectionInterface')->getMock();
$this->connector = new HappyEyeBallsConnector($this->loop, $this->tcp, $this->resolver);
}
public function testHappyFlow()
{
$first = new Deferred();
$this->resolver->expects($this->exactly(2))->method('resolveAll')->with($this->equalTo('example.com'), $this->anything())->willReturn($first->promise());
$connection = $this->getMockBuilder('React\Socket\ConnectionInterface')->getMock();
$this->tcp->expects($this->exactly(1))->method('connect')->with($this->equalTo('1.2.3.4:80?hostname=example.com'))->willReturn(Promise\resolve($connection));
$promise = $this->connector->connect('example.com:80');
$first->resolve(array('1.2.3.4'));
$resolvedConnection = Block\await($promise, $this->loop);
self::assertSame($connection, $resolvedConnection);
}
public function testThatAnyOtherPendingConnectionAttemptsWillBeCanceledOnceAConnectionHasBeenEstablished()
{
$connection = $this->getMockBuilder('React\Socket\ConnectionInterface')->getMock();
$lookupAttempts = array(
Promise\reject(new \Exception('error')),
Promise\resolve(array('1.2.3.4', '5.6.7.8', '9.10.11.12')),
);
$connectionAttempts = array(
new Promise\Promise(function () {}, $this->expectCallableOnce()),
Promise\resolve($connection),
new Promise\Promise(function () {}, $this->expectCallableNever()),
);
$this->resolver->expects($this->exactly(2))->method('resolveAll')->with($this->equalTo('example.com'), $this->anything())->will($this->returnCallback(function () use (&$lookupAttempts) {
return array_shift($lookupAttempts);
}));
$this->tcp->expects($this->exactly(2))->method('connect')->with($this->isType('string'))->will($this->returnCallback(function () use (&$connectionAttempts) {
return array_shift($connectionAttempts);
}));
$promise = $this->connector->connect('example.com:80');
$resolvedConnection = Block\await($promise, $this->loop);
self::assertSame($connection, $resolvedConnection);
}
public function testPassByResolverIfGivenIp()
{
$this->resolver->expects($this->never())->method('resolveAll');
$this->tcp->expects($this->once())->method('connect')->with($this->equalTo('127.0.0.1:80'))->will($this->returnValue(Promise\resolve()));
$this->connector->connect('127.0.0.1:80');
$this->loop->run();
}
public function testPassByResolverIfGivenIpv6()
{
$this->resolver->expects($this->never())->method('resolveAll');
$this->tcp->expects($this->once())->method('connect')->with($this->equalTo('[::1]:80'))->will($this->returnValue(Promise\reject()));
$this->connector->connect('[::1]:80');
$this->loop->run();
}
public function testPassThroughResolverIfGivenHost()
{
$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'))));
$this->tcp->expects($this->exactly(2))->method('connect')->with($this->equalTo('1.2.3.4:80?hostname=google.com'))->will($this->returnValue(Promise\reject()));
$this->connector->connect('google.com:80');
$this->loop->run();
}
public function testPassThroughResolverIfGivenHostWhichResolvesToIpv6()
{
$this->resolver->expects($this->exactly(2))->method('resolveAll')->with($this->equalTo('google.com'), $this->anything())->will($this->returnValue(Promise\resolve(array('::1'))));
$this->tcp->expects($this->exactly(2))->method('connect')->with($this->equalTo('[::1]:80?hostname=google.com'))->will($this->returnValue(Promise\reject()));
$this->connector->connect('google.com:80');
$this->loop->run();
}
public function testPassByResolverIfGivenCompleteUri()
{
$this->resolver->expects($this->never())->method('resolveAll');
$this->tcp->expects($this->once())->method('connect')->with($this->equalTo('scheme://127.0.0.1:80/path?query#fragment'))->will($this->returnValue(Promise\reject()));
$this->connector->connect('scheme://127.0.0.1:80/path?query#fragment');
$this->loop->run();
}
public function testPassThroughResolverIfGivenCompleteUri()
{
$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'))));
$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()));
$this->connector->connect('scheme://google.com:80/path?query#fragment');
$this->loop->run();
}
public function testPassThroughResolverIfGivenExplicitHost()
{
$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'))));
$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()));
$this->connector->connect('scheme://google.com:80/?hostname=google.de');
$this->loop->run();
}
/**
* @dataProvider provideIpvAddresses
*/
public function testIpv4ResolvesFirstSoButIPv6IsTheFirstToConnect(array $ipv6, array $ipv4)
{
$this->resolver->expects($this->at(0))->method('resolveAll')->with('google.com', Message::TYPE_AAAA)->will($this->returnValue(Promise\Timer\resolve(0.001, $this->loop)->then(function () use ($ipv6) {
return Promise\resolve($ipv6);
})));
$this->resolver->expects($this->at(1))->method('resolveAll')->with('google.com', Message::TYPE_A)->will($this->returnValue(Promise\resolve($ipv4)));
$i = 0;
while (count($ipv6) > 0 || count($ipv4) > 0) {
if (count($ipv6) > 0) {
$this->tcp->expects($this->at($i++))->method('connect')->with($this->equalTo('scheme://[' . array_shift($ipv6) . ']:80/?hostname=google.com'))->will($this->returnValue(Promise\resolve()));
}
if (count($ipv4) > 0) {
$this->tcp->expects($this->at($i++))->method('connect')->with($this->equalTo('scheme://' . array_shift($ipv4) . ':80/?hostname=google.com'))->will($this->returnValue(Promise\resolve()));
}
}
$this->connector->connect('scheme://google.com:80/?hostname=google.com');
$this->loop->run();
}
/**
* @dataProvider provideIpvAddresses
*/
public function testIpv6ResolvesFirstSoIsTheFirstToConnect(array $ipv6, array $ipv4)
{
$deferred = new Deferred();
$this->resolver->expects($this->at(0))->method('resolveAll')->with('google.com', Message::TYPE_AAAA)->will($this->returnValue(Promise\resolve($ipv6)));
$this->resolver->expects($this->at(1))->method('resolveAll')->with('google.com', Message::TYPE_A)->will($this->returnValue($deferred->promise()));
$this->tcp->expects($this->any())->method('connect')->with($this->stringContains(']:80/?hostname=google.com'))->will($this->returnValue(Promise\reject()));
$this->connector->connect('scheme://google.com:80/?hostname=google.com');
$this->loop->addTimer(0.07, function () use ($deferred) {
$deferred->reject(new \RuntimeException());
});
$this->loop->run();
}
/**
* @dataProvider provideIpvAddresses
*/
public function testIpv6DoesntResolvesWhileIpv4DoesFirstSoIpv4Connects(array $ipv6, array $ipv4)
{
$deferred = new Deferred();
$this->resolver->expects($this->at(0))->method('resolveAll')->with('google.com', Message::TYPE_AAAA)->will($this->returnValue($deferred->promise()));
$this->resolver->expects($this->at(1))->method('resolveAll')->with('google.com', Message::TYPE_A)->will($this->returnValue(Promise\resolve($ipv4)));
$this->tcp->expects($this->any())->method('connect')->with($this->stringContains(':80/?hostname=google.com'))->will($this->returnValue(Promise\reject()));
$this->connector->connect('scheme://google.com:80/?hostname=google.com');
$this->loop->addTimer(0.07, function () use ($deferred) {
$deferred->reject(new \RuntimeException());
});
$this->loop->run();
}
/**
* @dataProvider provideIpvAddresses
*/
public function testAttemptsToConnectBothIpv6AndIpv4Addresses(array $ipv6, array $ipv4)
{
$this->resolver->expects($this->at(0))->method('resolveAll')->with('google.com', Message::TYPE_AAAA)->will($this->returnValue(Promise\resolve($ipv6)));
$this->resolver->expects($this->at(1))->method('resolveAll')->with('google.com', Message::TYPE_A)->will($this->returnValue(Promise\resolve($ipv4)));
$i = 0;
while (count($ipv6) > 0 || count($ipv4) > 0) {
if (count($ipv6) > 0) {
$this->tcp->expects($this->at($i++))->method('connect')->with($this->equalTo('scheme://[' . array_shift($ipv6) . ']:80/?hostname=google.com'))->will($this->returnValue(Promise\resolve()));
}
if (count($ipv4) > 0) {
$this->tcp->expects($this->at($i++))->method('connect')->with($this->equalTo('scheme://' . array_shift($ipv4) . ':80/?hostname=google.com'))->will($this->returnValue(Promise\resolve()));
}
}
$this->connector->connect('scheme://google.com:80/?hostname=google.com');
$this->loop->run();
}
public function testThatTheIpv4ConnectionWillWait100MilisecondsWhenIpv6AndIpv4ResolveSimultaniously()
{
$timings = array();
$this->resolver->expects($this->at(0))->method('resolveAll')->with('google.com', Message::TYPE_AAAA)->will($this->returnValue(Promise\resolve(array('1:2:3:4'))));
$this->resolver->expects($this->at(1))->method('resolveAll')->with('google.com', Message::TYPE_A)->will($this->returnValue(Promise\resolve(array('1.2.3.4'))));
$this->tcp->expects($this->at(0))->method('connect')->with($this->equalTo('scheme://[1:2:3:4]:80/?hostname=google.com'))->will($this->returnCallback(function () use (&$timings) {
$timings[Message::TYPE_AAAA] = microtime(true);
return new Promise\Promise(function () {});
}));
$this->tcp->expects($this->at(1))->method('connect')->with($this->equalTo('scheme://1.2.3.4:80/?hostname=google.com'))->will($this->returnCallback(function () use (&$timings) {
$timings[Message::TYPE_A] = microtime(true);
return new Promise\Promise(function () {});
}));
$this->connector->connect('scheme://google.com:80/?hostname=google.com');
$this->loop->run();
self::assertGreaterThan(0.01, $timings[Message::TYPE_A] - $timings[Message::TYPE_AAAA]);
}
/**
* @dataProvider provideIpvAddresses
*/
public function testAssert100MilisecondsBetweenConnectionAttempts(array $ipv6, array $ipv4)
{
$timings = array();
$this->resolver->expects($this->at(0))->method('resolveAll')->with('google.com', Message::TYPE_AAAA)->will($this->returnValue(Promise\resolve($ipv6)));
$this->resolver->expects($this->at(1))->method('resolveAll')->with('google.com', Message::TYPE_A)->will($this->returnValue(Promise\resolve($ipv4)));
$this->tcp->expects($this->any())->method('connect')->with($this->stringContains(':80/?hostname=google.com'))->will($this->returnCallback(function () use (&$timings) {
$timings[] = microtime(true);
return new Promise\Promise(function () {});
}));
$this->connector->connect('scheme://google.com:80/?hostname=google.com');
$this->loop->run();
for ($i = 0; $i < (count($timings) - 1); $i++) {
self::assertGreaterThan(0.01, $timings[$i + 1] - $timings[$i]);
}
}
/**
* @dataProvider provideIpvAddresses
*/
public function testAttemptsToConnectBothIpv6AndIpv4AddressesAlternatingIpv6AndIpv4AddressesWhenMoreThenOneIsResolvedPerFamily(array $ipv6, array $ipv4)
{
$this->resolver->expects($this->at(0))->method('resolveAll')->with('google.com', Message::TYPE_AAAA)->will($this->returnValue(
Promise\Timer\resolve(0.1, $this->loop)->then(function () use ($ipv6) {
return Promise\resolve($ipv6);
})
));
$this->resolver->expects($this->at(1))->method('resolveAll')->with('google.com', Message::TYPE_A)->will($this->returnValue(
Promise\Timer\resolve(0.1, $this->loop)->then(function () use ($ipv4) {
return Promise\resolve($ipv4);
})
));
$i = 0;
while (count($ipv6) > 0 || count($ipv4) > 0) {
if (count($ipv6) > 0) {
$this->tcp->expects($this->at($i++))->method('connect')->with($this->equalTo('scheme://[' . array_shift($ipv6) . ']:80/?hostname=google.com'))->will($this->returnValue(Promise\reject()));
}
if (count($ipv4) > 0) {
$this->tcp->expects($this->at($i++))->method('connect')->with($this->equalTo('scheme://' . array_shift($ipv4) . ':80/?hostname=google.com'))->will($this->returnValue(Promise\reject()));
}
}
$this->connector->connect('scheme://google.com:80/?hostname=google.com');
$this->loop->run();
}
public function testRejectsImmediatelyIfUriIsInvalid()
{
$this->resolver->expects($this->never())->method('resolveAll');
$this->tcp->expects($this->never())->method('connect');
$promise = $this->connector->connect('////');
$promise->then($this->expectCallableNever(), $this->expectCallableOnce());
$this->loop->run();
}
/**
* @expectedException RuntimeException
* @expectedExceptionMessage Connection failed
*/
public function testRejectsWithTcpConnectorRejectionIfGivenIp()
{
$that = $this;
$promise = Promise\reject(new \RuntimeException('Connection failed'));
$this->resolver->expects($this->never())->method('resolveAll');
$this->tcp->expects($this->once())->method('connect')->with($this->equalTo('1.2.3.4:80'))->willReturn($promise);
$promise = $this->connector->connect('1.2.3.4:80');
$this->loop->addTimer(0.5, function () use ($that, $promise) {
$promise->cancel();
$that->throwRejection($promise);
});
$this->loop->run();
}
/**
* @expectedException RuntimeException
* @expectedExceptionMessage All attempts to connect to "example.com" have failed
* @dataProvider provideIpvAddresses
*/
public function testRejectsWithTcpConnectorRejectionAfterDnsIsResolved(array $ipv6, array $ipv4)
{
$that = $this;
$promise = Promise\reject(new \RuntimeException('Connection failed'));
$this->resolver->expects($this->at(0))->method('resolveAll')->with($this->equalTo('example.com'), $this->anything())->willReturn(Promise\resolve($ipv6));
$this->resolver->expects($this->at(1))->method('resolveAll')->with($this->equalTo('example.com'), $this->anything())->willReturn(Promise\resolve($ipv4));
$this->tcp->expects($this->any())->method('connect')->with($this->stringContains(':80?hostname=example.com'))->willReturn($promise);
$promise = $this->connector->connect('example.com:80');
$this->loop->addTimer(0.1 * (count($ipv4) + count($ipv6)), function () use ($that, $promise) {
$promise->cancel();
$that->throwRejection($promise);
});
$this->loop->run();
}
/**
* @expectedException RuntimeException
* @expectedExceptionMessage Connection to example.invalid:80 failed during DNS lookup: DNS error
*/
public function testSkipConnectionIfDnsFails()
{
$that = $this;
$this->resolver->expects($this->exactly(2))->method('resolveAll')->with($this->equalTo('example.invalid'), $this->anything())->willReturn(Promise\reject(new \RuntimeException('DNS error')));
$this->tcp->expects($this->never())->method('connect');
$promise = $this->connector->connect('example.invalid:80');
$this->loop->addTimer(0.5, function () use ($that, $promise) {
$that->throwRejection($promise);
});
$this->loop->run();
}
/**
* @expectedException RuntimeException
* @expectedExceptionMessage Connection to example.com:80 cancelled during DNS lookup
*/
public function testCancelDuringDnsCancelsDnsAndDoesNotStartTcpConnection()
{
$that = $this;
$this->resolver->expects($this->exactly(2))->method('resolveAll')->with('example.com', $this->anything())->will($this->returnCallback(function () use ($that) {
return new Promise\Promise(function () { }, $that->expectCallableExactly(1));
}));
$this->tcp->expects($this->never())->method('connect');
$promise = $this->connector->connect('example.com:80');
$this->loop->addTimer(0.05, function () use ($that, $promise) {
$promise->cancel();
$that->throwRejection($promise);
});
$this->loop->run();
}
public function testCancelDuringTcpConnectionCancelsTcpConnectionIfGivenIp()
{
$pending = new Promise\Promise(function () { }, $this->expectCallableOnce());
$this->resolver->expects($this->never())->method('resolveAll');
$this->tcp->expects($this->once())->method('connect')->with($this->equalTo('1.2.3.4:80'))->willReturn($pending);
$promise = $this->connector->connect('1.2.3.4:80');
$this->loop->addTimer(0.1, function () use ($promise) {
$promise->cancel();
});
$this->loop->run();
}
/**
* @dataProvider provideIpvAddresses
*/
public function testShouldConnectOverIpv4WhenIpv6LookupFails(array $ipv6, array $ipv4)
{
$this->resolver->expects($this->at(0))->method('resolveAll')->with($this->equalTo('example.com'), Message::TYPE_AAAA)->willReturn(Promise\reject(new \Exception('failure')));
$this->resolver->expects($this->at(1))->method('resolveAll')->with($this->equalTo('example.com'), Message::TYPE_A)->willReturn(Promise\resolve($ipv4));
$this->tcp->expects($this->exactly(1))->method('connect')->with($this->equalTo('1.2.3.4:80?hostname=example.com'))->willReturn(Promise\resolve($this->connection));
$promise = $this->connector->connect('example.com:80');;
$resolvedConnection = Block\await($promise, $this->loop);
self::assertSame($this->connection, $resolvedConnection);
}
/**
* @dataProvider provideIpvAddresses
*/
public function testShouldConnectOverIpv6WhenIpv4LookupFails(array $ipv6, array $ipv4)
{
if (count($ipv6) === 0) {
$ipv6[] = '1:2:3:4';
}
$this->resolver->expects($this->at(0))->method('resolveAll')->with($this->equalTo('example.com'), Message::TYPE_AAAA)->willReturn(Promise\resolve($ipv6));
$this->resolver->expects($this->at(1))->method('resolveAll')->with($this->equalTo('example.com'), Message::TYPE_A)->willReturn(Promise\reject(new \Exception('failure')));
$this->tcp->expects($this->exactly(1))->method('connect')->with($this->equalTo('[1:2:3:4]:80?hostname=example.com'))->willReturn(Promise\resolve($this->connection));
$promise = $this->connector->connect('example.com:80');;
$resolvedConnection = Block\await($promise, $this->loop);
self::assertSame($this->connection, $resolvedConnection);
}
/**
* @internal
*/
public function throwRejection($promise)
{
$ex = null;
$promise->then(null, function ($e) use (&$ex) {
$ex = $e;
});
throw $ex;
}
public function provideIpvAddresses()
{
$ipv6 = array(
array(),
array('1:2:3:4'),
array('1:2:3:4', '5:6:7:8'),
array('1:2:3:4', '5:6:7:8', '9:10:11:12'),
);
$ipv4 = array(
array('1.2.3.4'),
array('1.2.3.4', '5.6.7.8'),
array('1.2.3.4', '5.6.7.8', '9.10.11.12'),
);
$ips = array();
foreach ($ipv6 as $v6) {
foreach ($ipv4 as $v4) {
$ips[] = array(
$v6,
$v4
);
}
}
return $ips;
}
}