You are here

public function EachPromiseTest::testDynamicallyLimitsPendingPromises in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/guzzlehttp/promises/tests/EachPromiseTest.php \GuzzleHttp\Promise\Tests\EachPromiseTest::testDynamicallyLimitsPendingPromises()

File

vendor/guzzlehttp/promises/tests/EachPromiseTest.php, line 106

Class

EachPromiseTest
@covers GuzzleHttp\Promise\EachPromise

Namespace

GuzzleHttp\Promise\Tests

Code

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([
    0,
    1,
    1,
    1,
  ], $calls);
  $this
    ->assertFalse($promises
    ->valid());
}