From 8ec002633b742f27af81cf49f988f41d3e42f354 Mon Sep 17 00:00:00 2001 From: Aaron Piotrowski Date: Sun, 14 Oct 2018 23:24:35 -0500 Subject: [PATCH] Return promise from Process::start(); int from Process::getPid() --- lib/Process.php | 25 +++++++++++++++++-------- test/ProcessTest.php | 16 +++++----------- 2 files changed, 22 insertions(+), 19 deletions(-) diff --git a/lib/Process.php b/lib/Process.php index 2e7a8cd..a3cade8 100644 --- a/lib/Process.php +++ b/lib/Process.php @@ -9,6 +9,7 @@ use Amp\Process\Internal\ProcessRunner; use Amp\Process\Internal\ProcessStatus; use Amp\Process\Internal\Windows\Runner as WindowsProcessRunner; use Amp\Promise; +use function Amp\call; class Process { @@ -30,6 +31,9 @@ class Process /** @var ProcessHandle */ private $handle; + /** @var int|null */ + private $pid; + /** * @param string|string[] $command Command to run. * @param string|null $cwd Working directory or use an empty string to use the working directory of the @@ -90,15 +94,20 @@ class Process /** * Start the process. * + * @return Promise Resolves with the PID. + * * @throws StatusError If the process has already been started. */ - public function start() + public function start(): Promise { if ($this->handle) { throw new StatusError("Process has already been started."); } - $this->handle = $this->processRunner->start($this->command, $this->cwd, $this->env, $this->options); + return call(function () { + $this->handle = $this->processRunner->start($this->command, $this->cwd, $this->env, $this->options); + return $this->pid = yield $this->handle->pidDeferred->promise(); + }); } /** @@ -152,17 +161,17 @@ class Process /** * Returns the PID of the child process. * - * @return Promise + * @return int * - * @throws StatusError If the process has not started. + * @throws StatusError If the process has not started or has not completed starting. */ - public function getPid(): Promise + public function getPid(): int { - if (!$this->handle) { - throw new StatusError("Process has not been started."); + if (!$this->pid) { + throw new StatusError("Process has not been started or has not completed starting."); } - return $this->handle->pidDeferred->promise(); + return $this->pid; } /** diff --git a/test/ProcessTest.php b/test/ProcessTest.php index 4f757a1..3dbc2f7 100644 --- a/test/ProcessTest.php +++ b/test/ProcessTest.php @@ -8,7 +8,6 @@ use Amp\Loop; use Amp\Process\Process; use Amp\Process\ProcessInputStream; use Amp\Process\ProcessOutputStream; -use Amp\Promise; use PHPUnit\Framework\TestCase; class ProcessTest extends TestCase @@ -60,9 +59,7 @@ class ProcessTest extends TestCase { Loop::run(function () { $process = new Process(self::CMD_PROCESS); - $process->start(); - - $this->assertInternalType('int', yield $process->getPid()); + $this->assertInternalType('int', yield $process->start()); $this->assertSame(0, yield $process->join()); }); } @@ -75,9 +72,8 @@ class ProcessTest extends TestCase Loop::run(function () { $process = new Process(self::CMD_PROCESS_SLOW); - $process->start(); + yield $process->start(); $process->signal(0); - $this->assertInstanceOf(Promise::class, $process->getPid()); $this->assertSame(0, yield $process->join()); }); } @@ -249,14 +245,12 @@ class ProcessTest extends TestCase /** * @expectedException \Amp\Process\StatusError - * @expectedExceptionMessage Process has not been started. + * @expectedExceptionMessage Process has not been started or has not completed starting. */ public function testProcessHasNotBeenStartedWithGetPid() { - Loop::run(function () { - $process = new Process(self::CMD_PROCESS); - yield $process->getPid(); - }); + $process = new Process(self::CMD_PROCESS); + $process->getPid(); } /**