diff --git a/tests/DeferredTest.php b/tests/DeferredTest.php index de52c89d..8ee40b8a 100644 --- a/tests/DeferredTest.php +++ b/tests/DeferredTest.php @@ -76,4 +76,37 @@ public function shouldRejectWithoutCreatingGarbageCyclesIfCancellerHoldsReferenc $this->assertSame(0, gc_collect_cycles()); } + + /** @test */ + public function shouldNotLeaveGarbageCyclesWhenRemovingLastReferenceToPendingDeferred() + { + gc_collect_cycles(); + $deferred = new Deferred(); + $deferred->promise(); + unset($deferred); + + $this->assertSame(0, gc_collect_cycles()); + } + + /** @test */ + public function shouldNotLeaveGarbageCyclesWhenRemovingLastReferenceToPendingDeferredWithUnusedCanceller() + { + gc_collect_cycles(); + $deferred = new Deferred(function () { }); + $deferred->promise(); + unset($deferred); + + $this->assertSame(0, gc_collect_cycles()); + } + + /** @test */ + public function shouldNotLeaveGarbageCyclesWhenRemovingLastReferenceToPendingDeferredWithNoopCanceller() + { + gc_collect_cycles(); + $deferred = new Deferred(function () { }); + $deferred->promise()->cancel(); + unset($deferred); + + $this->assertSame(0, gc_collect_cycles()); + } } diff --git a/tests/FulfilledPromiseTest.php b/tests/FulfilledPromiseTest.php index 97fc8f6c..f5a2da80 100644 --- a/tests/FulfilledPromiseTest.php +++ b/tests/FulfilledPromiseTest.php @@ -47,4 +47,30 @@ public function shouldThrowExceptionIfConstructedWithAPromise() return new FulfilledPromise(new FulfilledPromise()); } + + /** @test */ + public function shouldNotLeaveGarbageCyclesWhenRemovingLastReferenceToFulfilledPromiseWithAlwaysFollowers() + { + gc_collect_cycles(); + $promise = new FulfilledPromise(1); + $promise->always(function () { + throw new \RuntimeException(); + }); + unset($promise); + + $this->assertSame(0, gc_collect_cycles()); + } + + /** @test */ + public function shouldNotLeaveGarbageCyclesWhenRemovingLastReferenceToFulfilledPromiseWithThenFollowers() + { + gc_collect_cycles(); + $promise = new FulfilledPromise(1); + $promise = $promise->then(function () { + throw new \RuntimeException(); + }); + unset($promise); + + $this->assertSame(0, gc_collect_cycles()); + } } diff --git a/tests/RejectedPromiseTest.php b/tests/RejectedPromiseTest.php index c886b009..825f56cd 100644 --- a/tests/RejectedPromiseTest.php +++ b/tests/RejectedPromiseTest.php @@ -47,4 +47,30 @@ public function shouldThrowExceptionIfConstructedWithAPromise() return new RejectedPromise(new RejectedPromise()); } + + /** @test */ + public function shouldNotLeaveGarbageCyclesWhenRemovingLastReferenceToRejectedPromiseWithAlwaysFollowers() + { + gc_collect_cycles(); + $promise = new RejectedPromise(1); + $promise->always(function () { + throw new \RuntimeException(); + }); + unset($promise); + + $this->assertSame(0, gc_collect_cycles()); + } + + /** @test */ + public function shouldNotLeaveGarbageCyclesWhenRemovingLastReferenceToRejectedPromiseWithThenFollowers() + { + gc_collect_cycles(); + $promise = new RejectedPromise(1); + $promise = $promise->then(null, function () { + throw new \RuntimeException(); + }); + unset($promise); + + $this->assertSame(0, gc_collect_cycles()); + } }