-
-
Notifications
You must be signed in to change notification settings - Fork 257
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed memory leak in CombinedCancellationToken (#384)
- Loading branch information
Showing
2 changed files
with
46 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Amp\Test; | ||
|
||
use Amp\CancellationTokenSource; | ||
use Amp\CombinedCancellationToken; | ||
|
||
class CombinedCancellationTokenTest extends BaseTest | ||
{ | ||
private const LOOP_COUNT = 20; | ||
private const TOKENS_COUNT = 1000; | ||
|
||
public function testBenchmark(): void | ||
{ | ||
$firstMemoryMeasure = 0; | ||
|
||
for ($i = 0; $i < self::LOOP_COUNT; $i++) { | ||
$tokens = []; | ||
for ($j = 0; $j < self::TOKENS_COUNT; $j++) { | ||
$tokens[] = (new CancellationTokenSource)->getToken(); | ||
} | ||
$combinedToken = new CombinedCancellationToken(...$tokens); | ||
|
||
if (!$firstMemoryMeasure && $i > self::LOOP_COUNT / 2) { | ||
// Warmup and store first memory usage after 50% of iterations | ||
$firstMemoryMeasure = \memory_get_usage(true); | ||
} | ||
// Remove tokens from memory | ||
unset($combinedToken); | ||
|
||
// Asserts | ||
if ($firstMemoryMeasure > 0) { | ||
self::assertEquals($firstMemoryMeasure, \memory_get_usage(true)); | ||
} | ||
} | ||
} | ||
} |