Esempio n. 1
0
 /**
  * @override
  * @inheritDoc
  */
 public function execute($params = [])
 {
     $promise = Promise::doResolve($params);
     return $promise->then(function ($params) {
         return $this->command($params);
     });
 }
 /**
  *
  */
 public function testApiSome_RejectsPromise_IfAnyInputPromiseRejects_BeforeDesiredNumberOfInputsAreResolved()
 {
     $test = $this->getTest();
     $mock = $test->createCallableMock();
     $mock->expects($test->once())->method('__invoke')->with($test->identicalTo([1 => 2, 2 => 3]));
     Promise::some([Promise::doResolve(1), Promise::doReject(2), Promise::doReject(3)], 2)->then($test->expectCallableNever(), $mock);
 }
 /**
  *
  */
 public function testApiAny_ResolvesPromise_IfAnyInputPromisesResolve()
 {
     $test = $this->getTest();
     $mock = $test->createCallableMock();
     $mock->expects($test->once())->method('__invoke')->with($test->identicalTo(2));
     Promise::any([Promise::doReject(1), Promise::doResolve(2), Promise::doReject(3)])->then($mock);
 }
Esempio n. 4
0
 /**
  * @override
  * @inheritDoc
  */
 public function solve($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($this->solver($ex, $params));
 }
Esempio n. 5
0
 /**
  * Handler to be called when solver is requested.
  *
  * @param Error|Exception|string $ex
  * @param mixed[] $params
  * @return mixed
  * @throws RejectionException
  */
 protected function solver($ex, $params = [])
 {
     $promise = Promise::doResolve();
     foreach ($this->handlers as $handler) {
         $current = $handler;
         $promise = $promise->then(function () use($ex, $params, $current) {
             return Promise::doResolve($current->solve($ex, $params));
         });
     }
     return $promise;
 }
Esempio n. 6
0
 /**
  * @override
  * @inheritDoc
  */
 protected function command($params = [])
 {
     $runtime = $this->runtime;
     $channel = $this->channel;
     $promise = Promise::doResolve();
     return $promise->then(function () use($runtime) {
         return $runtime->getManager()->getRuntimes();
     })->then(function ($children) use($channel) {
         $promises = [];
         foreach ($children as $childAlias) {
             $req = $this->createRequest($channel, $childAlias, new RuntimeCommand('arch:status'));
             $promises[] = $req->call();
         }
         return Promise::all($promises);
     })->then(function ($childrenData) use($runtime) {
         return ['parent' => $runtime->getParent(), 'alias' => $runtime->getAlias(), 'name' => $runtime->getName(), 'state' => $runtime->getState(), 'children' => $childrenData];
     });
 }
 /**
  *
  */
 public function testApiSolver_CallsHandlersOneAfterAnother()
 {
     $ex = new Exception('Exception');
     $params = ['params1' => 'value1'];
     $queue = '';
     $solver1 = $this->getMock(SolverInterface::class, [], [], '', false);
     $solver1->expects($this->once())->method('solve')->with($ex, $params)->will($this->returnCallback(function () use(&$queue) {
         $queue .= 'A';
         return Promise::doResolve()->then(function () use(&$queue) {
             $queue .= 'C';
         });
     }));
     $solver2 = $this->getMock(SolverInterface::class, [], [], '', false);
     $solver2->expects($this->once())->method('solve')->with($ex, $params)->will($this->returnCallback(function () use(&$queue) {
         $queue .= 'B';
         return 'done';
     }));
     $solver = $this->createSolver([$solver1, $solver2]);
     $callable = $this->createCallableMock();
     $callable->expects($this->once())->method('__invoke')->with('done');
     $result = $this->callProtectedMethod($solver, 'solver', [$ex, $params]);
     $result->then($callable);
     $this->assertSame('ACB', $queue);
 }
Esempio n. 8
0
 /**
  * @override
  * @inheritDoc
  */
 public function destroyProject($flags = Runtime::DESTROY_FORCE_SOFT)
 {
     if (!isset($this->data['pid'])) {
         return Promise::doResolve("Project was not needed to be destroyed, because it had not existed.");
     }
     $manager = $this;
     $pid = $this->data['pid'];
     $alias = $this->data['alias'];
     $name = $this->data['name'];
     if ($flags === Runtime::DESTROY_KEEP) {
         return Promise::doReject(new ResourceOccupiedException("Project could not be destroyed with force level=DESTROY_KEEP."));
     } else {
         if ($flags === Runtime::DESTROY_FORCE_SOFT) {
             $req = $this->createRequest($this->channel, $alias, new RuntimeCommand('container:destroy'));
             return $req->call()->then(function ($value) use($manager) {
                 usleep(1000.0);
                 $manager->freeProject();
                 return $value;
             });
         } else {
             if ($flags === Runtime::DESTROY_FORCE) {
                 return $this->destroyProject(Runtime::DESTROY_FORCE_SOFT)->then(null, function () use($manager) {
                     return $manager->destroyProject(Runtime::DESTROY_FORCE_HARD);
                 });
             }
         }
     }
     if (!$this->system->existsPid($pid)) {
         return Promise::doResolve()->then(function () use($manager) {
             $manager->freeProject();
             return "Project was not needed to be destroyed, because it had not existed.";
         });
     } else {
         if (!$this->system->kill($pid)) {
             return Promise::doReject(new ResourceOccupiedException("Project could not be killed forcefully."));
         }
     }
     return Promise::doResolve()->then(function () use($manager) {
         $manager->freeProject();
         return "Process has been destroyed!";
     });
 }
 /**
  *
  */
 public function testApiAlways_DoesNotSuppressRejection_WhenHandlerReturnsPromise_ForRejectedPromise()
 {
     $deferred = $this->createDeferred();
     $test = $this->getTest();
     $exception = new Exception();
     $mock = $test->createCallableMock();
     $mock->expects($test->once())->method('__invoke')->with($test->identicalTo($exception));
     $deferred->reject($exception);
     $deferred->getPromise()->always(function () {
         return Promise::doResolve(1);
     })->then(null, $mock);
 }
Esempio n. 10
0
 /**
  * @override
  * @inheritDoc
  */
 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}]."));
     } else {
         if ($state === Runtime::STATE_STOPPED) {
             return Promise::doResolve('Runtime has been already stopped.');
         }
     }
     $this->setState(Runtime::STATE_STOPPED);
     $emitter = $this->getEventEmitter();
     $emitter->emit('beforeStop');
     $emitter->emit('stop');
     $emitter->emit('afterStop');
     return Promise::doResolve('Runtime has been stopped.');
 }
Esempio n. 11
0
 /**
  * @override
  * @inheritDoc
  */
 public function always(callable $onFulfilledOrRejected)
 {
     return $this->then(null, function ($reason) use($onFulfilledOrRejected) {
         return Promise::doResolve($onFulfilledOrRejected())->then(function () use($reason) {
             return new static($reason);
         });
     });
 }
Esempio n. 12
0
 /**
  * @override
  * @inheritDoc
  */
 public function always(callable $onFulfilledOrRejected)
 {
     return $this->then(function ($value) use($onFulfilledOrRejected) {
         return Promise::doResolve($onFulfilledOrRejected())->then(function () use($value) {
             return $value;
         });
     });
 }
Esempio n. 13
0
 /**
  *
  */
 public function testApiResolve_Returns_FromFulfillmentHandler()
 {
     $deferred = $this->createDeferred();
     $test = $this->getTest();
     $value = 5;
     $mock = $test->createCallableMock();
     $mock->expects($test->once())->method('__invoke')->with($test->identicalTo($value));
     $deferred->getPromise()->spread(function () use($value) {
         return Promise::doResolve($value);
     })->then($mock);
     $deferred->resolve();
 }
Esempio n. 14
0
 /**
  * @override
  * @inheritDoc
  */
 public function sendMessage($alias, $message, $flags = Channel::MODE_DEFAULT)
 {
     $result = $this->channel->send($alias, $message, $flags);
     return Promise::doResolve($result);
 }
Esempio n. 15
0
 /**
  * @param Error|Exception $ex
  * @param mixed[] $params
  * @return mixed
  */
 protected function solver($ex, $params = [])
 {
     $this->runtime->fail($ex, $params);
     return Promise::doResolve('Runtime has handled failure.');
 }
Esempio n. 16
0
 /**
  * @override
  * @inheritDoc
  */
 public function flushProcesses($flags = Runtime::DESTROY_KEEP)
 {
     return Promise::doResolve("Processes have been flushed.");
 }
Esempio n. 17
0
 /**
  * @override
  * @inheritDoc
  */
 public function getProcesses()
 {
     $list = [];
     foreach ($this->processes as $alias => $process) {
         $list[] = $alias;
     }
     return Promise::doResolve($list);
 }
Esempio n. 18
0
 /**
  * @override
  * @inheritDoc
  */
 public function getThreads()
 {
     $list = [];
     foreach ($this->threads as $alias => $wrapper) {
         $list[] = $alias;
     }
     return Promise::doResolve($list);
 }
Esempio n. 19
0
 /**
  *
  */
 public function testApiAlways_DoesNotSuppressCancellation_WhenHandlerReturnsPromise()
 {
     $deferred = $this->createDeferred();
     $test = $this->getTest();
     $value = new StdClass();
     $mock = $test->createCallableMock();
     $mock->expects($test->once())->method('__invoke')->with($test->identicalTo($value));
     $deferred->getPromise()->always(function () {
         return Promise::doResolve(1);
     })->then(null, null, $mock);
     $deferred->cancel($value);
 }
Esempio n. 20
0
 /**
  * @override
  * @inheritDoc
  */
 public function solve($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 for [{$classBaseEx}] is not registered."));
     }
     $try++;
     $params = array_merge($this->params, $params);
     $valueOrPromise = $this->getSolver($chosen)->solve($ex, $params);
     return Promise::doResolve($valueOrPromise);
 }
 /**
  *
  */
 public function testApiReduce_RejectsPromise_WhenInputContainsRejection()
 {
     $test = $this->getTest();
     $mock = $test->createCallableMock();
     $mock->expects($test->once())->method('__invoke')->with($test->identicalTo(2));
     Promise::reduce([Promise::doResolve(1), Promise::doReject(2), Promise::doResolve(3)], $this->plus(), Promise::doResolve(1))->then($test->expectCallableNever(), $mock);
 }
Esempio n. 22
0
 /**
  * @return callable
  */
 protected function promiseMapper()
 {
     return function ($val) {
         return Promise::doResolve($val * 2);
     };
 }
 /**
  *
  */
 public function testApiDoResolve_ReturnsPromise()
 {
     $test = $this->getTest();
     $test->assertInstanceOf(PromiseInterface::class, Promise::doResolve(1));
 }
Esempio n. 24
0
 /**
  * @param InputInterface  $input
  * @param OutputInterface $output
  * @return int|null|void
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $this->onStart();
     $promise = Promise::doResolve($this->command($input, $output));
     $promise->then(function ($value) {
         return $this->onSuccess($value);
     }, function ($ex) {
         return $this->onFailure($ex);
     }, function ($ex) {
         return $this->onCancel($ex);
     })->always(function () {
         $this->onStop();
     });
 }
Esempio n. 25
0
 /**
  * @override
  * @inheritDoc
  */
 public function flushThreads($flags = Runtime::DESTROY_KEEP)
 {
     return Promise::doResolve("Threads have been flushed.");
 }