Skip to content
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
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -375,16 +375,14 @@ object to contain a list of entries.

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 responses on the client like this:

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

function getSipPeers()
function getSipPeers(): array
{
$factory = new Clue\React\Ami\Factory();

Expand All @@ -398,11 +396,13 @@ function getSipPeers()
return $ret;
});

return Block\await($promise, Loop::get(), 5.0);
return await($promise);
}
```

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.

### Message

Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
"react/socket": "^1.14"
},
"require-dev": {
"clue/block-react": "^1.5",
"phpunit/phpunit": "^9.6 || ^5.7 || ^4.8.36"
"phpunit/phpunit": "^9.6 || ^5.7 || ^4.8.36",
"react/async": "^4.2 || ^3.2 || ^2.2"
},
"autoload": {
"psr-4": {
Expand Down
43 changes: 24 additions & 19 deletions tests/FunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

namespace Clue\Tests\React\Ami;

use Clue\React\Ami\Factory;
use Clue\React\Ami\Client;
use Clue\React\Ami\ActionSender;
use Clue\React\Block;
use React\Promise\PromiseInterface;
use Clue\React\Ami\Client;
use Clue\React\Ami\Factory;
use Clue\React\Ami\Protocol\Response;
use React\EventLoop\Loop;

class FunctionalTest extends TestCase
{
Expand All @@ -19,7 +19,6 @@ class FunctionalTest extends TestCase
public static function setUpLoopBeforeClass()
{
self::$address = getenv('LOGIN');
self::$loop = \React\EventLoop\Factory::create();
}

/**
Expand All @@ -34,10 +33,16 @@ public function setUpSkipTest()

public function testConnection()
{
$factory = new Factory(self::$loop);
$factory = new Factory();

$client = \React\Async\await($factory->createClient(self::$address));
assert($client instanceof Client);

$client = $this->waitFor($factory->createClient(self::$address));
/* @var $client Client */
// let loop tick for reactphp/async v4 to clean up any remaining references
// @link https://github.com/reactphp/async/pull/65 reported upstream // TODO remove me once merged
if (function_exists('React\Async\async')) {
\React\Async\delay(0.0);
}

$this->assertFalse($client->isBusy());

Expand All @@ -52,7 +57,7 @@ public function testPing(Client $client)
{
$sender = new ActionSender($client);

$pong = $this->waitFor($sender->ping());
$pong = \React\Async\await($sender->ping());

$this->assertInstanceOf('Clue\React\Ami\Protocol\Response', $pong);
}
Expand All @@ -64,7 +69,7 @@ public function testPing(Client $client)
public function testInvalidCommandGetsRejected(Client $client)
{
$this->setExpectedException('Exception');
$this->waitFor($client->request($client->createAction('Invalid')));
\React\Async\await($client->request($client->createAction('Invalid')));
}

/**
Expand All @@ -75,15 +80,20 @@ public function testActionSenderLogoffDisconnects(Client $client)
{
$sender = new ActionSender($client);

$ret = $this->waitFor($sender->logoff());
$ret = \React\Async\await($sender->logoff());
assert($ret instanceof Response);

$this->assertInstanceOf('Clue\React\Ami\Protocol\Response', $ret);
// let loop tick for reactphp/async v4 to clean up any remaining references
// @link https://github.com/reactphp/async/pull/65 reported upstream // TODO remove me once merged
if (function_exists('React\Async\async')) {
\React\Async\delay(0.0);
}

$this->assertFalse($client->isBusy());

//$client->on('close', $this->expectCallableOnce());

self::$loop->run();
Loop::run();

return $client;
}
Expand All @@ -95,11 +105,6 @@ public function testActionSenderLogoffDisconnects(Client $client)
public function testSendRejectedAfterClose(Client $client)
{
$this->setExpectedException('Exception');
$this->waitFor($client->request($client->createAction('Ping')));
}

private function waitFor(PromiseInterface $promise)
{
return Block\await($promise, self::$loop, 5.0);
\React\Async\await($client->request($client->createAction('Ping')));
}
}