8000 Forward compatibility with react/promise 3 · reactphp/socket@19f23f5 · GitHub
[go: up one dir, main page]

Skip to content

Commit 19f23f5

Browse files
committed
Forward compatibility with react/promise 3
1 parent dd3c325 commit 19f23f5

14 files changed

+268
-240
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ jobs:
99
name: PHPUnit (PHP ${{ matrix.php }} on ${{ matrix.os }})
1010
runs-on: ${{ matrix.os }}
1111
strategy:
12+
fail-fast: false
1213
matrix:
1314
os:
1415
- ubuntu-20.04

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@
3030
"evenement/evenement": "^3.0 || ^2.0 || ^1.0",
3131
"react/dns": "^1.8",
3232
"react/event-loop": "^1.2",
33-
"react/promise": "^2.6.0 || ^1.2.1",
34-
"react/promise-timer": "^1.8",
33+
"react/promise": "3.x-dev as 2.999.9999",
34+
"react/promise-timer": "1.x-dev as 1.999.999",
3535
"react/stream": "^1.2"
3636
},
3737
"require-dev": {

src/DnsConnector.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ function ($_, $reject) use (&$promise, &$resolved, $uri) {
103103
}
104104

105105
// (try to) cancel pending DNS lookup / connection attempt
106-
if ($promise instanceof CancellablePromiseInterface) {
106+
if ($promise instanceof CancellablePromiseInterface || (!\interface_exists('React\Promise\CancellablePromiseInterface') && \method_exists($promise, 'cancel'))) {
107107
// overwrite callback arguments for PHP7+ only, so they do not show
108108
// up in the Exception trace and do not cause a possible cyclic reference.
109109
$_ = $reject = null;

src/HappyEyeBallsConnectionBuilder.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,11 @@ public function connect()
8282
};
8383
};
8484

85-
$that->resolverPromises[Message::TYPE_AAAA] = $that->resolve(Message::TYPE_AAAA, $reject)->then($lookupResolve(Message::TYPE_AAAA));
85+
$lookupResolveError = function ($type) {
86+
return function ($error) { };
87+
};
88+
89+
$that->resolverPromises[Message::TYPE_AAAA] = $that->resolve(Message::TYPE_AAAA, $reject)->then($lookupResolve(Message::TYPE_AAAA), $lookupResolveError(Message::TYPE_AAAA));
8690
$that->resolverPromises[Message::TYPE_A] = $that->resolve(Message::TYPE_A, $reject)->then(function (a F438 rray $ips) use ($that, &$timer) {
8791
// happy path: IPv6 has resolved already (or could not resolve), continue with IPv4 addresses
8892
if ($that->resolved[Message::TYPE_AAAA] === true || !$ips) {
@@ -101,7 +105,7 @@ public function connect()
101105
});
102106

103107
return $deferred->promise();
104-
})->then($lookupResolve(Message::TYPE_A));
108+
})->then($lookupResolve(Message::TYPE_A), $lookupResolveError(Message::TYPE_A));
105109
}, function ($_, $reject) use ($that, &$timer) {
106110
$reject(new \RuntimeException(
107111
'Connection to ' . $that->uri . ' cancelled' . (!$that->connectionPromises ? ' during DNS lookup' : '') . ' (ECONNABORTED)',
@@ -248,13 +252,13 @@ public function cleanUp()
248252
$this->connectQueue = array();
249253

250254
foreach ($this->connectionPromises as $connectionPromise) {
251-
if ($connectionPromise instanceof CancellablePromiseInterface) {
255+
if ($connectionPromise instanceof CancellablePromiseInterface || (!\interface_exists('React\Promise\CancellablePromiseInterface') && \method_exists($connectionPromise, 'cancel'))) {
252256
$connectionPromise->cancel();
253257
}
254258
}
255259

256260
foreach ($this->resolverPromises as $resolverPromise) {
257-
if ($resolverPromise instanceof CancellablePromiseInterface) {
261+
if ($resolverPromise instanceof CancellablePromiseInterface || (!\interface_exists('React\Promise\CancellablePromiseInterface') && \method_exists($resolverPromise, 'cancel'))) {
258262
$resolverPromise->cancel();
259263
}
260264
}

src/SecureConnector.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ public function connect($uri)
4949
)->then(function (ConnectionInterface $connection) use ($context, $encryption, $uri, &$promise, &$connected) {
5050
// (unencrypted) TCP/IP connection succeeded
5151
$connected = true;
52-
5352
if (!$connection instanceof Connection) {
5453
$connection->close();
5554
throw new \UnexpectedValueException('Base connector does not use internal Connection class exposing stream resource');
@@ -61,7 +60,7 @@ public function connect($uri)
6160
}
6261

6362
// try to enable encryption
64-
return $promise = $encryption->enable($connection)->then(null, function ($error) use ($connection, $uri) {
63+
return $encryption->enable($connection)->then(null, function ($error) use ($connection, $uri) {
6564
// establishing encryption failed => close invalid connection and return error
6665
$connection->close();
6766

tests/DnsConnectorTest.php

Lines changed: 19 additions & 19 deletions
$this->tcp->expects($this->never())->method('connect');
Original file line numberDiff line numberDiff line change
@@ -26,49 +26,49 @@ public function setUpMocks()
2626
public function testPassByResolverIfGivenIp()
2727
{
2828
$this->resolver->expects($this->never())->method('resolve');
29-
$this->tcp->expects($this->once())->method('connect')->with($this->equalTo('127.0.0.1:80'))->will($this->returnValue(Promise\reject()));
29+
$this->tcp->expects($this->once())->method('connect')->with($this->equalTo('127.0.0.1:80'))->will($this->returnValue(Promise\reject(new \Exception('reject'))));
3030

31-
$this->connector->connect('127.0.0.1:80');
31+
$this->connector->connect('127.0.0.1:80')->then(null, function () {});
3232
}
3333

3434
public function testPassThroughResolverIfGivenHost()
3535
{
3636
$this->resolver->expects($this->once())->method('resolve')->with($this->equalTo('google.com'))->will($this->returnValue(Promise\resolve('1.2.3.4')));
37-
$this->tcp->expects($this->once())->method('connect')->with($this->equalTo('1.2.3.4:80?hostname=google.com'))->will($this->returnValue(Promise\reject()));
37+
$this->tcp->expects($this->once())->method('connect')->with($this->equalTo('1.2.3.4:80?hostname=google.com'))->will($this->returnValue(Promise\reject(new \Exception('reject'))));
3838

39-
$this->connector->connect('google.com:80');
39+
$this->connector->connect('google.com:80')->then(null, function () {});
4040
}
4141

4242
public function testPassThroughResolverIfGivenHostWhichResolvesToIpv6()
4343
{
4444
$this->resolver->expects($this->once())->method('resolve')->with($this->equalTo('google.com'))->will($this->returnValue(Promise\resolve('::1')));
45-
$this->tcp->expects($this->once())->method('connect')->with($this->equalTo('[::1]:80?hostname=google.com'))->will($this->returnValue(Promise\reject()));
45+
$this->tcp->expects($this->once())->method('connect')->with($this->equalTo('[::1]:80?hostname=google.com'))->will($this->returnValue(Promise\reject(new \Exception('reject'))));
4646

47-
$this->connector->connect('google.com:80');
47+
$this->connector->connect('google.com:80')->then(null, function () {});
4848
}
4949

5050
public function testPassByResolverIfGivenCompleteUri()
5151
{
5252
$this->resolver->expects($this->never())->method('resolve');
53-
$this->tcp->expects($this->once())->method('connect')->with($this->equalTo('scheme://127.0.0.1:80/path?query#fragment'))->will($this->returnValue(Promise\reject()));
53+
$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'))));
5454

55-
$this->connector->connect('scheme://127.0.0.1:80/path?query#fragment');
55+
$this->connector->connect('scheme://127.0.0.1:80/path?query#fragment')->then(null, function () {});
5656
}
5757

5858
public function testPassThroughResolverIfGivenCompleteUri()
5959
{
6060
$this->resolver->expects($this->once())->method('resolve')->with($this->equalTo('google.com'))->will($this->returnValue(Promise\resolve('1.2.3.4')));
61-
$this->tcp->expects($this->once())->method('connect')->with($this->equalTo('scheme://1.2.3.4:80/path?query&hostname=google.com#fragment'))->will($this->returnValue(Promise\reject()));
61+
$this->tcp->expects($this->once())->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'))));
6262

63-
$this->connector->connect('scheme://google.com:80/path?query#fragment');
63+
$this->connector->connect('scheme://google.com:80/path?query#fragment')->then(null, function () {});
6464
}
6565

6666
public function testPassThroughResolverIfGivenExplicitHost()
6767
{
6868
$this->resolver->expects($this->once())->method('resolve')->with($this->equalTo('google.com'))->will($this->returnValue(Promise\resolve('1.2.3.4')));
69-
$this->tcp->expects($this->once())->method('connect')->with($this->equalTo('scheme://1.2.3.4:80/?hostname=google.de'))->will($this->returnValue(Promise\reject()));
69+
$this->tcp->expects($this->once())->method('connect')->with($this->equalTo('scheme://1.2.3.4:80/?hostname=google.de'))->will($this->returnValue(Promise\reject(new \Exception('reject'))));
7070

71-
$this->connector->connect('scheme://google.com:80/?hostname=google.de');
71+
$this->connector->connect('scheme://google.com:80/?hostname=google.de')->then(null, function () {});
7272
}
7373

7474
public function testRejectsImmediatelyIfUriIsInvalid()
@@ -205,7 +205,7 @@ public function testRejectionExceptionUsesPreviousExceptionIfDnsFails()
205205

206206
public function testCancelDuringDnsCancelsDnsAndDoesNotStartTcpConnection()
207207
{
208-
$pending = new Promise\Promise(function () { }, $this->expectCallableOnce());
208+
$pending = new Promise\Promise(function () { }, function () { });
209209
$this->resolver->expects($this->once())->method('resolve')->with($this->equalTo('example.com'))->will($this->returnValue($pending));
210210
211211

@@ -237,7 +237,7 @@ public function testCancelDuringTcpConnectionCancelsTcpConnectionIfGivenIp()
237237

238238
public function testCancelDuringTcpConnectionCancelsTcpConnectionAfterDnsIsResolved()
239239
{
240-
$pending = new Promise\Promise(function () { }, $this->expectCallableOnce());
240+
$pending = new Promise\Promise(function () { }, function () { });
241241
$this->resolver->expects($this->once())->method('resolve')->with($this->equalTo('example.com'))->willReturn(Promise\resolve('1.2.3.4'));
242242
$this->tcp->expects($this->once())->method('connect')->with($this->equalTo('1.2.3.4:80?hostname=example.com'))->willReturn($pending);
243243

@@ -288,7 +288,7 @@ public function testRejectionDuringDnsLookupShouldNotCreateAnyGarbageReferences(
288288
$this->resolver->expects($this->once())->method('resolve')->with($this->equalTo('example.com'))->willReturn($dns->promise());
289289
$this->tcp->expects($this->never())->method('connect');
290290

291-
$promise = $this->connector->connect('example.com:80');
291+
$promise = $this->connector->connect('example.com:80')->then(null, function () { });
292292
$dns->reject(new \RuntimeException('DNS failed'));
293293
unset($promise, $dns);
294294

@@ -309,7 +309,7 @@ public function testRejectionAfterDnsLookupShouldNotCreateAnyGarbageReferences()
309309
$tcp = new Deferred();
310310
$this->tcp->expects($this->once())->method('connect')->with($this->equalTo('1.2.3.4:80?hostname=example.com'))->willReturn($tcp->promise());
311311

312-
$promise = $this->connector->connect('example.com:80');
312+
$promise = $this->connector->connect('example.com:80')->then(null, function () { });
313313
$dns->resolve('1.2.3.4');
314314
$tcp->reject(new \RuntimeException('Connection failed'));
315315
unset($promise, $dns, $tcp);
@@ -334,7 +334,7 @@ public function testRejectionAfterDnsLookupShouldNotCreateAnyGarbageReferencesAg
334334
});
335335
$this->tcp->expects($this->once())->method('connect')->with($this->equalTo('1.2.3.4:80?hostname=example.com'))->willReturn($tcp->promise());
336336

337-
$promise = $this->connector->connect('example.com:80');
337+
$promise = $this->connector->connect('example.com:80')->then(null, function () { });
338338
$dns->resolve('1.2.3.4');
339339

340340
unset($promise, $dns, $tcp);
@@ -356,7 +356,7 @@ public f 9F43 unction testCancelDuringDnsLookupShouldNotCreateAnyGarbageReferences()
356356
$this->resolver->expects($this->once())->method('resolve')->with($this->equalTo('example.com'))->willReturn($dns->promise());
357357
$this->tcp->expects($this->never())->method('connect');
358358

359-
$promise = $this->connector->connect('example.com:80');
359+
$promise = $this->connector->connect('example.com:80')->then(null, function () { });
360360

361361
$promise->cancel();
362362
unset($promise, $dns);
@@ -379,7 +379,7 @@ public function testCancelDuringTcpConnectionShouldNotCreateAnyGarbageReferences
379379
});
380380
$this->tcp->expects($this->once())->method('connect')->with($this->equalTo('1.2.3.4:80?hostname=example.com'))->willReturn($tcp);
381381

382-
$promise = $this->connector->connect('example.com:80');
382+
$promise = $this->connector->connect('example.com:80')->then(function () { }, function () { });
383383
$dns->resolve('1.2.3.4');
384384

385385
$promise->cancel();

0 commit comments

Comments
 (0)
0