/var/www/hkosl.com/littleark/webadmin/libraies/guzzlehttp/promises/tests/EachPromiseTest.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
<?php
namespace GuzzleHttp\Promise\Tests;

use 
GuzzleHttp\Promise\RejectedPromise;
use 
GuzzleHttp\Promise\FulfilledPromise;
use 
GuzzleHttp\Promise\Promise;
use 
GuzzleHttp\Promise\PromiseInterface;
use 
GuzzleHttp\Promise\EachPromise;
use 
GuzzleHttp\Promise as P;

/**
 * @covers GuzzleHttp\Promise\EachPromise
 */
class EachPromiseTest extends \PHPUnit_Framework_TestCase
{
    public function 
testReturnsSameInstance()
    {
        
$each = new EachPromise([], ['concurrency' => 100]);
        
$this->assertSame($each->promise(), $each->promise());
    }

    public function 
testInvokesAllPromises()
    {
        
$promises = [new Promise(), new Promise(), new Promise()];
        
$called = [];
        
$each = new EachPromise($promises, [
            
'fulfilled' => function ($value) use (&$called) {
                
$called[] = $value;
            }
        ]);
        
$p $each->promise();
        
$promises[0]->resolve('a');
        
$promises[1]->resolve('c');
        
$promises[2]->resolve('b');
        
P\queue()->run();
        
$this->assertEquals(['a''c''b'], $called);
        
$this->assertEquals(PromiseInterface::FULFILLED$p->getState());
    }

    public function 
testIsWaitable()
    {
        
$a $this->createSelfResolvingPromise('a');
        
$b $this->createSelfResolvingPromise('b');
        
$called = [];
        
$each = new EachPromise([$a$b], [
            
'fulfilled' => function ($value) use (&$called) { $called[] = $value; }
        ]);
        
$p $each->promise();
        
$this->assertNull($p->wait());
        
$this->assertEquals(PromiseInterface::FULFILLED$p->getState());
        
$this->assertEquals(['a''b'], $called);
    }

    public function 
testCanResolveBeforeConsumingAll()
    {
        
$called 0;
        
$a $this->createSelfResolvingPromise('a');
        
$b = new Promise(function () { $this->fail(); });
        
$each = new EachPromise([$a$b], [
            
'fulfilled' => function ($value$idxPromise $aggregate) use (&$called) {
                
$this->assertSame($idx0);
                
$this->assertEquals('a'$value);
                
$aggregate->resolve(null);
                
$called++;
            },
            
'rejected' => function (\Exception $reason) {
                
$this->fail($reason->getMessage());
            }
        ]);
        
$p $each->promise();
        
$p->wait();
        
$this->assertNull($p->wait());
        
$this->assertEquals(1$called);
        
$this->assertEquals(PromiseInterface::FULFILLED$a->getState());
        
$this->assertEquals(PromiseInterface::PENDING$b->getState());
        
// Resolving $b has no effect on the aggregate promise.
        
$b->resolve('foo');
        
$this->assertEquals(1$called);
    }

    public function 
testLimitsPendingPromises()
    {
        
$pending = [new Promise(), new Promise(), new Promise(), new Promise()];
        
$promises = new \ArrayIterator($pending);
        
$each = new EachPromise($promises, ['concurrency' => 2]);
        
$p $each->promise();
        
$this->assertCount(2$this->readAttribute($each'pending'));
        
$pending[0]->resolve('a');
        
$this->assertCount(2$this->readAttribute($each'pending'));
        
$this->assertTrue($promises->valid());
        
$pending[1]->resolve('b');
        
P\queue()->run();
        
$this->assertCount(2$this->readAttribute($each'pending'));
        
$this->assertTrue($promises->valid());
        
$promises[2]->resolve('c');
        
P\queue()->run();
        
$this->assertCount(1$this->readAttribute($each'pending'));
        
$this->assertEquals(PromiseInterface::PENDING$p->getState());
        
$promises[3]->resolve('d');
        
P\queue()->run();
        
$this->assertNull($this->readAttribute($each'pending'));
        
$this->assertEquals(PromiseInterface::FULFILLED$p->getState());
        
$this->assertFalse($promises->valid());
    }

    public function 
testDynamicallyLimitsPendingPromises()
    {
        
$calls = [];
        
$pendingFn = function ($count) use (&$calls) {
            
$calls[] = $count;
            return 
2;
        };
        
$pending = [new Promise(), new Promise(), new Promise(), new Promise()];
        
$promises = new \ArrayIterator($pending);
        
$each = new EachPromise($promises, ['concurrency' => $pendingFn]);
        
$p $each->promise();
        
$this->assertCount(2$this->readAttribute($each'pending'));
        
$pending[0]->resolve('a');
        
$this->assertCount(2$this->readAttribute($each'pending'));
        
$this->assertTrue($promises->valid());
        
$pending[1]->resolve('b');
        
$this->assertCount(2$this->readAttribute($each'pending'));
        
P\queue()->run();
        
$this->assertTrue($promises->valid());
        
$promises[2]->resolve('c');
        
P\queue()->run();
        
$this->assertCount(1$this->readAttribute($each'pending'));
        
$this->assertEquals(PromiseInterface::PENDING$p->getState());
        
$promises[3]->resolve('d');
        
P\queue()->run();
        
$this->assertNull($this->readAttribute($each'pending'));
        
$this->assertEquals(PromiseInterface::FULFILLED$p->getState());
        
$this->assertEquals([0111], $calls);
        
$this->assertFalse($promises->valid());
    }

    public function 
testClearsReferencesWhenResolved()
    {
        
$called false;
        
$a = new Promise(function () use (&$a, &$called) {
            
$a->resolve('a');
            
$called true;
        });
        
$each = new EachPromise([$a], [
            
'concurrency'       => function () { return 1; },
            
'fulfilled' => function () {},
            
'rejected'  => function () {}
        ]);
        
$each->promise()->wait();
        
$this->assertNull($this->readAttribute($each'onFulfilled'));
        
$this->assertNull($this->readAttribute($each'onRejected'));
        
$this->assertNull($this->readAttribute($each'iterable'));
        
$this->assertNull($this->readAttribute($each'pending'));
        
$this->assertNull($this->readAttribute($each'concurrency'));
        
$this->assertTrue($called);
    }

    public function 
testCanBeCancelled()
    {
        
$this->markTestIncomplete();
    }

    public function 
testFulfillsImmediatelyWhenGivenAnEmptyIterator()
    {
        
$each = new EachPromise(new \ArrayIterator([]));
        
$result $each->promise()->wait();
    }

    public function 
testDoesNotBlowStackWithFulfilledPromises()
    {
        
$pending = [];
        for (
$i 0$i 100$i++) {
            
$pending[] = new FulfilledPromise($i);
        }
        
$values = [];
        
$each = new EachPromise($pending, [
            
'fulfilled' => function ($value) use (&$values) {
                
$values[] = $value;
            }
        ]);
        
$called false;
        
$each->promise()->then(function () use (&$called) {
            
$called true;
        });
        
$this->assertFalse($called);
        
P\queue()->run();
        
$this->assertTrue($called);
        
$this->assertEquals(range(099), $values);
    }

    public function 
testDoesNotBlowStackWithRejectedPromises()
    {
        
$pending = [];
        for (
$i 0$i 100$i++) {
            
$pending[] = new RejectedPromise($i);
        }
        
$values = [];
        
$each = new EachPromise($pending, [
            
'rejected' => function ($value) use (&$values) {
                
$values[] = $value;
            }
        ]);
        
$called false;
        
$each->promise()->then(
            function () use (&
$called) { $called true; },
            function () { 
$this->fail('Should not have rejected.'); }
        );
        
$this->assertFalse($called);
        
P\queue()->run();
        
$this->assertTrue($called);
        
$this->assertEquals(range(099), $values);
    }

    public function 
testReturnsPromiseForWhatever()
    {
        
$called = [];
        
$arr = ['a''b'];
        
$each = new EachPromise($arr, [
            
'fulfilled' => function ($v) use (&$called) { $called[] = $v; }
        ]);
        
$p $each->promise();
        
$this->assertNull($p->wait());
        
$this->assertEquals(['a''b'], $called);
    }

    public function 
testRejectsAggregateWhenNextThrows()
    {
        
$iter = function () {
            
yield 'a';
            throw new \
Exception('Failure');
        };
        
$each = new EachPromise($iter());
        
$p $each->promise();
        
$e null;
        
$received null;
        
$p->then(null, function ($reason) use (&$e) { $e $reason; });
        
P\queue()->run();
        
$this->assertInstanceOf('Exception'$e);
        
$this->assertEquals('Failure'$e->getMessage());
    }

    public function 
testDoesNotCallNextOnIteratorUntilNeededWhenWaiting()
    {
        
$results = [];
        
$values = [10];
        
$remaining 9;
        
$iter = function () use (&$values) {
            while (
$value array_pop($values)) {
                
yield $value;
            }
        };
        
$each = new EachPromise($iter(), [
            
'concurrency' => 1,
            
'fulfilled' => function ($r) use (&$results, &$values, &$remaining) {
                
$results[] = $r;
                if (
$remaining 0) {
                    
$values[] = $remaining--;
                }
            }
        ]);
        
$each->promise()->wait();
        
$this->assertEquals(range(101), $results);
    }

    public function 
testDoesNotCallNextOnIteratorUntilNeededWhenAsync()
    {
        
$firstPromise = new Promise();
        
$pending = [$firstPromise];
        
$values = [$firstPromise];
        
$results = [];
        
$remaining 9;
        
$iter = function () use (&$values) {
            while (
$value array_pop($values)) {
                
yield $value;
            }
        };
        
$each = new EachPromise($iter(), [
            
'concurrency' => 1,
            
'fulfilled' => function ($r) use (&$results, &$values, &$remaining, &$pending) {
                
$results[] = $r;
                if (
$remaining-- > 0) {
                    
$pending[] = $values[] = new Promise();
                }
            }
        ]);
        
$i 0;
        
$each->promise();
        while (
$promise array_pop($pending)) {
            
$promise->resolve($i++);
            
P\queue()->run();
        }
        
$this->assertEquals(range(09), $results);
    }

    private function 
createSelfResolvingPromise($value)
    {
        
$p = new Promise(function () use (&$p$value) {
            
$p->resolve($value);
        });

        return 
$p;
    }

    public function 
testMutexPreventsGeneratorRecursion()
    {
        
$results $promises = [];
        for (
$i 0$i 20$i++) {
            
$p $this->createSelfResolvingPromise($i);
            
$pending[] = $p;
            
$promises[] = $p;
        }

        
$iter = function () use (&$promises, &$pending) {
            foreach (
$promises as $promise) {
                
// Resolve a promises, which will trigger the then() function,
                // which would cause the EachPromise to try to add more
                // promises to the queue. Without a lock, this would trigger
                // a "Cannot resume an already running generator" fatal error.
                
if ($p array_pop($pending)) {
                    
$p->wait();
                }
                
yield $promise;
            }
        };

        
$each = new EachPromise($iter(), [
            
'concurrency' => 5,
            
'fulfilled' => function ($r) use (&$results, &$pending) {
                
$results[] = $r;
            }
        ]);

        
$each->promise()->wait();
        
$this->assertCount(20$results);
    }
}