Example #1
0
 /**
  * Run test method as a coroutine.
  */
 public final function runTestCaseWithinLoop(...$args)
 {
     $this->setName($this->testMethodNameBackup);
     $result = $this->{$this->testMethodNameBackup}(...$args);
     if ($result instanceof \Generator) {
         $generator = $result;
         $loop = $this->getLoop();
         $e = null;
         try {
             Loop::execute(function () use(&$result, &$e) {
                 $coroutine = new Coroutine($result);
                 $coroutine->when(function ($error, $val = null) use(&$result, &$e) {
                     if ($error) {
                         $e = $error;
                     } else {
                         $result = $val;
                     }
                     $this->disposeTest();
                 });
             }, $loop);
         } finally {
             if ($this->loggers) {
                 try {
                     foreach ($this->loggers as $handler) {
                         $this->getLoopConfig()->getLogger()->removeHandler($handler);
                     }
                 } finally {
                     $this->loggers = [];
                 }
             }
             if ($loop instanceof KoolLoop) {
                 $loop->reset();
             }
         }
         if ($e !== null) {
             throw $e;
         }
         if ($generator->valid()) {
             throw new \RuntimeException('Async test case did not finish!');
         }
     }
     return $result;
 }
Example #2
0
 /**
  * {@inheritdoc}
  */
 public function resolve(string $host) : Awaitable
 {
     $host = \strtolower(\trim($host, '[]'));
     if ($host === 'localhost') {
         return new Success(new Address('::1', '127.0.0.1'));
     }
     if (Address::isResolved($host)) {
         return new Success(new Address($host));
     }
     if (isset($this->hosts[$host])) {
         return new Success($this->hosts[$host]);
     }
     if (isset($this->cache[$host]) && \time() < $this->cache[$host]['expires']) {
         return new Success($this->cache[$host]['address']);
     }
     if (!isset($this->pending[$host])) {
         $this->pending[$host] = $resolver = new Coroutine($this->resolveHost($host));
         $resolver->when(function (\Throwable $e = null, array $val = null) use($host) {
             unset($this->pending[$host]);
             if ($val) {
                 $this->cache[$host] = $val;
             }
         });
     }
     return new Task(new Transform($this->pending[$host], function (array $result) {
         return $result['address'];
     }));
 }