/**
  * @param RequestInterface[] $requests
  * @param array $options
  *
  * @return array
  *
  * @deprecated Use {@link send()}
  */
 public function performMultiple($requests, array $options = [])
 {
     $promises = [];
     foreach ($requests as $key => $request) {
         $promises[$key] = $this->send($request, isset($options[$key]) ? $options[$key] : []);
     }
     P\queue()->run();
     $responses = [];
     foreach ($promises as $key => $promise) {
         $responses[$key] = $promise->wait();
     }
     return $responses;
 }
Ejemplo n.º 2
0
 public function testCanYieldErrorsAndSuccessesWithoutRecursion()
 {
     $promises = [];
     for ($i = 0; $i < 20; $i++) {
         $promises[] = new P\Promise();
     }
     $co = P\coroutine(function () use($promises) {
         for ($i = 0; $i < 20; $i += 4) {
             try {
                 (yield $promises[$i]);
                 (yield $promises[$i + 1]);
             } catch (\Exception $e) {
                 (yield $promises[$i + 2]);
                 (yield $promises[$i + 3]);
             }
         }
     });
     for ($i = 0; $i < 20; $i += 4) {
         $promises[$i]->resolve($i);
         $promises[$i + 1]->reject($i + 1);
         $promises[$i + 2]->resolve($i + 2);
         $promises[$i + 3]->resolve($i + 3);
     }
     $co->then(function ($value) use(&$result) {
         $result = $value;
     });
     P\queue()->run();
     $this->assertEquals('19', $result);
 }
Ejemplo n.º 3
0
 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);
 }
Ejemplo n.º 4
0
 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());
 }
Ejemplo n.º 5
0
 public function testOtherwiseIsSugarForRejections()
 {
     $p = new Promise();
     $p->reject('foo');
     $p->otherwise(function ($v) use(&$c) {
         $c = $v;
     });
     P\queue()->run();
     $this->assertEquals($c, 'foo');
 }
Ejemplo n.º 6
0
 /**
  * Runs until all outstanding connections have completed.
  */
 public function execute()
 {
     $queue = P\queue();
     while ($this->handles || !$queue->isEmpty()) {
         // If there are no transfers, then sleep for the next delay
         if (!$this->active && $this->delays) {
             usleep($this->timeToNext());
         }
         $this->tick();
     }
 }
 public function requestAsync($method, $endpoint, array $data = array())
 {
     $promise = $this->client->requestAsync($method, $this->api_prefix . $endpoint, $this->getRequestOptions($method, $data))->then(function (ResponseInterface $res) {
         return $this->getBody($res);
     }, function (RequestException $e) {
         // See if we can simplify the exception.
         if (!$e->hasResponse()) {
             throw new StockfighterRequestException('', -1, 'No response from server.');
         }
         $this->handleResponseErrors($e->getResponse());
         // Otherwise, throw a general error.
         throw new StockfighterRequestException('', -1, 'Unknown error: ' . $e->getMessage());
     })->then($this->global_fulfilled, $this->global_rejected);
     // Add a timer to the loop to monitor this request.
     $timer = $this->stockfighter->loop->addPeriodicTimer(0, \Closure::bind(function () use(&$timer) {
         $this->tick();
         if (empty($this->handles) && Promise\queue()->isEmpty()) {
             $timer->cancel();
         }
     }, $this->handler, $this->handler));
     // Return the promise.
     return $promise;
 }