Skip to content

Commit 5c580d3

Browse files
committed
Forward compatibility with react/promise 3
1 parent 28fac70 commit 5c580d3

11 files changed

+104
-157
lines changed

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
"evenement/evenement": "^3.0 || ^2.0 || ^1.0",
3131
"react/dns": "^1.1",
3232
"react/event-loop": "^1.0 || ^0.5",
33-
"react/promise": "^2.6.0 || ^1.2.1",
33+
"react/promise": "^2.6 || ^1.2.1",
3434
"react/promise-timer": "^1.4.0",
3535
"react/stream": "^1.1"
3636
},

src/DnsConnector.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ function ($_, $reject) use (&$promise, &$resolved, $uri) {
101101
}
102102

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

src/HappyEyeBallsConnectionBuilder.php

+8-4
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 (array $ips) use ($that, &$timer) {
8791
// happy path: IPv6 has resolved already, continue with IPv4 addresses
8892
if ($that->resolved[Message::TYPE_AAAA] === true) {
@@ -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('Connection to ' . $that->uri . ' cancelled' . (!$that->connectionPromises ? ' during DNS lookup' : '')));
107111
$_ = $reject = null;
@@ -274,13 +278,13 @@ public function cleanUp()
274278
$this->connectQueue = array();
275279

276280
foreach ($this->connectionPromises as $connectionPromise) {
277-
if ($connectionPromise instanceof CancellablePromiseInterface) {
281+
if ($connectionPromise instanceof CancellablePromiseInterface || (!\interface_exists('React\Promise\CancellablePromiseInterface') && \method_exists($connectionPromise, 'cancel'))) {
278282
$connectionPromise->cancel();
279283
}
280284
}
281285

282286
foreach ($this->resolverPromises as $resolverPromise) {
283-
if ($resolverPromise instanceof CancellablePromiseInterface) {
287+
if ($resolverPromise instanceof CancellablePromiseInterface || (!\interface_exists('React\Promise\CancellablePromiseInterface') && \method_exists($resolverPromise, 'cancel'))) {
284288
$resolverPromise->cancel();
285289
}
286290
}

src/SecureConnector.php

+2-20
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,7 @@ public function connect($uri)
4040
$context = $this->context;
4141

4242
$encryption = $this->streamEncryption;
43-
$connected = false;
44-
$promise = $this->connector->connect($uri)->then(function (ConnectionInterface $connection) use ($context, $encryption, $uri, &$promise, &$connected) {
45-
// (unencrypted) TCP/IP connection succeeded
46-
$connected = true;
47-
43+
return $this->connector->connect($uri)->then(function (ConnectionInterface $connection) use ($context, $encryption, $uri) {
4844
if (!$connection instanceof Connection) {
4945
$connection->close();
5046
throw new \UnexpectedValueException('Base connector does not use internal Connection class exposing stream resource');
@@ -56,7 +52,7 @@ public function connect($uri)
5652
}
5753

5854
// try to enable encryption
59-
return $promise = $encryption->enable($connection)->then(null, function ($error) use ($connection, $uri) {
55+
return $encryption->enable($connection)->then(null, function ($error) use ($connection, $uri) {
6056
// establishing encryption failed => close invalid connection and return error
6157
$connection->close();
6258

@@ -66,19 +62,5 @@ public function connect($uri)
6662
);
6763
});
6864
});
69-
70-
return new \React\Promise\Promise(
71-
function ($resolve, $reject) use ($promise) {
72-
$promise->then($resolve, $reject);
73-
},
74-
function ($_, $reject) use (&$promise, $uri, &$connected) {
75-
if ($connected) {
76-
$reject(new \RuntimeException('Connection to ' . $uri . ' cancelled during TLS handshake'));
77-
}
78-
79-
$promise->cancel();
80-
$promise = null;
81-
}
82-
);
8365
}
8466
}

tests/DnsConnectorTest.php

+19-19
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()
@@ -134,7 +134,7 @@ public function testRejectionExceptionUsesPreviousExceptionIfDnsFails()
134134

135135
public function testCancelDuringDnsCancelsDnsAndDoesNotStartTcpConnection()
136136
{
137-
$pending = new Promise\Promise(function () { }, $this->expectCallableOnce());
137+
$pending = new Promise\Promise(function () { }, function () { });
138138
$this->resolver->expects($this->once())->method('resolve')->with($this->equalTo('example.com'))->will($this->returnValue($pending));
139139
$this->tcp->expects($this->never())->method('connect');
140140

@@ -157,7 +157,7 @@ public function testCancelDuringTcpConnectionCancelsTcpConnectionIfGivenIp()
157157

158158
public function testCancelDuringTcpConnectionCancelsTcpConnectionAfterDnsIsResolved()
159159
{
160-
$pending = new Promise\Promise(function () { }, $this->expectCallableOnce());
160+
$pending = new Promise\Promise(function () { }, function () { });
161161
$this->resolver->expects($this->once())->method('resolve')->with($this->equalTo('example.com'))->willReturn(Promise\resolve('1.2.3.4'));
162162
$this->tcp->expects($this->once())->method('connect')->with($this->equalTo('1.2.3.4:80?hostname=example.com'))->willReturn($pending);
163163

@@ -196,7 +196,7 @@ public function testRejectionDuringDnsLookupShouldNotCreateAnyGarbageReferences(
196196
$this->resolver->expects($this->once())->method('resolve')->with($this->equalTo('example.com'))->willReturn($dns->promise());
197197
$this->tcp->expects($this->never())->method('connect');
198198

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

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

220-
$promise = $this->connector->connect('example.com:80');
220+
$promise = $this->connector->connect('example.com:80')->then(null, function () { });
221221
$dns->resolve('1.2.3.4');
222222
$tcp->reject(new \RuntimeException('Connection failed'));
223223
unset($promise, $dns, $tcp);
@@ -242,7 +242,7 @@ public function testRejectionAfterDnsLookupShouldNotCreateAnyGarbageReferencesAg
242242
});
243243
$this->tcp->expects($this->once())->method('connect')->with($this->equalTo('1.2.3.4:80?hostname=example.com'))->willReturn($tcp->promise());
244244

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

248248
unset($promise, $dns, $tcp);
@@ -264,7 +264,7 @@ public function testCancelDuringDnsLookupShouldNotCreateAnyGarbageReferences()
264264
$this->resolver->expects($this->once())->method('resolve')->with($this->equalTo('example.com'))->willReturn($dns->promise());
265265
$this->tcp->expects($this->never())->method('connect');
266266

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

269269
$promise->cancel();
270270
unset($promise, $dns);
@@ -287,7 +287,7 @@ public function testCancelDuringTcpConnectionShouldNotCreateAnyGarbageReferences
287287
});
288288
$this->tcp->expects($this->once())->method('connect')->with($this->equalTo('1.2.3.4:80?hostname=example.com'))->willReturn($tcp);
289289

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

293293
$promise->cancel();

0 commit comments

Comments
 (0)