Skip to content

Commit

Permalink
Additional tests to avoid future garbage cycle regressions
Browse files Browse the repository at this point in the history
  • Loading branch information
clue committed Jun 11, 2018
1 parent 72087fd commit ef228a9
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 0 deletions.
33 changes: 33 additions & 0 deletions tests/DeferredTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
26 changes: 26 additions & 0 deletions tests/FulfilledPromiseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
26 changes: 26 additions & 0 deletions tests/RejectedPromiseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}

0 comments on commit ef228a9

Please sign in to comment.