Skip to content

Commit

Permalink
Merge pull request #53 from dinooo13/async
Browse files Browse the repository at this point in the history
Use reactphp/async instead of clue/reactphp-block
  • Loading branch information
clue committed Nov 22, 2022
2 parents 2a8b1e5 + 1b35c7b commit afc1f1d
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 24 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"react/promise": "^3 || ^2.1 || ^1.2"
},
"require-dev": {
"clue/block-react": "~1.0",
"react/async": "^4 || ^3 || ^2",
"phpunit/phpunit": "^9.5 || ^5.7 || ^4.8.35"
},
"autoload": {
Expand Down
27 changes: 13 additions & 14 deletions tests/FactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace React\Tests\Datagram;

use Clue\React\Block;
use React\Datagram\Factory;
use React\Datagram\Socket;
use React\Promise;
Expand Down Expand Up @@ -40,7 +39,7 @@ public function testCreateClient()

$promise = $this->factory->createClient('127.0.0.1:12345');

$capturedClient = Block\await($promise, $this->loop);
$capturedClient = \React\Async\await($promise);
$this->assertInstanceOf('React\Datagram\Socket', $capturedClient);

$this->assertEquals('127.0.0.1:12345', $capturedClient->getRemoteAddress());
Expand All @@ -59,7 +58,7 @@ public function testCreateClientLocalhost()

$promise = $this->factory->createClient('localhost:12345');

$capturedClient = Block\await($promise, $this->loop);
$capturedClient = \React\Async\await($promise);
$this->assertInstanceOf('React\Datagram\Socket', $capturedClient);

$this->assertEquals('127.0.0.1:12345', $capturedClient->getRemoteAddress());
Expand All @@ -77,7 +76,7 @@ public function testCreateClientLocalhostWithDefaultResolver()

$promise = $this->factory->createClient('localhost:12345');

$capturedClient = Block\await($promise, $this->loop);
$capturedClient = \React\Async\await($promise);
$this->assertInstanceOf('React\Datagram\SocketInterface', $capturedClient);

$capturedClient->close();
Expand All @@ -88,7 +87,7 @@ public function testCreateClientIpv6()
$promise = $this->factory->createClient('[::1]:12345');

try {
$capturedClient = Block\await($promise, $this->loop);
$capturedClient = \React\Async\await($promise);
} catch (\Exception $e) {
$this->markTestSkipped('Unable to start IPv6 client socket (IPv6 not supported on this system?)');
}
Expand All @@ -107,7 +106,7 @@ public function testCreateServer()
{
$promise = $this->factory->createServer('127.0.0.1:12345');

$capturedServer = Block\await($promise, $this->loop);
$capturedServer = \React\Async\await($promise);
$this->assertInstanceOf('React\Datagram\Socket', $capturedServer);

$this->assertEquals('127.0.0.1:12345', $capturedServer->getLocalAddress());
Expand All @@ -122,7 +121,7 @@ public function testCreateServerRandomPort()
{
$promise = $this->factory->createServer('127.0.0.1:0');

$capturedServer = Block\await($promise, $this->loop);
$capturedServer = \React\Async\await($promise);
$this->assertInstanceOf('React\Datagram\Socket', $capturedServer);

$this->assertNotEquals('127.0.0.1:0', $capturedServer->getLocalAddress());
Expand All @@ -135,15 +134,15 @@ public function testCreateClientWithIpWillNotUseResolver()
{
$this->resolver->expects($this->never())->method('resolve');

$client = Block\await($this->factory->createClient('127.0.0.1:0'), $this->loop);
$client = \React\Async\await($this->factory->createClient('127.0.0.1:0'));
$client->close();
}

public function testCreateClientWithHostnameWillUseResolver()
{
$this->resolver->expects($this->once())->method('resolve')->with('example.com')->willReturn(Promise\resolve('127.0.0.1'));

$client = Block\await($this->factory->createClient('example.com:0'), $this->loop);
$client = \React\Async\await($this->factory->createClient('example.com:0'));
$client->close();
}

Expand All @@ -152,19 +151,19 @@ public function testCreateClientWithHostnameWillRejectIfResolverRejects()
$this->resolver->expects($this->once())->method('resolve')->with('example.com')->willReturn(Promise\reject(new \RuntimeException('test')));

$this->setExpectedException('RuntimeException');
Block\await($this->factory->createClient('example.com:0'), $this->loop);
\React\Async\await($this->factory->createClient('example.com:0'));
}

public function testCreateClientWithInvalidHostnameWillReject()
{
$this->setExpectedException('Exception', 'Unable to create client socket');
Block\await($this->factory->createClient('/////'), $this->loop);
\React\Async\await($this->factory->createClient('/////'));
}

public function testCreateServerWithInvalidHostnameWillReject()
{
$this->setExpectedException('Exception', 'Unable to create server socket');
Block\await($this->factory->createServer('/////'), $this->loop);
\React\Async\await($this->factory->createServer('/////'));
}

public function testCancelCreateClientWithCancellableHostnameResolver()
Expand All @@ -176,7 +175,7 @@ public function testCancelCreateClientWithCancellableHostnameResolver()
$promise->cancel();

$this->setExpectedException('RuntimeException');
Block\await($promise, $this->loop);
\React\Async\await($promise);
}

public function testCancelCreateClientWithUncancellableHostnameResolver()
Expand All @@ -188,6 +187,6 @@ public function testCancelCreateClientWithUncancellableHostnameResolver()
$promise->cancel();

$this->setExpectedException('RuntimeException');
Block\await($promise, $this->loop);
\React\Async\await($promise);
}
}
17 changes: 8 additions & 9 deletions tests/SocketTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace React\Tests\Datagram;

use React\Datagram\Socket;
use Clue\React\Block;

class SocketTest extends TestCase
{
Expand All @@ -25,7 +24,7 @@ public function setUpFactory()
public function testCreateClientCloseWillNotBlock()
{
$promise = $this->factory->createClient('127.0.0.1:12345');
$client = Block\await($promise, $this->loop);
$client = \React\Async\await($promise);

$client->send('test');
$client->close();
Expand All @@ -52,7 +51,7 @@ public function testClientCloseAgainWillNotBlock(Socket $client)
public function testCreateClientEndWillNotBlock()
{
$promise = $this->factory->createClient('127.0.0.1:12345');
$client = Block\await($promise, $this->loop);
$client = \React\Async\await($promise);

$client->send('test');
$client->end();
Expand Down Expand Up @@ -89,7 +88,7 @@ public function testClientSendAfterEndIsNoop(Socket $client)
public function testClientSendHugeWillFailWithoutCallingCustomErrorHandler()
{
$promise = $this->factory->createClient('127.0.0.1:12345');
$client = Block\await($promise, $this->loop);
$client = \React\Async\await($promise);

$client->send(str_repeat(1, 1024 * 1024));
$client->on('error', $this->expectCallableOnce());
Expand All @@ -109,7 +108,7 @@ public function testClientSendHugeWillFailWithoutCallingCustomErrorHandler()
public function testClientSendNoServerWillFail()
{
$promise = $this->factory->createClient('127.0.0.1:1234');
$client = Block\await($promise, $this->loop);
$client = \React\Async\await($promise);

// send a message to a socket that is not actually listening
// expect the remote end to reject this by sending an ICMP message
Expand All @@ -135,10 +134,10 @@ public function testClientSendNoServerWillFail()
public function testCreatePair()
{
$promise = $this->factory->createServer('127.0.0.1:0');
$server = Block\await($promise, $this->loop);
$server = \React\Async\await($promise);

$promise = $this->factory->createClient($server->getLocalAddress());
$client = Block\await($promise, $this->loop);
$client = \React\Async\await($promise);

$that = $this;
$server->on('message', function ($message, $remote, $server) use ($that) {
Expand Down Expand Up @@ -181,7 +180,7 @@ public function testSanitizeAddress($address)
$promise = $this->factory->createServer($address);

try {
$server = Block\await($promise, $this->loop);
$server = \React\Async\await($promise);
} catch (\Exception $e) {
if (strpos($address, '[') === false) {
throw $e;
Expand All @@ -191,7 +190,7 @@ public function testSanitizeAddress($address)
}

$promise = $this->factory->createClient($server->getLocalAddress());
$client = Block\await($promise, $this->loop);
$client = \React\Async\await($promise);

$that = $this;
$server->on('message', function ($message, $remote, $server) use ($that) {
Expand Down

0 comments on commit afc1f1d

Please sign in to comment.