Skip to content

Clean up event loop left overs from promise #56

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

Merged
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ public function sendAsyncRequest(RequestInterface $request)
$request->getHeaders(),
$request->getBody()
),
$this->loop,
$request
);

Expand Down
16 changes: 3 additions & 13 deletions src/Promise.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@

use function React\Async\await;

use React\EventLoop\LoopInterface;
use React\Promise\PromiseInterface;
use RuntimeException;

/**
* React promise adapter implementation.
Expand Down Expand Up @@ -58,19 +56,11 @@ final class Promise implements HttpPromise
*/
private $promise;

/**
* ReactPHP LoopInterface.
*
* @var LoopInterface
*/
private $loop;

public function __construct(PromiseInterface $promise, LoopInterface $loop, RequestInterface $request)
public function __construct(PromiseInterface $promise, RequestInterface $request)
{
$this->state = self::PENDING;

$this->request = $request;
$this->loop = $loop;
$this->promise = $promise->then(
function (?ResponseInterface $response): ?ResponseInterface {
$this->response = $response;
Expand All @@ -86,7 +76,7 @@ function ($reason): void {

if ($reason instanceof HttplugException) {
$this->exception = $reason;
} elseif ($reason instanceof RuntimeException) {
} elseif ($reason instanceof \RuntimeException) {
$this->exception = new HttplugException\NetworkException($reason->getMessage(), $this->request, $reason);
} elseif ($reason instanceof \Throwable) {
$this->exception = new HttplugException\TransferException('Invalid exception returned from ReactPHP', 0, $reason);
Expand All @@ -101,7 +91,7 @@ function ($reason): void {

public function then(?callable $onFulfilled = null, ?callable $onRejected = null)
{
return new self($this->promise->then($onFulfilled, $onRejected), $this->loop, $this->request);
return new self($this->promise->then($onFulfilled, $onRejected), $this->request);
}

/**
Expand Down
18 changes: 4 additions & 14 deletions tests/PromiseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,17 @@

use Http\Adapter\React\Exception\UnexpectedValueException;
use Http\Adapter\React\Promise;
use Http\Adapter\React\ReactFactory;
use Http\Client\Exception\HttpException;
use Http\Client\Exception\NetworkException;
use Http\Client\Exception\TransferException;
use InvalidArgumentException;
use Nyholm\Psr7\Factory\Psr17Factory;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use React\Promise\Promise as ReactPromise;
use RuntimeException;

class PromiseTest extends TestCase
{
private $loop;

public function setUp(): void
{
$this->loop = ReactFactory::buildEventLoop();
}

public function testChain()
{
$factory = new Psr17Factory();
Expand All @@ -35,7 +25,7 @@ public function testChain()
$resolve($response);
});

$promise = new Promise($reactPromise, $this->loop, $request);
$promise = new Promise($reactPromise, $request);

$lastPromise = $promise->then(function (ResponseInterface $response) use ($factory) {
return $factory->createResponse(300, $response->getReasonPhrase());
Expand All @@ -60,7 +50,7 @@ public function testPromiseExceptionsAreTranslatedToHttplug(
$reject($reason);
});

$promise = new Promise($reactPromise, $this->loop, $request);
$promise = new Promise($reactPromise, $request);
$this->expectException($adapterExceptionClass);
$promise->wait();
}
Expand All @@ -72,8 +62,8 @@ public function exceptionThatIsThrownFromReactProvider()

return [
'string' => [$request, 'whatever', UnexpectedValueException::class],
'InvalidArgumentException' => [$request, new InvalidArgumentException('Something went wrong'), TransferException::class],
'RuntimeException' => [$request, new RuntimeException('Something happened inside ReactPHP engine'), NetworkException::class],
'InvalidArgumentException' => [$request, new \InvalidArgumentException('Something went wrong'), TransferException::class],
'RuntimeException' => [$request, new \RuntimeException('Something happened inside ReactPHP engine'), NetworkException::class],
'NetworkException' => [$request, new NetworkException('Something happened inside ReactPHP engine', $request), NetworkException::class],
'HttpException' => [$request, new HttpException('Something happened inside ReactPHP engine', $request, $response), HttpException::class],
];
Expand Down