| 1 | <?php
|
|---|
| 2 | namespace GuzzleHttp\Promise\Tests;
|
|---|
| 3 |
|
|---|
| 4 | use GuzzleHttp\Promise\RejectedPromise;
|
|---|
| 5 | use GuzzleHttp\Promise\FulfilledPromise;
|
|---|
| 6 | use GuzzleHttp\Promise\Promise;
|
|---|
| 7 | use GuzzleHttp\Promise\PromiseInterface;
|
|---|
| 8 | use GuzzleHttp\Promise\EachPromise;
|
|---|
| 9 | use GuzzleHttp\Promise as P;
|
|---|
| 10 |
|
|---|
| 11 | /**
|
|---|
| 12 | * @covers GuzzleHttp\Promise\EachPromise
|
|---|
| 13 | */
|
|---|
| 14 | class EachPromiseTest extends \PHPUnit_Framework_TestCase
|
|---|
| 15 | {
|
|---|
| 16 | public function testReturnsSameInstance()
|
|---|
| 17 | {
|
|---|
| 18 | $each = new EachPromise([], ['concurrency' => 100]);
|
|---|
| 19 | $this->assertSame($each->promise(), $each->promise());
|
|---|
| 20 | }
|
|---|
| 21 |
|
|---|
| 22 | public function testInvokesAllPromises()
|
|---|
| 23 | {
|
|---|
| 24 | $promises = [new Promise(), new Promise(), new Promise()];
|
|---|
| 25 | $called = [];
|
|---|
| 26 | $each = new EachPromise($promises, [
|
|---|
| 27 | 'fulfilled' => function ($value) use (&$called) {
|
|---|
| 28 | $called[] = $value;
|
|---|
| 29 | }
|
|---|
| 30 | ]);
|
|---|
| 31 | $p = $each->promise();
|
|---|
| 32 | $promises[0]->resolve('a');
|
|---|
| 33 | $promises[1]->resolve('c');
|
|---|
| 34 | $promises[2]->resolve('b');
|
|---|
| 35 | P\queue()->run();
|
|---|
| 36 | $this->assertEquals(['a', 'c', 'b'], $called);
|
|---|
| 37 | $this->assertEquals(PromiseInterface::FULFILLED, $p->getState());
|
|---|
| 38 | }
|
|---|
| 39 |
|
|---|
| 40 | public function testIsWaitable()
|
|---|
| 41 | {
|
|---|
| 42 | $a = new Promise(function () use (&$a) { $a->resolve('a'); });
|
|---|
| 43 | $b = new Promise(function () use (&$b) { $b->resolve('b'); });
|
|---|
| 44 | $called = [];
|
|---|
| 45 | $each = new EachPromise([$a, $b], [
|
|---|
| 46 | 'fulfilled' => function ($value) use (&$called) { $called[] = $value; }
|
|---|
| 47 | ]);
|
|---|
| 48 | $p = $each->promise();
|
|---|
| 49 | $this->assertNull($p->wait());
|
|---|
| 50 | $this->assertEquals(PromiseInterface::FULFILLED, $p->getState());
|
|---|
| 51 | $this->assertEquals(['a', 'b'], $called);
|
|---|
| 52 | }
|
|---|
| 53 |
|
|---|
| 54 | public function testCanResolveBeforeConsumingAll()
|
|---|
| 55 | {
|
|---|
| 56 | $called = 0;
|
|---|
| 57 | $a = new Promise(function () use (&$a) { $a->resolve('a'); });
|
|---|
| 58 | $b = new Promise(function () { $this->fail(); });
|
|---|
| 59 | $each = new EachPromise([$a, $b], [
|
|---|
| 60 | 'fulfilled' => function ($value, $idx, Promise $aggregate) use (&$called) {
|
|---|
| 61 | $this->assertSame($idx, 0);
|
|---|
| 62 | $this->assertEquals('a', $value);
|
|---|
| 63 | $aggregate->resolve(null);
|
|---|
| 64 | $called++;
|
|---|
| 65 | },
|
|---|
| 66 | 'rejected' => function (\Exception $reason) {
|
|---|
| 67 | $this->fail($reason->getMessage());
|
|---|
| 68 | }
|
|---|
| 69 | ]);
|
|---|
| 70 | $p = $each->promise();
|
|---|
| 71 | $p->wait();
|
|---|
| 72 | $this->assertNull($p->wait());
|
|---|
| 73 | $this->assertEquals(1, $called);
|
|---|
| 74 | $this->assertEquals(PromiseInterface::FULFILLED, $a->getState());
|
|---|
| 75 | $this->assertEquals(PromiseInterface::PENDING, $b->getState());
|
|---|
| 76 | // Resolving $b has no effect on the aggregate promise.
|
|---|
| 77 | $b->resolve('foo');
|
|---|
| 78 | $this->assertEquals(1, $called);
|
|---|
| 79 | }
|
|---|
| 80 |
|
|---|
| 81 | public function testLimitsPendingPromises()
|
|---|
| 82 | {
|
|---|
| 83 | $pending = [new Promise(), new Promise(), new Promise(), new Promise()];
|
|---|
| 84 | $promises = new \ArrayIterator($pending);
|
|---|
| 85 | $each = new EachPromise($promises, ['concurrency' => 2]);
|
|---|
| 86 | $p = $each->promise();
|
|---|
| 87 | $this->assertCount(2, $this->readAttribute($each, 'pending'));
|
|---|
| 88 | $pending[0]->resolve('a');
|
|---|
| 89 | $this->assertCount(2, $this->readAttribute($each, 'pending'));
|
|---|
| 90 | $this->assertTrue($promises->valid());
|
|---|
| 91 | $pending[1]->resolve('b');
|
|---|
| 92 | P\queue()->run();
|
|---|
| 93 | $this->assertCount(2, $this->readAttribute($each, 'pending'));
|
|---|
| 94 | $this->assertTrue($promises->valid());
|
|---|
| 95 | $promises[2]->resolve('c');
|
|---|
| 96 | P\queue()->run();
|
|---|
| 97 | $this->assertCount(1, $this->readAttribute($each, 'pending'));
|
|---|
| 98 | $this->assertEquals(PromiseInterface::PENDING, $p->getState());
|
|---|
| 99 | $promises[3]->resolve('d');
|
|---|
| 100 | P\queue()->run();
|
|---|
| 101 | $this->assertNull($this->readAttribute($each, 'pending'));
|
|---|
| 102 | $this->assertEquals(PromiseInterface::FULFILLED, $p->getState());
|
|---|
| 103 | $this->assertFalse($promises->valid());
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 | public function testDynamicallyLimitsPendingPromises()
|
|---|
| 107 | {
|
|---|
| 108 | $calls = [];
|
|---|
| 109 | $pendingFn = function ($count) use (&$calls) {
|
|---|
| 110 | $calls[] = $count;
|
|---|
| 111 | return 2;
|
|---|
| 112 | };
|
|---|
| 113 | $pending = [new Promise(), new Promise(), new Promise(), new Promise()];
|
|---|
| 114 | $promises = new \ArrayIterator($pending);
|
|---|
| 115 | $each = new EachPromise($promises, ['concurrency' => $pendingFn]);
|
|---|
| 116 | $p = $each->promise();
|
|---|
| 117 | $this->assertCount(2, $this->readAttribute($each, 'pending'));
|
|---|
| 118 | $pending[0]->resolve('a');
|
|---|
| 119 | $this->assertCount(2, $this->readAttribute($each, 'pending'));
|
|---|
| 120 | $this->assertTrue($promises->valid());
|
|---|
| 121 | $pending[1]->resolve('b');
|
|---|
| 122 | $this->assertCount(2, $this->readAttribute($each, 'pending'));
|
|---|
| 123 | P\queue()->run();
|
|---|
| 124 | $this->assertTrue($promises->valid());
|
|---|
| 125 | $promises[2]->resolve('c');
|
|---|
| 126 | P\queue()->run();
|
|---|
| 127 | $this->assertCount(1, $this->readAttribute($each, 'pending'));
|
|---|
| 128 | $this->assertEquals(PromiseInterface::PENDING, $p->getState());
|
|---|
| 129 | $promises[3]->resolve('d');
|
|---|
| 130 | P\queue()->run();
|
|---|
| 131 | $this->assertNull($this->readAttribute($each, 'pending'));
|
|---|
| 132 | $this->assertEquals(PromiseInterface::FULFILLED, $p->getState());
|
|---|
| 133 | $this->assertEquals([0, 1, 1, 1], $calls);
|
|---|
| 134 | $this->assertFalse($promises->valid());
|
|---|
| 135 | }
|
|---|
| 136 |
|
|---|
| 137 | public function testClearsReferencesWhenResolved()
|
|---|
| 138 | {
|
|---|
| 139 | $called = false;
|
|---|
| 140 | $a = new Promise(function () use (&$a, &$called) {
|
|---|
| 141 | $a->resolve('a');
|
|---|
| 142 | $called = true;
|
|---|
| 143 | });
|
|---|
| 144 | $each = new EachPromise([$a], [
|
|---|
| 145 | 'concurrency' => function () { return 1; },
|
|---|
| 146 | 'fulfilled' => function () {},
|
|---|
| 147 | 'rejected' => function () {}
|
|---|
| 148 | ]);
|
|---|
| 149 | $each->promise()->wait();
|
|---|
| 150 | $this->assertNull($this->readAttribute($each, 'onFulfilled'));
|
|---|
| 151 | $this->assertNull($this->readAttribute($each, 'onRejected'));
|
|---|
| 152 | $this->assertNull($this->readAttribute($each, 'iterable'));
|
|---|
| 153 | $this->assertNull($this->readAttribute($each, 'pending'));
|
|---|
| 154 | $this->assertNull($this->readAttribute($each, 'concurrency'));
|
|---|
| 155 | $this->assertTrue($called);
|
|---|
| 156 | }
|
|---|
| 157 |
|
|---|
| 158 | public function testCanBeCancelled()
|
|---|
| 159 | {
|
|---|
| 160 | $this->markTestIncomplete();
|
|---|
| 161 | }
|
|---|
| 162 |
|
|---|
| 163 | public function testDoesNotBlowStackWithFulfilledPromises()
|
|---|
| 164 | {
|
|---|
| 165 | $pending = [];
|
|---|
| 166 | for ($i = 0; $i < 100; $i++) {
|
|---|
| 167 | $pending[] = new FulfilledPromise($i);
|
|---|
| 168 | }
|
|---|
| 169 | $values = [];
|
|---|
| 170 | $each = new EachPromise($pending, [
|
|---|
| 171 | 'fulfilled' => function ($value) use (&$values) {
|
|---|
| 172 | $values[] = $value;
|
|---|
| 173 | }
|
|---|
| 174 | ]);
|
|---|
| 175 | $called = false;
|
|---|
| 176 | $each->promise()->then(function () use (&$called) {
|
|---|
| 177 | $called = true;
|
|---|
| 178 | });
|
|---|
| 179 | $this->assertFalse($called);
|
|---|
| 180 | P\queue()->run();
|
|---|
| 181 | $this->assertTrue($called);
|
|---|
| 182 | $this->assertEquals(range(0, 99), $values);
|
|---|
| 183 | }
|
|---|
| 184 |
|
|---|
| 185 | public function testDoesNotBlowStackWithRejectedPromises()
|
|---|
| 186 | {
|
|---|
| 187 | $pending = [];
|
|---|
| 188 | for ($i = 0; $i < 100; $i++) {
|
|---|
| 189 | $pending[] = new RejectedPromise($i);
|
|---|
| 190 | }
|
|---|
| 191 | $values = [];
|
|---|
| 192 | $each = new EachPromise($pending, [
|
|---|
| 193 | 'rejected' => function ($value) use (&$values) {
|
|---|
| 194 | $values[] = $value;
|
|---|
| 195 | }
|
|---|
| 196 | ]);
|
|---|
| 197 | $called = false;
|
|---|
| 198 | $each->promise()->then(
|
|---|
| 199 | function () use (&$called) { $called = true; },
|
|---|
| 200 | function () { $this->fail('Should not have rejected.'); }
|
|---|
| 201 | );
|
|---|
| 202 | $this->assertFalse($called);
|
|---|
| 203 | P\queue()->run();
|
|---|
| 204 | $this->assertTrue($called);
|
|---|
| 205 | $this->assertEquals(range(0, 99), $values);
|
|---|
| 206 | }
|
|---|
| 207 |
|
|---|
| 208 | public function testReturnsPromiseForWhatever()
|
|---|
| 209 | {
|
|---|
| 210 | $called = [];
|
|---|
| 211 | $arr = ['a', 'b'];
|
|---|
| 212 | $each = new EachPromise($arr, [
|
|---|
| 213 | 'fulfilled' => function ($v) use (&$called) { $called[] = $v; }
|
|---|
| 214 | ]);
|
|---|
| 215 | $p = $each->promise();
|
|---|
| 216 | $this->assertNull($p->wait());
|
|---|
| 217 | $this->assertEquals(['a', 'b'], $called);
|
|---|
| 218 | }
|
|---|
| 219 |
|
|---|
| 220 | public function testRejectsAggregateWhenNextThrows()
|
|---|
| 221 | {
|
|---|
| 222 | $iter = function () {
|
|---|
| 223 | yield 'a';
|
|---|
| 224 | throw new \Exception('Failure');
|
|---|
| 225 | };
|
|---|
| 226 | $each = new EachPromise($iter());
|
|---|
| 227 | $p = $each->promise();
|
|---|
| 228 | $e = null;
|
|---|
| 229 | $received = null;
|
|---|
| 230 | $p->then(null, function ($reason) use (&$e) { $e = $reason; });
|
|---|
| 231 | P\queue()->run();
|
|---|
| 232 | $this->assertInstanceOf('Exception', $e);
|
|---|
| 233 | $this->assertEquals('Failure', $e->getMessage());
|
|---|
| 234 | }
|
|---|
| 235 |
|
|---|
| 236 | public function testDoesNotCallNextOnIteratorUntilNeededWhenWaiting()
|
|---|
| 237 | {
|
|---|
| 238 | $results = [];
|
|---|
| 239 | $values = [10];
|
|---|
| 240 | $remaining = 9;
|
|---|
| 241 | $iter = function () use (&$values) {
|
|---|
| 242 | while ($value = array_pop($values)) {
|
|---|
| 243 | yield $value;
|
|---|
| 244 | }
|
|---|
| 245 | };
|
|---|
| 246 | $each = new EachPromise($iter(), [
|
|---|
| 247 | 'concurrency' => 1,
|
|---|
| 248 | 'fulfilled' => function ($r) use (&$results, &$values, &$remaining) {
|
|---|
| 249 | $results[] = $r;
|
|---|
| 250 | if ($remaining > 0) {
|
|---|
| 251 | $values[] = $remaining--;
|
|---|
| 252 | }
|
|---|
| 253 | }
|
|---|
| 254 | ]);
|
|---|
| 255 | $each->promise()->wait();
|
|---|
| 256 | $this->assertEquals(range(10, 1), $results);
|
|---|
| 257 | }
|
|---|
| 258 |
|
|---|
| 259 | public function testDoesNotCallNextOnIteratorUntilNeededWhenAsync()
|
|---|
| 260 | {
|
|---|
| 261 | $firstPromise = new Promise();
|
|---|
| 262 | $pending = [$firstPromise];
|
|---|
| 263 | $values = [$firstPromise];
|
|---|
| 264 | $results = [];
|
|---|
| 265 | $remaining = 9;
|
|---|
| 266 | $iter = function () use (&$values) {
|
|---|
| 267 | while ($value = array_pop($values)) {
|
|---|
| 268 | yield $value;
|
|---|
| 269 | }
|
|---|
| 270 | };
|
|---|
| 271 | $each = new EachPromise($iter(), [
|
|---|
| 272 | 'concurrency' => 1,
|
|---|
| 273 | 'fulfilled' => function ($r) use (&$results, &$values, &$remaining, &$pending) {
|
|---|
| 274 | $results[] = $r;
|
|---|
| 275 | if ($remaining-- > 0) {
|
|---|
| 276 | $pending[] = $values[] = new Promise();
|
|---|
| 277 | }
|
|---|
| 278 | }
|
|---|
| 279 | ]);
|
|---|
| 280 | $i = 0;
|
|---|
| 281 | $each->promise();
|
|---|
| 282 | while ($promise = array_pop($pending)) {
|
|---|
| 283 | $promise->resolve($i++);
|
|---|
| 284 | P\queue()->run();
|
|---|
| 285 | }
|
|---|
| 286 | $this->assertEquals(range(0, 9), $results);
|
|---|
| 287 | }
|
|---|
| 288 | }
|
|---|