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

Improve memory consumption for failed connection attempts #161

Merged
merged 1 commit into from
Jun 11, 2018
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
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
"react/dns": "^0.4.13",
"react/event-loop": "^1.0 || ^0.5 || ^0.4 || ^0.3.5",
"react/stream": "^1.0 || ^0.7.1",
"react/promise": "^2.1 || ^1.2",
"react/promise-timer": "~1.0"
"react/promise": "^2.6.0 || ^1.2.1",
"react/promise-timer": "^1.4.0"
},
"require-dev": {
"clue/block-react": "^1.2",
Expand Down
3 changes: 1 addition & 2 deletions src/TcpConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ private function waitForStreamOnce($stream)
$resolve(new Connection($stream, $loop));
}
});
}, function ($resolve, $reject, $progress) use ($loop, $stream) {
}, function () use ($loop, $stream) {
$loop->removeWriteStream($stream);
fclose($stream);

Expand All @@ -123,7 +123,6 @@ private function waitForStreamOnce($stream)
}
// @codeCoverageIgnoreEnd

$resolve = $reject = $progress = null;
throw new RuntimeException('Cancelled while waiting for TCP/IP connection to be established');
});
}
Expand Down
157 changes: 157 additions & 0 deletions tests/IntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,163 @@ public function testConnectingFailsIfDnsUsesInvalidResolver()
Block\await($connector->connect('google.com:80'), $loop, self::TIMEOUT);
}

public function testCancellingPendingConnectionWithoutTimeoutShouldNotCreateAnyGarbageReferences()
{
if (class_exists('React\Promise\When')) {
$this->markTestSkipped('Not supported on legacy Promise v1 API');
}

$loop = Factory::create();
$connector = new Connector($loop, array('timeout' => false));

gc_collect_cycles();
$promise = $connector->connect('8.8.8.8:80');
$promise->cancel();
unset($promise);

$this->assertEquals(0, gc_collect_cycles());
}

public function testCancellingPendingConnectionShouldNotCreateAnyGarbageReferences()
{
if (class_exists('React\Promise\When')) {
$this->markTestSkipped('Not supported on legacy Promise v1 API');
}

$loop = Factory::create();
$connector = new Connector($loop);

gc_collect_cycles();
$promise = $connector->connect('8.8.8.8:80');
$promise->cancel();
unset($promise);

$this->assertEquals(0, gc_collect_cycles());
}

public function testWaitingForRejectedConnectionShouldNotCreateAnyGarbageReferences()
{
if (class_exists('React\Promise\When')) {
$this->markTestSkipped('Not supported on legacy Promise v1 API');
}

$loop = Factory::create();
$connector = new Connector($loop, array('timeout' => false));

gc_collect_cycles();

$wait = true;
$promise = $connector->connect('127.0.0.1:1')->then(
null,
function ($e) use (&$wait) {
$wait = false;
throw $e;
}
);

// run loop for short period to ensure we detect connection refused error
Block\sleep(0.01, $loop);
if ($wait) {
Block\sleep(0.2, $loop);
if ($wait) {
$this->fail('Connection attempt did not fail');
}
}
unset($promise);

$this->assertEquals(0, gc_collect_cycles());
}

/**
* @requires PHP 7
*/
public function testWaitingForConnectionTimeoutShouldNotCreateAnyGarbageReferences()
{
if (class_exists('React\Promise\When')) {
$this->markTestSkipped('Not supported on legacy Promise v1 API');
}

$loop = Factory::create();
$connector = new Connector($loop, array('timeout' => 0.001));

gc_collect_cycles();

$wait = true;
$promise = $connector->connect('google.com:80')->then(
null,
function ($e) use (&$wait) {
$wait = false;
throw $e;
}
);

// run loop for short period to ensure we detect connection timeout error
Block\sleep(0.01, $loop);
if ($wait) {
Block\sleep(0.2, $loop);
if ($wait) {
$this->fail('Connection attempt did not fail');
}
}
unset($promise);

$this->assertEquals(0, gc_collect_cycles());
}

public function testWaitingForInvalidDnsConnectionShouldNotCreateAnyGarbageReferences()
{
if (class_exists('React\Promise\When')) {
$this->markTestSkipped('Not supported on legacy Promise v1 API');
}

$loop = Factory::create();
$connector = new Connector($loop, array('timeout' => false));

gc_collect_cycles();

$wait = true;
$promise = $connector->connect('example.invalid:80')->then(
null,
function ($e) use (&$wait) {
$wait = false;
throw $e;
}
);

// run loop for short period to ensure we detect DNS error
Block\sleep(0.01, $loop);
if ($wait) {
Block\sleep(0.2, $loop);
if ($wait) {
$this->fail('Connection attempt did not fail');
}
}
unset($promise);

$this->assertEquals(0, gc_collect_cycles());
}

public function testWaitingForSuccessfullyClosedConnectionShouldNotCreateAnyGarbageReferences()
{
if (class_exists('React\Promise\When')) {
$this->markTestSkipped('Not supported on legacy Promise v1 API');
}

$loop = Factory::create();
$connector = new Connector($loop, array('timeout' => false));

gc_collect_cycles();
$promise = $connector->connect('google.com:80')->then(
function ($conn) {
$conn->close();
}
);
Block\await($promise, $loop, self::TIMEOUT);
unset($promise);

$this->assertEquals(0, gc_collect_cycles());
}

public function testConnectingFailsIfTimeoutIsTooSmall()
{
if (!function_exists('stream_socket_enable_crypto')) {
Expand Down