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

Use default loop in testsuite and close server and connection leftovers #283

Merged
merged 3 commits into from
Jan 13, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"react/stream": "^1.2"
},
"require-dev": {
"clue/block-react": "^1.2",
"clue/block-react": "^1.5",
"phpunit/phpunit": "^9.3 || ^5.7 || ^4.8.35",
"react/promise-stream": "^1.2"
},
Expand Down
11 changes: 8 additions & 3 deletions tests/FdServerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
namespace React\Tests\Socket;

use Clue\React\Block;
use React\EventLoop\Loop;
use React\Promise\Promise;
use React\Socket\ConnectionInterface;
use React\Socket\FdServer;

class FdServerTest extends TestCase
Expand Down Expand Up @@ -302,16 +302,21 @@ public function testServerEmitsConnectionEventForNewConnection()

$client = stream_socket_client('tcp://' . stream_socket_get_name($socket, false));

$server = new FdServer($fd, Loop::get());
$server = new FdServer($fd);
$promise = new Promise(function ($resolve) use ($server) {
$server->on('connection', $resolve);
});

$connection = Block\await($promise, Loop::get(), 1.0);
$connection = Block\await($promise, null, 1.0);

/**
* @var ConnectionInterface $connection
*/
$this->assertInstanceOf('React\Socket\ConnectionInterface', $connection);

fclose($client);
$connection->close();
$server->close();
}

public function testEmitsErrorWhenAcceptListenerFails()
Expand Down
55 changes: 23 additions & 32 deletions tests/FunctionalConnectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace React\Tests\Socket;

use Clue\React\Block;
use React\EventLoop\Factory;
use React\EventLoop\Loop;
use React\Promise\Deferred;
use React\Socket\ConnectionInterface;
use React\Socket\Connector;
Expand All @@ -20,13 +20,11 @@ class FunctionalConnectorTest extends TestCase
/** @test */
public function connectionToTcpServerShouldSucceedWithLocalhost()
{
$loop = Factory::create();
$server = new TcpServer(9998);

$server = new TcpServer(9998, $loop);
$connector = new Connector(array());

$connector = new Connector(array(), $loop);

$connection = Block\await($connector->connect('localhost:9998'), $loop, self::TIMEOUT);
$connection = Block\await($connector->connect('localhost:9998'), null, self::TIMEOUT);

$server->close();

Expand All @@ -44,18 +42,16 @@ public function testConnectTwiceWithoutHappyEyeBallsOnlySendsSingleDnsQueryDueTo
$this->markTestSkipped('Not supported on Windows for PHP versions < 7.0 and legacy HHVM');
}

$loop = Factory::create();

$socket = stream_socket_server('udp://127.0.0.1:0', $errno, $errstr, STREAM_SERVER_BIND);

$connector = new Connector(array(
'dns' => 'udp://' . stream_socket_get_name($socket, false),
'happy_eyeballs' => false
), $loop);
));

// minimal DNS proxy stub which forwards DNS messages to actual DNS server
$received = 0;
$loop->addReadStream($socket, function ($socket) use (&$received) {
Loop::addReadStream($socket, function ($socket) use (&$received) {
$request = stream_socket_recvfrom($socket, 65536, 0, $peer);

$client = stream_socket_client('udp://8.8.8.8:53');
Expand All @@ -64,15 +60,18 @@ public function testConnectTwiceWithoutHappyEyeBallsOnlySendsSingleDnsQueryDueTo

stream_socket_sendto($socket, $response, 0, $peer);
++$received;
fclose($client);
});

$connection = Block\await($connector->connect('example.com:80'), $loop);
$connection = Block\await($connector->connect('example.com:80'));
$connection->close();
$this->assertEquals(1, $received);

$connection = Block\await($connector->connect('example.com:80'), $loop);
$connection = Block\await($connector->connect('example.com:80'));
$connection->close();
$this->assertEquals(1, $received);

Loop::removeReadStream($socket);
}

/**
Expand All @@ -84,11 +83,9 @@ public function connectionToRemoteTCP4n6ServerShouldResultInOurIP()
// max_nesting_level was set to 100 for PHP Versions < 5.4 which resulted in failing test for legacy PHP
ini_set('xdebug.max_nesting_level', 256);

$loop = Factory::create();

$connector = new Connector(array('happy_eyeballs' => true), $loop);
$connector = new Connector(array('happy_eyeballs' => true));

$ip = Block\await($this->request('dual.tlund.se', $connector), $loop, self::TIMEOUT);
$ip = Block\await($this->request('dual.tlund.se', $connector), null, self::TIMEOUT);

$this->assertNotFalse(inet_pton($ip));
}
Expand All @@ -99,12 +96,10 @@ public function connectionToRemoteTCP4n6ServerShouldResultInOurIP()
*/
public function connectionToRemoteTCP4ServerShouldResultInOurIP()
{
$loop = Factory::create();

$connector = new Connector(array('happy_eyeballs' => true), $loop);
$connector = new Connector(array('happy_eyeballs' => true));

try {
$ip = Block\await($this->request('ipv4.tlund.se', $connector), $loop, self::TIMEOUT);
$ip = Block\await($this->request('ipv4.tlund.se', $connector), null, self::TIMEOUT);
} catch (\Exception $e) {
$this->checkIpv4();
throw $e;
Expand All @@ -120,12 +115,10 @@ public function connectionToRemoteTCP4ServerShouldResultInOurIP()
*/
public function connectionToRemoteTCP6ServerShouldResultInOurIP()
{
$loop = Factory::create();

$connector = new Connector(array('happy_eyeballs' => true), $loop);
$connector = new Connector(array('happy_eyeballs' => true));

try {
$ip = Block\await($this->request('ipv6.tlund.se', $connector), $loop, self::TIMEOUT);
$ip = Block\await($this->request('ipv6.tlund.se', $connector), null, self::TIMEOUT);
} catch (\Exception $e) {
$this->checkIpv6();
throw $e;
Expand All @@ -141,30 +134,28 @@ public function testCancelPendingTlsConnectionDuringTlsHandshakeShouldCloseTcpCo
$this->markTestSkipped('Not supported on legacy HHVM');
}

$loop = Factory::create();

$server = new TcpServer(0, $loop);
$server = new TcpServer(0);
$uri = str_replace('tcp://', 'tls://', $server->getAddress());

$connector = new Connector(array(), $loop);
$connector = new Connector(array());
$promise = $connector->connect($uri);

$deferred = new Deferred();
$server->on('connection', function (ConnectionInterface $connection) use ($promise, $deferred, $loop) {
$server->on('connection', function (ConnectionInterface $connection) use ($promise, $deferred) {
$connection->on('close', function () use ($deferred) {
$deferred->resolve();
});

$loop->futureTick(function () use ($promise) {
Loop::futureTick(function () use ($promise) {
$promise->cancel();
});
});

Block\await($deferred->promise(), $loop, self::TIMEOUT);
Block\await($deferred->promise(), null, self::TIMEOUT);
$server->close();

try {
Block\await($promise, $loop, self::TIMEOUT);
Block\await($promise, null, self::TIMEOUT);
$this->fail();
} catch (\Exception $e) {
$this->assertInstanceOf('RuntimeException', $e);
Expand Down
Loading