Skip to content

Commit

Permalink
amphp#64 context restart
Browse files Browse the repository at this point in the history
  • Loading branch information
Ekstazi committed Dec 7, 2018
1 parent a5ae5b8 commit e77c06a
Show file tree
Hide file tree
Showing 8 changed files with 218 additions and 22 deletions.
8 changes: 7 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"amphp/amp": "^2",
"amphp/byte-stream": "^1.5",
"amphp/parser": "^1",
"amphp/process": "^1",
"amphp/process": "dev-master as 1.0.2",
"amphp/sync": "^1.0.1"
},
"require-dev": {
Expand Down Expand Up @@ -54,6 +54,12 @@
"php": "7.0.0"
}
},
"repositories": [
{
"type": "vcs",
"url": "https://github.com/Ekstazi/process"
}
],
"scripts": {
"check": [
"@cs",
Expand Down
7 changes: 7 additions & 0 deletions lib/Context/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,11 @@ public function kill();
* @throws \Amp\Parallel\Sync\PanicError If the context throws an uncaught exception.
*/
public function join(): Promise;

/**
* Restarts the execution context.
* @param $force bool Whether to force restart or wait until finish first
* @return Promise
*/
public function restart($force = false): Promise;
}
15 changes: 15 additions & 0 deletions lib/Context/Process.php
Original file line number Diff line number Diff line change
Expand Up @@ -358,4 +358,19 @@ public function kill()
{
$this->process->kill();
}

/**
* {@inheritdoc}
*/
public function restart($force = false): Promise
{
return call(function () use ($force) {
if ($force) {
$this->kill();
} else {
yield $this->join();
}
yield $this->start();
});
}
}
18 changes: 17 additions & 1 deletion lib/Context/Thread.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ public function kill()
} finally {
$this->close();
}
$this->oid = 0;
}
}

Expand Down Expand Up @@ -248,7 +249,7 @@ public function join(): Promise
Loop::disable($this->watcher);
$this->close();
}

$this->oid = 0;
return $response->getResult();
});
}
Expand Down Expand Up @@ -308,4 +309,19 @@ public function send($data): Promise
return $result;
});
}

/**
* {@inheritdoc}
*/
public function restart($force = false): Promise
{
return call(function () use ($force) {
if ($force) {
$this->kill();
} else {
yield $this->join();
}
yield $this->start();
});
}
}
12 changes: 12 additions & 0 deletions lib/Worker/Internal/WorkerProcess.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,16 @@ public function join(): Promise
{
return $this->process->join();
}

public function restart($force = false): Promise
{
return call(function () use ($force) {
if ($force) {
$this->kill();
} else {
yield $this->join();
}
return $this->start();
});
}
}
100 changes: 80 additions & 20 deletions test/Context/AbstractContextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,26 +66,6 @@ public function testStartWhileRunningThrowsError()
});
}

/**
* @expectedException \Amp\Parallel\Context\StatusError
*/
public function testStartMultipleTimesThrowsError()
{
$this->assertRunTimeGreaterThan(function () {
Loop::run(function () {
$context = $this->createContext(function () {
\sleep(1);
});

yield $context->start();
yield $context->join();

yield $context->start();
yield $context->join();
});
}, 2000);
}

/**
* @expectedException \Amp\Parallel\Sync\PanicError
*/
Expand Down Expand Up @@ -308,4 +288,84 @@ public function testExitingContextOnSend()
yield $context->send(\str_pad("", 1024 * 1024, "-"));
});
}

public function testStartAfterJoin()
{
$this->assertRunTimeGreaterThan(function () {
Loop::run(function () {
$context = $this->createContext(function () {
\usleep(100000);
});
for ($i=0; $i<=1;$i++) {
$this->assertFalse($context->isRunning());

yield $context->start();

$this->assertTrue($context->isRunning());

yield $context->join();
$this->assertFalse($context->isRunning());
}
});
}, 200);
}

public function testStartAfterKill()
{
$this->assertRunTimeLessThan(function () {
Loop::run(function () {
$context = $this->createContext(function () {
\usleep(100000);
});
for ($i=0; $i<=1;$i++) {
$this->assertFalse($context->isRunning());

yield $context->start();

$this->assertTrue($context->isRunning());

$this->assertRunTimeLessThan([$context, 'kill'], 1000);
$this->assertFalse($context->isRunning());
}
});
}, 200);
}

public function testRestart()
{
$this->assertRunTimeGreaterThan(function () {
Loop::run(function () {
$context = $this->createContext(function () {
\usleep(100000);
});
$this->assertFalse($context->isRunning());
yield $context->start();

for ($i = 0; $i <= 1; $i++) {
$this->assertTrue($context->isRunning());

yield $context->restart();
}
});
}, 200);
}

public function testForceRestart()
{
$this->assertRunTimeLessThan(function () {
Loop::run(function () {
$context = $this->createContext(function () {
\usleep(100000);
});
$this->assertFalse($context->isRunning());
yield $context->start();

for ($i = 0; $i <= 1; $i++) {
$this->assertTrue($context->isRunning());

yield $context->restart(true);
}
});
}, 200);
}
}
72 changes: 72 additions & 0 deletions test/Context/ProcessTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,76 @@ public function testParseError()
\var_dump(yield $process->join());
});
}

public function testStartAfterJoin()
{
$this->assertRunTimeGreaterThan(function () {
Loop::run(function () {
$context = new Process(__DIR__ . "/wait-process.php");
for ($i=0; $i<=1; $i++) {
$this->assertFalse($context->isRunning());

yield $context->start();

$this->assertTrue($context->isRunning());

yield $context->join();
$this->assertFalse($context->isRunning());
}
});
}, 2000);
}

public function testStartAfterKill()
{
$this->assertRunTimeLessThan(function () {
Loop::run(function () {
$context = new Process(__DIR__ . "/wait-process.php");
for ($i=0; $i<=1;$i++) {
$this->assertFalse($context->isRunning());

yield $context->start();

$this->assertTrue($context->isRunning());

$this->assertRunTimeLessThan([$context, 'kill'], 1000);
$this->assertFalse($context->isRunning());
}
});
}, 2000);
}

public function testRestart()
{
$this->assertRunTimeGreaterThan(function () {
Loop::run(function () {
$context = new Process(__DIR__ . "/wait-process.php");
$this->assertFalse($context->isRunning());
yield $context->start();

for ($i = 0; $i <= 1; $i++) {
$this->assertTrue($context->isRunning());

yield $context->restart();
}
});
}, 2000);
}

public function testForceRestart()
{
$this->assertRunTimeLessThan(function () {
Loop::run(function () {
$context = new Process(__DIR__ . "/wait-process.php");
$this->assertFalse($context->isRunning());
yield $context->start();

for ($i = 0; $i <= 1; $i++) {
$this->assertTrue($context->isRunning());

yield $context->restart(true);
}
});
}, 2000);
}
}
8 changes: 8 additions & 0 deletions test/Context/wait-process.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

use Amp\Parallel\Sync\Channel;

return function (Channel $channel) use ($argv): string {
\usleep(1000000);
return 'test';
};

0 comments on commit e77c06a

Please sign in to comment.