123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734 |
- <?php
- declare(strict_types=1);
- namespace GuzzleHttp\Promise\Tests;
- use GuzzleHttp\Promise\AggregateException;
- use GuzzleHttp\Promise as P;
- use GuzzleHttp\Promise\FulfilledPromise;
- use GuzzleHttp\Promise\Promise;
- use GuzzleHttp\Promise\PromiseInterface;
- use GuzzleHttp\Promise\RejectedPromise;
- use GuzzleHttp\Promise\RejectionException;
- use GuzzleHttp\Promise\TaskQueue;
- use PHPUnit\Framework\TestCase;
- class UtilsTest extends TestCase
- {
- public function testWaitsOnAllPromisesIntoArray(): void
- {
- $e = new \Exception();
- $a = new Promise(function () use (&$a): void { $a->resolve('a'); });
- $b = new Promise(function () use (&$b): void { $b->reject('b'); });
- $c = new Promise(function () use (&$c, $e): void { $c->reject($e); });
- $results = P\Utils::inspectAll([$a, $b, $c]);
- $this->assertSame([
- ['state' => 'fulfilled', 'value' => 'a'],
- ['state' => 'rejected', 'reason' => 'b'],
- ['state' => 'rejected', 'reason' => $e],
- ], $results);
- }
- public function testUnwrapsPromisesWithNoDefaultAndFailure(): void
- {
- $this->expectException(\GuzzleHttp\Promise\RejectionException::class);
- $promises = [new FulfilledPromise('a'), new Promise()];
- P\Utils::unwrap($promises);
- }
- public function testUnwrapsPromisesWithNoDefault(): void
- {
- $promises = [new FulfilledPromise('a')];
- $this->assertSame(['a'], P\Utils::unwrap($promises));
- }
- public function testUnwrapsPromisesWithKeys(): void
- {
- $promises = [
- 'foo' => new FulfilledPromise('a'),
- 'bar' => new FulfilledPromise('b'),
- ];
- $this->assertSame([
- 'foo' => 'a',
- 'bar' => 'b',
- ], P\Utils::unwrap($promises));
- }
- public function testAllAggregatesSortedArray(): void
- {
- $a = new Promise();
- $b = new Promise();
- $c = new Promise();
- $d = P\Utils::all([$a, $b, $c]);
- $b->resolve('b');
- $a->resolve('a');
- $c->resolve('c');
- $d->then(
- function ($value) use (&$result): void { $result = $value; },
- function ($reason) use (&$result): void { $result = $reason; }
- );
- P\Utils::queue()->run();
- $this->assertSame(['a', 'b', 'c'], $result);
- }
- public function testPromisesDynamicallyAddedToStack(): void
- {
- $promises = new \ArrayIterator();
- $counter = 0;
- $promises['a'] = new FulfilledPromise('a');
- $promises['b'] = $promise = new Promise(function () use (&$promise, &$promises, &$counter): void {
- ++$counter; // Make sure the wait function is called only once
- $promise->resolve('b');
- $promises['c'] = $subPromise = new Promise(function () use (&$subPromise): void {
- $subPromise->resolve('c');
- });
- });
- $result = P\Utils::all($promises, true)->wait();
- $this->assertCount(3, $promises);
- $this->assertCount(3, $result);
- $this->assertSame($result['c'], 'c');
- $this->assertSame(1, $counter);
- }
- public function testAllThrowsWhenAnyRejected(): void
- {
- $a = new Promise();
- $b = new Promise();
- $c = new Promise();
- $d = P\Utils::all([$a, $b, $c]);
- $b->resolve('b');
- $a->reject('fail');
- $c->resolve('c');
- $d->then(
- function ($value) use (&$result): void { $result = $value; },
- function ($reason) use (&$result): void { $result = $reason; }
- );
- P\Utils::queue()->run();
- $this->assertSame('fail', $result);
- }
- public function testSomeAggregatesSortedArrayWithMax(): void
- {
- $a = new Promise();
- $b = new Promise();
- $c = new Promise();
- $d = P\Utils::some(2, [$a, $b, $c]);
- $b->resolve('b');
- $c->resolve('c');
- $a->resolve('a');
- $d->then(function ($value) use (&$result): void { $result = $value; });
- P\Utils::queue()->run();
- $this->assertSame(['b', 'c'], $result);
- }
- public function testSomeRejectsWhenTooManyRejections(): void
- {
- $a = new Promise();
- $b = new Promise();
- $d = P\Utils::some(2, [$a, $b]);
- $a->reject('bad');
- $b->resolve('good');
- P\Utils::queue()->run();
- $this->assertTrue(P\Is::rejected($d));
- $d->then(null, function ($reason) use (&$called): void {
- $called = $reason;
- });
- P\Utils::queue()->run();
- $this->assertInstanceOf(AggregateException::class, $called);
- $this->assertContains('bad', $called->getReason());
- }
- public function testCanWaitUntilSomeCountIsSatisfied(): void
- {
- $a = new Promise(function () use (&$a): void { $a->resolve('a'); });
- $b = new Promise(function () use (&$b): void { $b->resolve('b'); });
- $c = new Promise(function () use (&$c): void { $c->resolve('c'); });
- $d = P\Utils::some(2, [$a, $b, $c]);
- $this->assertSame(['a', 'b'], $d->wait());
- }
- public function testThrowsIfImpossibleToWaitForSomeCount(): void
- {
- $this->expectException(\GuzzleHttp\Promise\AggregateException::class);
- $this->expectExceptionMessage('Not enough promises to fulfill count');
- $a = new Promise(function () use (&$a): void { $a->resolve('a'); });
- $d = P\Utils::some(2, [$a]);
- $d->wait();
- }
- public function testThrowsIfResolvedWithoutCountTotalResults(): void
- {
- $this->expectException(\GuzzleHttp\Promise\AggregateException::class);
- $this->expectExceptionMessage('Not enough promises to fulfill count');
- $a = new Promise();
- $b = new Promise();
- $d = P\Utils::some(3, [$a, $b]);
- $a->resolve('a');
- $b->resolve('b');
- $d->wait();
- }
- public function testAnyReturnsFirstMatch(): void
- {
- $a = new Promise();
- $b = new Promise();
- $c = P\Utils::any([$a, $b]);
- $b->resolve('b');
- $a->resolve('a');
- $c->then(function ($value) use (&$result): void { $result = $value; });
- P\Utils::queue()->run();
- $this->assertSame('b', $result);
- }
- public function testSettleFulfillsWithFulfilledAndRejected(): void
- {
- $a = new Promise();
- $b = new Promise();
- $c = new Promise();
- $d = P\Utils::settle([$a, $b, $c]);
- $b->resolve('b');
- $c->resolve('c');
- $a->reject('a');
- P\Utils::queue()->run();
- $this->assertTrue(P\Is::fulfilled($d));
- $d->then(function ($value) use (&$result): void { $result = $value; });
- P\Utils::queue()->run();
- $this->assertSame([
- ['state' => 'rejected', 'reason' => 'a'],
- ['state' => 'fulfilled', 'value' => 'b'],
- ['state' => 'fulfilled', 'value' => 'c'],
- ], $result);
- }
- public function testCanInspectFulfilledPromise(): void
- {
- $p = new FulfilledPromise('foo');
- $this->assertSame([
- 'state' => 'fulfilled',
- 'value' => 'foo',
- ], P\Utils::inspect($p));
- }
- public function testCanInspectRejectedPromise(): void
- {
- $p = new RejectedPromise('foo');
- $this->assertSame([
- 'state' => 'rejected',
- 'reason' => 'foo',
- ], P\Utils::inspect($p));
- }
- public function testCanInspectRejectedPromiseWithNormalException(): void
- {
- $e = new \Exception('foo');
- $p = new RejectedPromise($e);
- $this->assertSame([
- 'state' => 'rejected',
- 'reason' => $e,
- ], P\Utils::inspect($p));
- }
- public function testReturnsTrampoline(): void
- {
- $this->assertInstanceOf(TaskQueue::class, P\Utils::queue());
- $this->assertSame(P\Utils::queue(), P\Utils::queue());
- }
- public function testCanScheduleThunk(): void
- {
- $tramp = P\Utils::queue();
- $promise = P\Utils::task(function () { return 'Hi!'; });
- $c = null;
- $promise->then(function ($v) use (&$c): void { $c = $v; });
- $this->assertNull($c);
- $tramp->run();
- $this->assertSame('Hi!', $c);
- }
- public function testCanScheduleThunkWithRejection(): void
- {
- $tramp = P\Utils::queue();
- $promise = P\Utils::task(function (): void { throw new \Exception('Hi!'); });
- $c = null;
- $promise->otherwise(function ($v) use (&$c): void { $c = $v; });
- $this->assertNull($c);
- $tramp->run();
- $this->assertSame('Hi!', $c->getMessage());
- }
- public function testCanScheduleThunkWithWait(): void
- {
- $tramp = P\Utils::queue();
- $promise = P\Utils::task(function () { return 'a'; });
- $this->assertSame('a', $promise->wait());
- $tramp->run();
- }
- public function testYieldsFromCoroutine(): void
- {
- if (defined('HHVM_VERSION')) {
- $this->markTestIncomplete('Broken on HHVM.');
- }
- $promise = P\Coroutine::of(function () {
- $value = (yield new FulfilledPromise('a'));
- yield $value.'b';
- });
- $promise->then(function ($value) use (&$result): void { $result = $value; });
- P\Utils::queue()->run();
- $this->assertSame('ab', $result);
- }
- public function testCanCatchExceptionsInCoroutine(): void
- {
- if (defined('HHVM_VERSION')) {
- $this->markTestIncomplete('Broken on HHVM.');
- }
- $promise = P\Coroutine::of(function () {
- try {
- yield new RejectedPromise('a');
- $this->fail('Should have thrown into the coroutine!');
- } catch (RejectionException $e) {
- $value = (yield new FulfilledPromise($e->getReason()));
- yield $value.'b';
- }
- });
- $promise->then(function ($value) use (&$result): void { $result = $value; });
- P\Utils::queue()->run();
- $this->assertTrue(P\Is::fulfilled($promise));
- $this->assertSame('ab', $result);
- }
- /**
- * @dataProvider rejectsParentExceptionProvider
- */
- public function testRejectsParentExceptionWhenException(PromiseInterface $promise): void
- {
- $promise->then(
- function (): void { $this->fail(); },
- function ($reason) use (&$result): void { $result = $reason; }
- );
- P\Utils::queue()->run();
- $this->assertInstanceOf(\Exception::class, $result);
- $this->assertSame('a', $result->getMessage());
- }
- public function rejectsParentExceptionProvider()
- {
- return [
- [P\Coroutine::of(function () {
- yield new FulfilledPromise(0);
- throw new \Exception('a');
- })],
- [P\Coroutine::of(function () {
- throw new \Exception('a');
- yield new FulfilledPromise(0);
- })],
- ];
- }
- public function testCanRejectFromRejectionCallback(): void
- {
- if (defined('HHVM_VERSION')) {
- $this->markTestIncomplete('Broken on HHVM.');
- }
- $promise = P\Coroutine::of(function () {
- yield new FulfilledPromise(0);
- yield new RejectedPromise('no!');
- });
- $promise->then(
- function (): void { $this->fail(); },
- function ($reason) use (&$result): void { $result = $reason; }
- );
- P\Utils::queue()->run();
- $this->assertInstanceOf(RejectionException::class, $result);
- $this->assertSame('no!', $result->getReason());
- }
- public function testCanAsyncReject(): void
- {
- if (defined('HHVM_VERSION')) {
- $this->markTestIncomplete('Broken on HHVM.');
- }
- $rej = new Promise();
- $promise = P\Coroutine::of(function () use ($rej) {
- yield new FulfilledPromise(0);
- yield $rej;
- });
- $promise->then(
- function (): void { $this->fail(); },
- function ($reason) use (&$result): void { $result = $reason; }
- );
- $rej->reject('no!');
- P\Utils::queue()->run();
- $this->assertInstanceOf(RejectionException::class, $result);
- $this->assertSame('no!', $result->getReason());
- }
- public function testCanCatchAndThrowOtherException(): void
- {
- $promise = P\Coroutine::of(function () {
- try {
- yield new RejectedPromise('a');
- $this->fail('Should have thrown into the coroutine!');
- } catch (RejectionException $e) {
- throw new \Exception('foo');
- }
- });
- $promise->otherwise(function ($value) use (&$result): void { $result = $value; });
- P\Utils::queue()->run();
- $this->assertTrue(P\Is::rejected($promise));
- $this->assertStringContainsString('foo', $result->getMessage());
- }
- public function testCanCatchAndYieldOtherException(): void
- {
- if (defined('HHVM_VERSION')) {
- $this->markTestIncomplete('Broken on HHVM.');
- }
- $promise = P\Coroutine::of(function () {
- try {
- yield new RejectedPromise('a');
- $this->fail('Should have thrown into the coroutine!');
- } catch (RejectionException $e) {
- yield new RejectedPromise('foo');
- }
- });
- $promise->otherwise(function ($value) use (&$result): void { $result = $value; });
- P\Utils::queue()->run();
- $this->assertTrue(P\Is::rejected($promise));
- $this->assertStringContainsString('foo', $result->getMessage());
- }
- public function createLotsOfSynchronousPromise()
- {
- return P\Coroutine::of(function () {
- $value = 0;
- for ($i = 0; $i < 1000; ++$i) {
- $value = (yield new FulfilledPromise($i));
- }
- yield $value;
- });
- }
- public function testLotsOfSynchronousDoesNotBlowStack(): void
- {
- if (defined('HHVM_VERSION')) {
- $this->markTestIncomplete('Broken on HHVM.');
- }
- $promise = $this->createLotsOfSynchronousPromise();
- $promise->then(function ($v) use (&$r): void { $r = $v; });
- P\Utils::queue()->run();
- $this->assertSame(999, $r);
- }
- public function testLotsOfSynchronousWaitDoesNotBlowStack(): void
- {
- if (defined('HHVM_VERSION')) {
- $this->markTestIncomplete('Broken on HHVM.');
- }
- $promise = $this->createLotsOfSynchronousPromise();
- $promise->then(function ($v) use (&$r): void { $r = $v; });
- $this->assertSame(999, $promise->wait());
- $this->assertSame(999, $r);
- }
- private function createLotsOfFlappingPromise()
- {
- return P\Coroutine::of(function () {
- $value = 0;
- for ($i = 0; $i < 1000; ++$i) {
- try {
- if ($i % 2) {
- $value = (yield new FulfilledPromise($i));
- } else {
- $value = (yield new RejectedPromise($i));
- }
- } catch (\Exception $e) {
- $value = (yield new FulfilledPromise($i));
- }
- }
- yield $value;
- });
- }
- public function testLotsOfTryCatchingDoesNotBlowStack(): void
- {
- if (defined('HHVM_VERSION')) {
- $this->markTestIncomplete('Broken on HHVM.');
- }
- $promise = $this->createLotsOfFlappingPromise();
- $promise->then(function ($v) use (&$r): void { $r = $v; });
- P\Utils::queue()->run();
- $this->assertSame(999, $r);
- }
- public function testLotsOfTryCatchingWaitingDoesNotBlowStack(): void
- {
- if (defined('HHVM_VERSION')) {
- $this->markTestIncomplete('Broken on HHVM.');
- }
- $promise = $this->createLotsOfFlappingPromise();
- $promise->then(function ($v) use (&$r): void { $r = $v; });
- $this->assertSame(999, $promise->wait());
- $this->assertSame(999, $r);
- }
- public function testAsyncPromisesWithCorrectlyYieldedValues(): void
- {
- if (defined('HHVM_VERSION')) {
- $this->markTestIncomplete('Broken on HHVM.');
- }
- $promises = [
- new Promise(),
- new Promise(),
- new Promise(),
- ];
- eval('
- $promise = \GuzzleHttp\Promise\Coroutine::of(function () use ($promises) {
- $value = null;
- $this->assertSame(\'skip\', (yield new \GuzzleHttp\Promise\FulfilledPromise(\'skip\')));
- foreach ($promises as $idx => $p) {
- $value = (yield $p);
- $this->assertSame($idx, $value);
- $this->assertSame(\'skip\', (yield new \GuzzleHttp\Promise\FulfilledPromise(\'skip\')));
- }
- $this->assertSame(\'skip\', (yield new \GuzzleHttp\Promise\FulfilledPromise(\'skip\')));
- yield $value;
- });
- ');
- $promises[0]->resolve(0);
- $promises[1]->resolve(1);
- $promises[2]->resolve(2);
- $promise->then(function ($v) use (&$r): void { $r = $v; });
- P\Utils::queue()->run();
- $this->assertSame(2, $r);
- }
- public function testYieldFinalWaitablePromise(): void
- {
- if (defined('HHVM_VERSION')) {
- $this->markTestIncomplete('Broken on HHVM.');
- }
- $p1 = new Promise(function () use (&$p1): void {
- $p1->resolve('skip me');
- });
- $p2 = new Promise(function () use (&$p2): void {
- $p2->resolve('hello!');
- });
- $co = P\Coroutine::of(function () use ($p1, $p2) {
- yield $p1;
- yield $p2;
- });
- P\Utils::queue()->run();
- $this->assertSame('hello!', $co->wait());
- }
- public function testCanYieldFinalPendingPromise(): void
- {
- if (defined('HHVM_VERSION')) {
- $this->markTestIncomplete('Broken on HHVM.');
- }
- $p1 = new Promise();
- $p2 = new Promise();
- $co = P\Coroutine::of(function () use ($p1, $p2) {
- yield $p1;
- yield $p2;
- });
- $p1->resolve('a');
- $p2->resolve('b');
- $co->then(function ($value) use (&$result): void { $result = $value; });
- P\Utils::queue()->run();
- $this->assertSame('b', $result);
- }
- public function testCanNestYieldsAndFailures(): void
- {
- if (defined('HHVM_VERSION')) {
- $this->markTestIncomplete('Broken on HHVM.');
- }
- $p1 = new Promise();
- $p2 = new Promise();
- $p3 = new Promise();
- $p4 = new Promise();
- $p5 = new Promise();
- $co = P\Coroutine::of(function () use ($p1, $p2, $p3, $p4, $p5) {
- try {
- yield $p1;
- } catch (\Exception $e) {
- yield $p2;
- try {
- yield $p3;
- yield $p4;
- } catch (\Exception $e) {
- yield $p5;
- }
- }
- });
- $p1->reject('a');
- $p2->resolve('b');
- $p3->resolve('c');
- $p4->reject('d');
- $p5->resolve('e');
- $co->then(function ($value) use (&$result): void { $result = $value; });
- P\Utils::queue()->run();
- $this->assertSame('e', $result);
- }
- public function testCanYieldErrorsAndSuccessesWithoutRecursion(): void
- {
- if (defined('HHVM_VERSION')) {
- $this->markTestIncomplete('Broken on HHVM.');
- }
- $promises = [];
- for ($i = 0; $i < 20; ++$i) {
- $promises[] = new Promise();
- }
- $co = P\Coroutine::of(function () use ($promises) {
- for ($i = 0; $i < 20; $i += 4) {
- try {
- yield $promises[$i];
- yield $promises[$i + 1];
- } catch (\Exception $e) {
- yield $promises[$i + 2];
- yield $promises[$i + 3];
- }
- }
- });
- for ($i = 0; $i < 20; $i += 4) {
- $promises[$i]->resolve($i);
- $promises[$i + 1]->reject($i + 1);
- $promises[$i + 2]->resolve($i + 2);
- $promises[$i + 3]->resolve($i + 3);
- }
- $co->then(function ($value) use (&$result): void { $result = $value; });
- P\Utils::queue()->run();
- $this->assertSame(19, $result);
- }
- public function testCanWaitOnPromiseAfterFulfilled(): void
- {
- if (defined('HHVM_VERSION')) {
- $this->markTestIncomplete('Broken on HHVM.');
- }
- $f = function () {
- static $i = 0;
- ++$i;
- return $p = new Promise(function () use (&$p, $i): void {
- $p->resolve($i.'-bar');
- });
- };
- $promises = [];
- for ($i = 0; $i < 20; ++$i) {
- $promises[] = $f();
- }
- $p = P\Coroutine::of(function () use ($promises) {
- yield new FulfilledPromise('foo!');
- foreach ($promises as $promise) {
- yield $promise;
- }
- });
- $this->assertSame('20-bar', $p->wait());
- }
- public function testCanWaitOnErroredPromises(): void
- {
- if (defined('HHVM_VERSION')) {
- $this->markTestIncomplete('Broken on HHVM.');
- }
- $p1 = new Promise(function () use (&$p1): void { $p1->reject('a'); });
- $p2 = new Promise(function () use (&$p2): void { $p2->resolve('b'); });
- $p3 = new Promise(function () use (&$p3): void { $p3->resolve('c'); });
- $p4 = new Promise(function () use (&$p4): void { $p4->reject('d'); });
- $p5 = new Promise(function () use (&$p5): void { $p5->resolve('e'); });
- $p6 = new Promise(function () use (&$p6): void { $p6->reject('f'); });
- $co = P\Coroutine::of(function () use ($p1, $p2, $p3, $p4, $p5, $p6) {
- try {
- yield $p1;
- } catch (\Exception $e) {
- yield $p2;
- try {
- yield $p3;
- yield $p4;
- } catch (\Exception $e) {
- yield $p5;
- yield $p6;
- }
- }
- });
- $res = P\Utils::inspect($co);
- $this->assertSame('f', $res['reason']);
- }
- public function testCoroutineOtherwiseIntegrationTest(): void
- {
- if (defined('HHVM_VERSION')) {
- $this->markTestIncomplete('Broken on HHVM.');
- }
- $a = new Promise();
- $b = new Promise();
- $promise = P\Coroutine::of(function () use ($a, $b) {
- // Execute the pool of commands concurrently, and process errors.
- yield $a;
- yield $b;
- })->otherwise(function (\Exception $e): void {
- // Throw errors from the operations as a specific Multipart error.
- throw new \OutOfBoundsException('a', 0, $e);
- });
- $a->resolve('a');
- $b->reject('b');
- $reason = P\Utils::inspect($promise)['reason'];
- $this->assertInstanceOf(\OutOfBoundsException::class, $reason);
- $this->assertInstanceOf(RejectionException::class, $reason->getPrevious());
- }
- public function testCanManuallySettleTaskQueueGeneratedPromises(): void
- {
- $p1 = P\Utils::task(function () { return 'a'; });
- $p2 = P\Utils::task(function () { return 'b'; });
- $p3 = P\Utils::task(function () { return 'c'; });
- $p1->cancel();
- $p2->resolve('b2');
- $results = P\Utils::inspectAll([$p1, $p2, $p3]);
- $this->assertSame([
- ['state' => 'rejected', 'reason' => 'Promise has been cancelled'],
- ['state' => 'fulfilled', 'value' => 'b2'],
- ['state' => 'fulfilled', 'value' => 'c'],
- ], $results);
- }
- }
|