/** * Run test scenario as simulation. * * @param callable(TestCase) $simulation * @return PromiseInterface */ public function simulate(callable $simulation) { $data = $simulation($this); $promise = new Promise(); $loop = $this->loop(); $loop->addTimer(15, function () use($loop) { $loop->stop(); $this->fail('Timeout for test has been reached.'); }); $loop->start(); return $promise->resolve($data); }
/** * @param mixed[] $params * @return PromiseInterface */ public function execute($params = []) { $promise = Promise::doResolve($params); return $promise->then(function ($params) { return $this->command($params); }); }
/** * @param Error|Exception $ex * @param mixed[] $params * @return PromiseInterface */ public function handle($ex, $params = []) { foreach ($this->requires as $require) { if (!isset($params[$require])) { return Promise::doReject(new IllegalCallException('Missing parameter [' . $require . '] for [' . get_class($this) . '].')); } } return Promise::doResolve([$ex, $params])->spread(function ($ex, $params) { return $this->handler($ex, $params); }); }
/** * @param Error|Exception $ex * @param mixed[] $params * @return mixed * @throws RejectionException */ protected function handler($ex, $params = []) { $promise = Promise::doResolve(); foreach ($this->handlers as $handler) { $current = $handler; $promise = $promise->then(function ($value) use($ex, $params, $current) { return Promise::doResolve($current->handle($ex, $params)); }); } return $promise; }
/** * @param Error|Exception $ex * @param mixed[] $params * @return mixed */ protected function handler($ex, $params = []) { $manager = $this->runtime->manager(); $alias = $params['origin']; if ($manager->existsThread($alias)) { return $manager->createThread($alias, null, Runtime::CREATE_FORCE); } else { if ($manager->existsProcess($alias)) { return $manager->createProcess($alias, null, Runtime::CREATE_FORCE); } } return Promise::doReject(new RejectionException("Runtime [{$alias}] does not exists.")); }
/** * @param mixed[] $params * @return mixed */ protected function command($params = []) { $runtime = $this->runtime; $channel = $this->channel; $promise = Promise::doResolve(); return $promise->then(function () use($runtime) { return $runtime->manager()->getRuntimes(); })->then(function ($children) use($channel) { $promises = []; foreach ($children as $childAlias) { $req = new Request($channel, $childAlias, new RuntimeCommand('arch:status')); $promises[] = $req->call(); } return Promise::all($promises); })->then(function ($childrenData) use($runtime) { return ['parent' => $runtime->parent(), 'alias' => $runtime->alias(), 'name' => $runtime->name(), 'state' => $runtime->state(), 'children' => $childrenData]; }); }
/** * @param mixed[] $params * @return mixed */ protected function command($params = []) { $runtime = $this->runtime; $channel = $this->channel; $promise = $this->runtime->stop(); return $promise->then(function () use($runtime) { return $runtime->manager()->getRuntimes(); })->then(function ($children) use($channel) { $promises = []; foreach ($children as $childAlias) { $req = new Request($channel, $childAlias, new RuntimeCommand('arch:stop')); $promises[] = $req->call(); } return Promise::all($promises); })->then(function () { return 'Part of architecture has been stopped.'; }, function () { throw new RejectionException('Part of architecture could not be stopped.'); }); }
/** * @param int $flags * @return PromiseInterface */ public function flushProcesses($flags = Runtime::DESTROY_KEEP) { return Promise::doReject(new RejectionException('Processes storage cannot be flushed.')); }
/** * @param Error|Exception $ex * @param mixed[] $params * @param int $try * @return PromiseInterface */ public function handle($ex, $params = [], &$try = 0) { $classBaseEx = get_class($ex); $classes = array_merge([$classBaseEx], class_parents($ex)); $indexMin = -1; $chosen = null; foreach ($classes as $class) { $indexCurrent = array_search($class, array_keys($this->rules), true); if ($indexCurrent !== false && ($indexMin === -1 || $indexCurrent < $indexMin)) { $indexMin = $indexCurrent; $chosen = $class; } } if ($chosen === null) { return Promise::doReject(new ExecutionException("SolverInterface [{$classBaseEx}] is not registered.")); } $try++; $params = array_merge($this->params, $params); $valueOrPromise = $this->getHandler($chosen)->handle($ex, $params); return Promise::doResolve($valueOrPromise); }
/** * @param string $command * @param mixed[] $params * @return PromiseInterface */ private function executeCommand($command, $params = []) { try { return $this->commander->execute($command, $params); } catch (Error $ex) { return Promise::doReject($ex); } catch (Exception $ex) { return Promise::doReject($ex); } }
/** * @param int $flags * @return PromiseInterface */ public function flushRuntimes($flags = Runtime::DESTROY_KEEP) { return Promise::all([$this->flushThreads($flags), $this->flushProcesses($flags)]); }
/** * @param Error|Exception $ex * @param mixed[] $params * @return mixed */ protected function handler($ex, $params = []) { $this->runtime->fail($ex, $params); return Promise::doResolve('Runtime has handled failure.'); }
/** * @return PromiseInterface */ public function stop() { $state = $this->getState(); if ($state === Runtime::STATE_CREATED || $state === Runtime::STATE_DESTROYED) { return Promise::doReject(new RejectionException("It is not possible to stop runtime from state [{$state}].")); } if ($state !== Runtime::STATE_STOPPED) { $this->setState(Runtime::STATE_STOPPED); $emitter = $this->eventEmitter(); $emitter->emit('beforeStop'); $emitter->emit('stop'); $emitter->emit('afterStop'); } return Promise::doResolve('Runtime has been stopped.'); }
/** * @param int $flags * @return PromiseInterface */ public function flushThreads($flags = Runtime::DESTROY_KEEP) { return Promise::doResolve("Threads have been flushed."); }
/** * @param int $flags * @return PromiseInterface */ public function flushProcesses($flags = Runtime::DESTROY_KEEP) { return Promise::doResolve("Processes have been flushed."); }
/** * @param int $flags * @return PromiseInterface */ public function flushProcesses($flags = Runtime::DESTROY_KEEP) { $promises = []; if ($flags !== Runtime::DESTROY_KEEP) { foreach ($this->processes as $alias => $process) { $promises[] = $this->destroyProcess($alias, $flags); } } return Promise::all($promises)->always(function () { $this->processes = []; $this->updateStorage(); })->then(function () { return 'Processes storage has been flushed.'; }); }