Skip to content

Commit

Permalink
Merge pull request #308 from clue-labs/garbage
Browse files Browse the repository at this point in the history
Update test suite to collect all garbage cycles
  • Loading branch information
WyriHaximus authored Jul 11, 2023
2 parents 9f5ac5f + 6017070 commit 5603979
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 23 deletions.
21 changes: 15 additions & 6 deletions tests/DnsConnectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -293,8 +293,9 @@ public function testRejectionDuringDnsLookupShouldNotCreateAnyGarbageReferences(
$this->markTestSkipped('Not supported on legacy Promise v1 API');
}

gc_collect_cycles();
gc_collect_cycles(); // clear twice to avoid leftovers in PHP 7.4 with ext-xdebug and code coverage turned on
while (gc_collect_cycles()) {
// collect all garbage cycles
}

$dns = new Deferred();
$this->resolver->expects($this->once())->method('resolve')->with($this->equalTo('example.com'))->willReturn($dns->promise());
Expand All @@ -316,7 +317,9 @@ public function testRejectionAfterDnsLookupShouldNotCreateAnyGarbageReferences()
$this->markTestSkipped('Not supported on legacy Promise v1 API');
}

gc_collect_cycles();
while (gc_collect_cycles()) {
// collect all garbage cycles
}

$dns = new Deferred();
$this->resolver->expects($this->once())->method('resolve')->with($this->equalTo('example.com'))->willReturn($dns->promise());
Expand All @@ -341,7 +344,9 @@ public function testRejectionAfterDnsLookupShouldNotCreateAnyGarbageReferencesAg
$this->markTestSkipped('Not supported on legacy Promise v1 API');
}

gc_collect_cycles();
while (gc_collect_cycles()) {
// collect all garbage cycles
}

$dns = new Deferred();
$this->resolver->expects($this->once())->method('resolve')->with($this->equalTo('example.com'))->willReturn($dns->promise());
Expand Down Expand Up @@ -369,7 +374,9 @@ public function testCancelDuringDnsLookupShouldNotCreateAnyGarbageReferences()
$this->markTestSkipped('Not supported on legacy Promise v1 API');
}

gc_collect_cycles();
while (gc_collect_cycles()) {
// collect all garbage cycles
}

$dns = new Deferred(function () {
throw new \RuntimeException();
Expand All @@ -391,7 +398,9 @@ public function testCancelDuringTcpConnectionShouldNotCreateAnyGarbageReferences
$this->markTestSkipped('Not supported on legacy Promise v1 API');
}

gc_collect_cycles();
while (gc_collect_cycles()) {
// collect all garbage cycles
}

$dns = new Deferred();
$this->resolver->expects($this->once())->method('resolve')->with($this->equalTo('example.com'))->willReturn($dns->promise());
Expand Down
35 changes: 26 additions & 9 deletions tests/IntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,9 @@ public function testCancellingPendingConnectionWithoutTimeoutShouldNotCreateAnyG

$connector = new Connector(array('timeout' => false));

gc_collect_cycles();
gc_collect_cycles(); // clear twice to avoid leftovers in PHP 7.4 with ext-xdebug and code coverage turned on
while (gc_collect_cycles()) {
// collect all garbage cycles
}

$promise = $connector->connect('8.8.8.8:80');
$promise->cancel();
Expand All @@ -144,7 +145,10 @@ public function testCancellingPendingConnectionShouldNotCreateAnyGarbageReferenc

$connector = new Connector(array());

gc_collect_cycles();
while (gc_collect_cycles()) {
// collect all garbage cycles
}

$promise = $connector->connect('8.8.8.8:80');
$promise->cancel();
unset($promise);
Expand All @@ -167,7 +171,9 @@ public function testWaitingForRejectedConnectionShouldNotCreateAnyGarbageReferen

$connector = new Connector(array('timeout' => false));

gc_collect_cycles();
while (gc_collect_cycles()) {
// collect all garbage cycles
}

$wait = true;
$promise = $connector->connect('127.0.0.1:1')->then(
Expand Down Expand Up @@ -201,7 +207,9 @@ public function testWaitingForConnectionTimeoutDuringDnsLookupShouldNotCreateAny

$connector = new Connector(array('timeout' => 0.001));

gc_collect_cycles();
while (gc_collect_cycles()) {
// collect all garbage cycles
}

$wait = true;
$promise = $connector->connect('google.com:80')->then(
Expand Down Expand Up @@ -232,7 +240,9 @@ public function testWaitingForConnectionTimeoutDuringTcpConnectionShouldNotCreat

$connector = new Connector(array('timeout' => 0.000001));

gc_collect_cycles();
while (gc_collect_cycles()) {
// collect all garbage cycles
}

$wait = true;
$promise = $connector->connect('8.8.8.8:53')->then(
Expand Down Expand Up @@ -263,7 +273,9 @@ public function testWaitingForInvalidDnsConnectionShouldNotCreateAnyGarbageRefer

$connector = new Connector(array('timeout' => false));

gc_collect_cycles();
while (gc_collect_cycles()) {
// collect all garbage cycles
}

$wait = true;
$promise = $connector->connect('example.invalid:80')->then(
Expand Down Expand Up @@ -304,7 +316,9 @@ public function testWaitingForInvalidTlsConnectionShouldNotCreateAnyGarbageRefer
)
));

gc_collect_cycles();
while (gc_collect_cycles()) {
// collect all garbage cycles
}

$wait = true;
$promise = $connector->connect('tls://self-signed.badssl.com:443')->then(
Expand Down Expand Up @@ -338,7 +352,10 @@ public function testWaitingForSuccessfullyClosedConnectionShouldNotCreateAnyGarb

$connector = new Connector(array('timeout' => false));

gc_collect_cycles();
while (gc_collect_cycles()) {
// collect all garbage cycles
}

$promise = $connector->connect('google.com:80')->then(
function ($conn) {
$conn->close();
Expand Down
11 changes: 7 additions & 4 deletions tests/SecureConnectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -258,14 +258,15 @@ public function testRejectionDuringConnectionShouldNotCreateAnyGarbageReferences
$this->markTestSkipped('Not supported on legacy Promise v1 API');
}

gc_collect_cycles();
gc_collect_cycles(); // clear twice to avoid leftovers in PHP 7.4 with ext-xdebug and code coverage turned on
while (gc_collect_cycles()) {
// collect all garbage cycles
}

$tcp = new Deferred();
$this->tcp->expects($this->once())->method('connect')->willReturn($tcp->promise());

$promise = $this->connector->connect('example.com:80');

$promise->then(null, $this->expectCallableOnce()); // avoid reporting unhandled rejection

$tcp->reject(new \RuntimeException());
Expand All @@ -280,7 +281,9 @@ public function testRejectionDuringTlsHandshakeShouldNotCreateAnyGarbageReferenc
$this->markTestSkipped('Not supported on legacy Promise v1 API');
}

gc_collect_cycles();
while (gc_collect_cycles()) {
// collect all garbage cycles
}

$connection = $this->getMockBuilder('React\Socket\Connection')->disableOriginalConstructor()->getMock();

Expand Down
5 changes: 3 additions & 2 deletions tests/TcpConnectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -372,8 +372,9 @@ public function testCancelDuringConnectionShouldNotCreateAnyGarbageReferences()
$this->markTestSkipped('Not supported on legacy Promise v1 API');
}

gc_collect_cycles();
gc_collect_cycles(); // clear twice to avoid leftovers in PHP 7.4 with ext-xdebug and code coverage turned on
while (gc_collect_cycles()) {
// collect all garbage cycles
}

$loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
$connector = new TcpConnector($loop);
Expand Down
8 changes: 6 additions & 2 deletions tests/TimeoutConnectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,9 @@ public function testRejectionDuringConnectionShouldNotCreateAnyGarbageReferences
$this->markTestSkipped('Not supported on legacy Promise v1 API');
}

gc_collect_cycles();
while (gc_collect_cycles()) {
// collect all garbage cycles
}

$connection = new Deferred();
$connector = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock();
Expand All @@ -223,7 +225,9 @@ public function testRejectionDueToTimeoutShouldNotCreateAnyGarbageReferences()
$this->markTestSkipped('Not supported on legacy Promise v1 API');
}

gc_collect_cycles();
while (gc_collect_cycles()) {
// collect all garbage cycles
}

$connection = new Deferred(function () {
throw new \RuntimeException('Connection cancelled');
Expand Down

0 comments on commit 5603979

Please sign in to comment.