Skip to content

Commit

Permalink
Merge pull request #464 from clue-labs/async
Browse files Browse the repository at this point in the history
Update test suite to use new reactphp/async package instead of clue/reactphp-block
  • Loading branch information
WyriHaximus committed Aug 10, 2022
2 parents b3ff9c8 + 9946ba7 commit b5a66a4
Show file tree
Hide file tree
Showing 11 changed files with 165 additions and 129 deletions.
20 changes: 12 additions & 8 deletions README.md
Expand Up @@ -373,20 +373,19 @@ See also [`withFollowRedirects()`](#withfollowredirects) for more details.

As stated above, this library provides you a powerful, async API by default.

If, however, you want to integrate this into your traditional, blocking environment,
you should look into also using [clue/reactphp-block](https://github.com/clue/reactphp-block).

The resulting blocking code could look something like this:
You can also integrate this into your traditional, blocking environment by using
[reactphp/async](https://github.com/reactphp/async). This allows you to simply
await async HTTP requests like this:

```php
use Clue\React\Block;
use function React\Async\await;

$browser = new React\Http\Browser();

$promise = $browser->get('http://example.com/');

try {
$response = Block\await($promise, Loop::get());
$response = await($promise);
// response successfully received
} catch (Exception $e) {
// an error occurred while performing the request
Expand All @@ -396,15 +395,20 @@ try {
Similarly, you can also process multiple requests concurrently and await an array of `Response` objects:

```php
use function React\Async\await;
use function React\Promise\all;

$promises = array(
$browser->get('http://example.com/'),
$browser->get('http://www.example.org/'),
);

$responses = Block\awaitAll($promises, Loop::get());
$responses = await(all($promises));
```

Please refer to [clue/reactphp-block](https://github.com/clue/reactphp-block#readme) for more details.
This is made possible thanks to fibers available in PHP 8.1+ and our
compatibility API that also works on all supported PHP versions.
Please refer to [reactphp/async](https://github.com/reactphp/async#readme) for more details.

Keep in mind the above remark about buffering the whole response message in memory.
As an alternative, you may also see one of the following chapters for the
Expand Down
5 changes: 3 additions & 2 deletions composer.json
Expand Up @@ -38,11 +38,12 @@
"ringcentral/psr7": "^1.2"
},
"require-dev": {
"clue/block-react": "^1.5",
"clue/http-proxy-react": "^1.7",
"clue/reactphp-ssh-proxy": "^1.3",
"clue/socks-react": "^1.3",
"phpunit/phpunit": "^9.3 || ^5.7 || ^4.8.35"
"phpunit/phpunit": "^9.3 || ^5.7 || ^4.8.35",
"react/async": "^4 || ^3 || ^2",
"react/promise-timer": "^1.9"
},
"autoload": {
"psr-4": { "React\\Http\\": "src" }
Expand Down
1 change: 0 additions & 1 deletion tests/BrowserTest.php
Expand Up @@ -2,7 +2,6 @@

namespace React\Tests\Http;

use Clue\React\Block;
use Psr\Http\Message\RequestInterface;
use React\Http\Browser;
use React\Promise\Promise;
Expand Down
11 changes: 5 additions & 6 deletions tests/Client/FunctionalIntegrationTest.php
Expand Up @@ -2,7 +2,6 @@

namespace React\Tests\Http\Client;

use Clue\React\Block;
use Psr\Http\Message\ResponseInterface;
use React\EventLoop\Loop;
use React\Http\Client\Client;
Expand Down Expand Up @@ -51,7 +50,7 @@ public function testRequestToLocalhostEmitsSingleRemoteConnection()
$promise = Stream\first($request, 'close');
$request->end();

Block\await($promise, null, self::TIMEOUT_LOCAL);
\React\Async\await(\React\Promise\Timer\timeout($promise, self::TIMEOUT_LOCAL));
}

public function testRequestLegacyHttpServerWithOnlyLineFeedReturnsSuccessfulResponse()
Expand All @@ -73,7 +72,7 @@ public function testRequestLegacyHttpServerWithOnlyLineFeedReturnsSuccessfulResp
$promise = Stream\first($request, 'close');
$request->end();

Block\await($promise, null, self::TIMEOUT_LOCAL);
\React\Async\await(\React\Promise\Timer\timeout($promise, self::TIMEOUT_LOCAL));
}

/** @group internet */
Expand All @@ -94,7 +93,7 @@ public function testSuccessfulResponseEmitsEnd()
$promise = Stream\first($request, 'close');
$request->end();

Block\await($promise, null, self::TIMEOUT_REMOTE);
\React\Async\await(\React\Promise\Timer\timeout($promise, self::TIMEOUT_REMOTE));
}

/** @group internet */
Expand Down Expand Up @@ -122,7 +121,7 @@ public function testPostDataReturnsData()

$request->end($data);

$buffer = Block\await($deferred->promise(), null, self::TIMEOUT_REMOTE);
$buffer = \React\Async\await(\React\Promise\Timer\timeout($deferred->promise(), self::TIMEOUT_REMOTE));

$this->assertNotEquals('', $buffer);

Expand Down Expand Up @@ -154,7 +153,7 @@ public function testPostJsonReturnsData()

$request->end($data);

$buffer = Block\await($deferred->promise(), null, self::TIMEOUT_REMOTE);
$buffer = \React\Async\await(\React\Promise\Timer\timeout($deferred->promise(), self::TIMEOUT_REMOTE));

$this->assertNotEquals('', $buffer);

Expand Down

0 comments on commit b5a66a4

Please sign in to comment.