diff --git a/lib/CombinedCancellationToken.php b/lib/CombinedCancellationToken.php index d3be864f..6b250d2b 100644 --- a/lib/CombinedCancellationToken.php +++ b/lib/CombinedCancellationToken.php @@ -18,15 +18,18 @@ final class CombinedCancellationToken implements CancellationToken public function __construct(CancellationToken ...$tokens) { + $thatException = &$this->exception; + $thatCallbacks = &$this->callbacks; + foreach ($tokens as $token) { - $id = $token->subscribe(function (CancelledException $exception) { - $this->exception = $exception; + $id = $token->subscribe(static function (CancelledException $exception) use (&$thatException, &$thatCallbacks) { + $thatException = $exception; - $callbacks = $this->callbacks; - $this->callbacks = []; + $callbacks = $thatCallbacks; + $thatCallbacks = []; foreach ($callbacks as $callback) { - asyncCall($callback, $this->exception); + asyncCall($callback, $thatException); } }); diff --git a/test/CombinedCancellationTokenTest.php b/test/CombinedCancellationTokenTest.php new file mode 100644 index 00000000..fb1d93a2 --- /dev/null +++ b/test/CombinedCancellationTokenTest.php @@ -0,0 +1,38 @@ +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)); + } + } + } +}