Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Forward compatibility with upcoming Promise v3 #460

Merged
merged 2 commits into from Sep 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 6 additions & 6 deletions composer.json
Expand Up @@ -31,16 +31,16 @@
"fig/http-message-util": "^1.1",
"psr/http-message": "^1.0",
"react/event-loop": "^1.2",
"react/promise": "^2.3 || ^1.2.1",
"react/promise-stream": "^1.1",
"react/socket": "^1.9",
"react/promise": "^3 || ^2.3 || ^1.2.1",
"react/promise-stream": "^1.4",
"react/socket": "^1.12",
"react/stream": "^1.2",
"ringcentral/psr7": "^1.2"
},
"require-dev": {
"clue/http-proxy-react": "^1.7",
"clue/reactphp-ssh-proxy": "^1.3",
"clue/socks-react": "^1.3",
"clue/http-proxy-react": "^1.8",
"clue/reactphp-ssh-proxy": "^1.4",
"clue/socks-react": "^1.4",
"phpunit/phpunit": "^9.3 || ^5.7 || ^4.8.35",
"react/async": "^4 || ^3 || ^2",
"react/promise-timer": "^1.9"
Expand Down
3 changes: 1 addition & 2 deletions src/Io/StreamingServer.php
Expand Up @@ -9,7 +9,6 @@
use React\Http\Message\Response;
use React\Http\Message\ServerRequest;
use React\Promise;
use React\Promise\CancellablePromiseInterface;
use React\Promise\PromiseInterface;
use React\Socket\ConnectionInterface;
use React\Socket\ServerInterface;
Expand Down Expand Up @@ -158,7 +157,7 @@ public function handleRequest(ConnectionInterface $conn, ServerRequestInterface
}

// cancel pending promise once connection closes
if ($response instanceof CancellablePromiseInterface) {
if ($response instanceof PromiseInterface && \method_exists($response, 'cancel')) {
$conn->on('close', function () use ($response) {
$response->cancel();
});
Expand Down
2 changes: 1 addition & 1 deletion src/Middleware/LimitConcurrentRequestsMiddleware.php
Expand Up @@ -206,6 +206,6 @@ public function processQueue()
$first = \reset($this->queue);
unset($this->queue[key($this->queue)]);

$first->resolve();
$first->resolve(null);
}
}
4 changes: 4 additions & 0 deletions tests/FunctionalHttpServerTest.php
Expand Up @@ -726,6 +726,10 @@ public function testConnectWithClosedThroughStreamReturnsNoData()

public function testLimitConcurrentRequestsMiddlewareRequestStreamPausing()
{
if (defined('HHVM_VERSION') && !interface_exists('React\Promise\PromisorInterface')) {
$this->markTestSkipped('Not supported on legacy HHVM with Promise v3');
}

$connector = new Connector();

$http = new HttpServer(
Expand Down
3 changes: 1 addition & 2 deletions tests/Io/MiddlewareRunnerTest.php
Expand Up @@ -8,7 +8,6 @@
use React\Http\Io\MiddlewareRunner;
use React\Http\Message\ServerRequest;
use React\Promise;
use React\Promise\CancellablePromiseInterface;
use React\Promise\PromiseInterface;
use React\Tests\Http\Middleware\ProcessStack;
use React\Tests\Http\TestCase;
Expand Down Expand Up @@ -479,7 +478,7 @@ function (RequestInterface $request) use ($once) {

$promise = $middleware($request);

$this->assertTrue($promise instanceof CancellablePromiseInterface);
$this->assertTrue($promise instanceof PromiseInterface && \method_exists($promise, 'cancel'));
$promise->cancel();
}
}
14 changes: 10 additions & 4 deletions tests/Io/TransactionTest.php
Expand Up @@ -436,11 +436,14 @@ public function testCancelBufferingResponseWillCloseStreamAndReject()
$response = new Response(200, array(), new ReadableBodyStream($stream));

// mock sender to resolve promise with the given $response in response to the given $request
$deferred = new Deferred();
$sender = $this->makeSenderMock();
$sender->expects($this->once())->method('send')->with($this->equalTo($request))->willReturn(Promise\resolve($response));
$sender->expects($this->once())->method('send')->with($this->equalTo($request))->willReturn($deferred->promise());

$transaction = new Transaction($sender, Loop::get());
$promise = $transaction->send($request);

$deferred->resolve($response);
$promise->cancel();

$this->setExpectedException('RuntimeException');
Expand Down Expand Up @@ -778,13 +781,16 @@ public function testCancelTransactionWillCloseBufferingStream()
$body = new ThroughStream();
$body->on('close', $this->expectCallableOnce());

// mock sender to resolve promise with the given $redirectResponse in
$redirectResponse = new Response(301, array('Location' => 'http://example.com/new'), new ReadableBodyStream($body));
$sender->expects($this->once())->method('send')->willReturn(Promise\resolve($redirectResponse));
// mock sender to resolve promise with the given $redirectResponse
$deferred = new Deferred();
$sender->expects($this->once())->method('send')->willReturn($deferred->promise());

$transaction = new Transaction($sender, $loop);
$promise = $transaction->send($request);

$redirectResponse = new Response(301, array('Location' => 'http://example.com/new'), new ReadableBodyStream($body));
$deferred->resolve($redirectResponse);

$promise->cancel();
}

Expand Down
8 changes: 4 additions & 4 deletions tests/Middleware/LimitConcurrentRequestsMiddlewareTest.php
Expand Up @@ -79,7 +79,7 @@ public function testLimitOneRequestConcurrently()
/**
* Ensure resolve frees up a slot
*/
$deferredA->resolve();
$deferredA->resolve(null);

$this->assertTrue($calledA);
$this->assertTrue($calledB);
Expand All @@ -88,7 +88,7 @@ public function testLimitOneRequestConcurrently()
/**
* Ensure reject also frees up a slot
*/
$deferredB->reject();
$deferredB->reject(new \RuntimeException());

$this->assertTrue($calledA);
$this->assertTrue($calledB);
Expand Down Expand Up @@ -194,7 +194,7 @@ public function testStreamDoesPauseAndThenResumeWhenDequeued()

$limitHandlers(new ServerRequest('GET', 'https://example.com/', array(), $body), function () {});

$deferred->reject();
$deferred->reject(new \RuntimeException());
}

public function testReceivesBufferedRequestSameInstance()
Expand Down Expand Up @@ -452,7 +452,7 @@ public function testReceivesStreamingBodyChangesInstanceWithCustomBodyButSameDat
$req = $request;
});

$deferred->reject();
$deferred->reject(new \RuntimeException());

$this->assertNotSame($request, $req);
$this->assertInstanceOf('Psr\Http\Message\ServerRequestInterface', $req);
Expand Down