Skip to content

Commit c8bd43b

Browse files
clueWyriHaximus
authored andcommitted
Clean up canceller function references when they are no longer needed
1 parent e0a908e commit c8bd43b

2 files changed

Lines changed: 52 additions & 2 deletions

File tree

tests/DeferredTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,41 @@ public function getPromiseTestAdapter(callable $canceller = null)
1919
'settle' => [$d, 'resolve'],
2020
]);
2121
}
22+
23+
/** @test */
24+
public function shouldRejectWithoutCreatingGarbageCyclesIfCancellerRejectsWithException()
25+
{
26+
gc_collect_cycles();
27+
$deferred = new Deferred(function ($resolve, $reject) {
28+
$reject(new \Exception('foo'));
29+
});
30+
$deferred->promise()->cancel();
31+
unset($deferred);
32+
33+
$this->assertSame(0, gc_collect_cycles());
34+
}
35+
36+
/** @test */
37+
public function shouldRejectWithoutCreatingGarbageCyclesIfParentCancellerRejectsWithException()
38+
{
39+
gc_collect_cycles();
40+
$deferred = new Deferred(function ($resolve, $reject) {
41+
$reject(new \Exception('foo'));
42+
});
43+
$deferred->promise()->then()->cancel();
44+
unset($deferred);
45+
46+
$this->assertSame(0, gc_collect_cycles());
47+
}
48+
49+
/** @test */
50+
public function shouldRejectWithoutCreatingGarbageCyclesIfCancellerHoldsReferenceAndExplicitlyRejectWithException()
51+
{
52+
gc_collect_cycles();
53+
$deferred = new Deferred(function () use (&$deferred) { });
54+
$deferred->reject(new \Exception('foo'));
55+
unset($deferred);
56+
57+
$this->assertSame(0, gc_collect_cycles());
58+
}
2259
}

tests/PromiseTest.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,6 @@ public function shouldRejectWithoutCreatingGarbageCyclesIfResolverThrowsExceptio
137137
public function shouldRejectWithoutCreatingGarbageCyclesIfCancellerWithReferenceThrowsException()
138138
{
139139
gc_collect_cycles();
140-
141140
$promise = new Promise(function () {}, function () use (&$promise) {
142141
throw new \Exception('foo');
143142
});
@@ -155,11 +154,25 @@ public function shouldRejectWithoutCreatingGarbageCyclesIfCancellerWithReference
155154
public function shouldRejectWithoutCreatingGarbageCyclesIfResolverWithReferenceThrowsException()
156155
{
157156
gc_collect_cycles();
158-
159157
$promise = new Promise(function () use (&$promise) {
160158
throw new \Exception('foo');
161159
});
160+
unset($promise);
161+
162+
$this->assertSame(0, gc_collect_cycles());
163+
}
162164

165+
/**
166+
* @test
167+
* @requires PHP 7
168+
* @see self::shouldRejectWithoutCreatingGarbageCyclesIfCancellerWithReferenceThrowsException
169+
*/
170+
public function shouldRejectWithoutCreatingGarbageCyclesIfCancellerHoldsReferenceAndResolverThrowsException()
171+
{
172+
gc_collect_cycles();
173+
$promise = new Promise(function () {
174+
throw new \Exception('foo');
175+
}, function () use (&$promise) { });
163176
unset($promise);
164177

165178
$this->assertSame(0, gc_collect_cycles());

0 commit comments

Comments
 (0)