public function testMapEndWithPendingItems() { $map = map(function ($n, callable $callback) { $this->eventLoop->nextTick(function () use($callback, $n) { $callback(null, 2 * $n); }); }, ['concurrency' => 5]); $items = []; $ended = false; $map->on('data', function ($n) use(&$items) { $items[] = $n; }); $map->on('end', function () use(&$ended) { $ended = true; }); for ($i = 0; $i < 10; $i++) { $map->write($i); } $map->end(); $this->assertFalse($ended); $this->assertEmpty($items); $this->eventLoop->tick(); $this->assertTrue($ended); $this->assertSame(range(0, 18, 2), $items); }
public function testFutureTickFiresBeforeIO() { $stream = $this->createStream(); $this->loop->addWriteStream($stream, function () { echo 'stream' . PHP_EOL; }); $this->loop->futureTick(function () { echo 'future-tick' . PHP_EOL; }); $this->expectOutputString('future-tick' . PHP_EOL . 'stream' . PHP_EOL); $this->loop->tick(); }
/** * {@inheritdoc} */ public function wait($unwrap = true) { if (null === $this->loop) { throw new \LogicException('You must set the loop before wait!'); } while (HttpPromise::PENDING === $this->getState()) { $this->loop->tick(); } if ($unwrap) { if (HttpPromise::REJECTED == $this->getState()) { throw $this->exception; } return $this->response; } }
protected function waitFor(PromiseInterface $promise, LoopInterface $loop) { $resolved = null; $exception = null; $promise->then(function ($c) use(&$resolved) { $resolved = $c; }, function ($error) use(&$exception) { $exception = $error; }); while ($resolved === null && $exception === null) { $loop->tick(); } if ($exception !== null) { throw $exception; } return $resolved; }
/** * @param array $requestArray * @return FutureArray */ public function __invoke(array $requestArray) { $request = new Request($requestArray['http_method'], $requestArray['url'], $requestArray['headers'], $requestArray['body'], $requestArray['version']); $ready = false; $httpRequest = $this->requestFactory->create($request, $requestArray['client'], $this->httpClient, $this->loop); return new FutureArray($httpRequest->then(function (ResponseInterface $response) use(&$ready, $requestArray) { $ready = true; $responseArray = ['effective_url' => $requestArray['url'], 'body' => $response->getBody(), 'headers' => $response->getHeaders(), 'status' => $response->getStatusCode(), 'reason' => $response->getReasonPhrase(), 'version' => $response->getProtocolVersion()]; return $responseArray; }, function ($error) use(&$ready) { $ready = true; return ['error' => $error]; }), function () use(&$ready) { do { $this->loop->tick(); } while (!$ready); }); }
/** * @param RequestInterface $request * @param array $options * @return Promise */ public function __invoke(RequestInterface $request, array $options) { $ready = false; $promise = new Promise(function () use(&$ready) { do { $this->loop->tick(); } while (!$ready); }); $this->requestFactory->create($request, $options, $this->httpClient, $this->loop)->then(function (ResponseInterface $response) use(&$ready, $promise) { $ready = true; $promise->resolve($response); $this->invokeQueue(); }, function ($error) use(&$ready, $promise) { $ready = true; $promise->reject($error); $this->invokeQueue(); }); return $promise; }
/** * Perform a single iteration of the event loop. */ public function tick() { $this->emit('tickStart'); $this->loop->tick(); $this->emit('tickDone'); }
/** * Execute a single event loop iteration */ public function tick() { $this->reactor->tick(); }
private function manyTicks() { for ($i = 0; $i < 20; $i++) { $this->loop->tick(); } }