2016-12-29 21:09:49 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Amp\Test;
|
|
|
|
|
2017-03-10 21:58:46 +01:00
|
|
|
class Promise implements \Amp\Promise {
|
2016-12-29 21:09:49 +01:00
|
|
|
use \Amp\Internal\Placeholder {
|
|
|
|
resolve as public;
|
|
|
|
fail as public;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-10 21:58:46 +01:00
|
|
|
class PromiseTest extends \Amp\Promise\Test {
|
2016-12-30 20:01:09 +01:00
|
|
|
public function promise() {
|
2016-12-29 21:09:49 +01:00
|
|
|
$promise = new Promise;
|
|
|
|
return [
|
|
|
|
$promise,
|
|
|
|
[$promise, 'resolve'],
|
|
|
|
[$promise, 'fail'],
|
|
|
|
];
|
|
|
|
}
|
2017-01-07 14:24:21 +01:00
|
|
|
|
|
|
|
public function testWhenQueueUnrolling() {
|
|
|
|
$count = 50;
|
|
|
|
$invoked = false;
|
|
|
|
|
|
|
|
$promise = new Promise;
|
|
|
|
$promise->when(function () { });
|
|
|
|
$promise->when(function () { });
|
|
|
|
$promise->when(function () use (&$invoked) {
|
|
|
|
$invoked = true;
|
|
|
|
$this->assertLessThan(30, count(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS)));
|
|
|
|
});
|
|
|
|
|
|
|
|
$last = $promise;
|
|
|
|
|
|
|
|
$f = function () use (&$f, &$count, &$last) {
|
|
|
|
$p = new Promise;
|
|
|
|
$p->when(function () { });
|
|
|
|
$p->when(function () { });
|
|
|
|
|
|
|
|
$last->resolve($p);
|
|
|
|
$last = $p;
|
|
|
|
|
|
|
|
if (--$count > 0) {
|
|
|
|
$f();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
$f();
|
|
|
|
$last->resolve();
|
|
|
|
|
|
|
|
$this->assertTrue($invoked);
|
|
|
|
}
|
2016-12-29 21:09:49 +01:00
|
|
|
}
|