Skip to content

Commit

Permalink
Use weak references to avoid the test causing a leak
Browse files Browse the repository at this point in the history
  • Loading branch information
schlessera committed Sep 11, 2023
1 parent dd91131 commit 81ba36f
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions tests/Transport/Curl/CurlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace WpOrg\Requests\Tests\Transport\Curl;

use CurlHandle;
use WeakReference;
use WpOrg\Requests\Exception;
use WpOrg\Requests\Hooks;
use WpOrg\Requests\Requests;
Expand All @@ -15,7 +16,10 @@ final class CurlTest extends BaseTestCase {
/**
* Temporary storage of the cURL handle to assert against.
*
* @var null|resource|\CurlHandle
* The handle is stored as a weak reference in order to avoid the test itself
* becoming the source of the memory leak due to locking the resource.
*
* @var null|WeakReference
*/
protected $curl_handle;

Expand All @@ -34,14 +38,20 @@ protected function getOptions($other = []) {

$this->curl_handle = null;

// On PHP < 7.2, we lack the capability to store weak references.
// In this case, we just skip the memory leak testing.
if (version_compare(PHP_VERSION, '7.2.0') < 0) {
return $options;
}

if (!array_key_exists('hooks', $options)) {
$options['hooks'] = new Hooks();
}

$options['hooks']->register(
'curl.before_request',
function ($handle) {
$this->curl_handle = $handle;
$this->curl_handle = WeakReference::create($handle);
}
);

Expand All @@ -54,19 +64,19 @@ function ($handle) {
* This is used for asserting that cURL handles are not leaking memory.
*/
protected function assert_post_conditions() {
if ($this->curl_handle === null) {
if (version_compare(PHP_VERSION, '7.2.0') < 0 || !$this->curl_handle instanceof WeakReference) {
// No cURL handle was used during this particular test scenario.
return;
}

if ($this->curl_handle instanceof CurlHandle) {
if ($this->curl_handle->get() instanceof CurlHandle) {
// CURL handles have been changed from resources into CurlHandle
// objects starting with PHP 8.0, which don;t need to be closed.
return;
}

if ($this->shouldClosedResourceAssertionBeSkipped($this->curl_handle) === false) {
$this->assertIsClosedResource($this->curl_handle);
if ($this->shouldClosedResourceAssertionBeSkipped($this->curl_handle->get()) === false) {
$this->assertIsClosedResource($this->curl_handle->get());

Check failure on line 79 in tests/Transport/Curl/CurlTest.php

View workflow job for this annotation

GitHub Actions / Test: PHP 8.1

Failed asserting that null is of type resource (closed).

Check failure on line 79 in tests/Transport/Curl/CurlTest.php

View workflow job for this annotation

GitHub Actions / Test: PHP 8.1

Failed asserting that null is of type resource (closed).

Check failure on line 79 in tests/Transport/Curl/CurlTest.php

View workflow job for this annotation

GitHub Actions / Test: PHP 8.1

Failed asserting that null is of type resource (closed).

Check failure on line 79 in tests/Transport/Curl/CurlTest.php

View workflow job for this annotation

GitHub Actions / Test: PHP 8.1

Failed asserting that null is of type resource (closed).

Check failure on line 79 in tests/Transport/Curl/CurlTest.php

View workflow job for this annotation

GitHub Actions / Test: PHP 8.1

Failed asserting that null is of type resource (closed).

Check failure on line 79 in tests/Transport/Curl/CurlTest.php

View workflow job for this annotation

GitHub Actions / Test: PHP 8.1

Failed asserting that null is of type resource (closed).

Check failure on line 79 in tests/Transport/Curl/CurlTest.php

View workflow job for this annotation

GitHub Actions / Test: PHP 8.1

Failed asserting that null is of type resource (closed).

Check failure on line 79 in tests/Transport/Curl/CurlTest.php

View workflow job for this annotation

GitHub Actions / Test: PHP 8.1

Failed asserting that null is of type resource (closed).

Check failure on line 79 in tests/Transport/Curl/CurlTest.php

View workflow job for this annotation

GitHub Actions / Test: PHP 8.1

Failed asserting that null is of type resource (closed).

Check failure on line 79 in tests/Transport/Curl/CurlTest.php

View workflow job for this annotation

GitHub Actions / Test: PHP 8.1

Failed asserting that null is of type resource (closed).
}
}

Expand Down

0 comments on commit 81ba36f

Please sign in to comment.