You are here

public function EachPromiseTest::testDoesNotCallNextOnIteratorUntilNeededWhenAsync in Zircon Profile 8

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

File

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

Class

EachPromiseTest
@covers GuzzleHttp\Promise\EachPromise

Namespace

GuzzleHttp\Promise\Tests

Code

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(0, 9), $results);
}