2017-12-06 01:21:39 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Amp\Parallel\Test\Context;
|
|
|
|
|
2018-10-06 17:05:49 +02:00
|
|
|
use Amp\Loop;
|
2017-12-06 01:21:39 +01:00
|
|
|
use Amp\Parallel\Context\Process;
|
|
|
|
use Amp\PHPUnit\TestCase;
|
|
|
|
|
2018-10-07 16:50:45 +02:00
|
|
|
class ProcessTest extends TestCase
|
|
|
|
{
|
|
|
|
public function testBasicProcess()
|
|
|
|
{
|
2018-10-06 17:05:49 +02:00
|
|
|
Loop::run(function () {
|
|
|
|
$process = new Process([
|
|
|
|
__DIR__ . "/test-process.php",
|
|
|
|
"Test"
|
|
|
|
]);
|
|
|
|
yield $process->start();
|
|
|
|
$this->assertSame("Test", yield $process->join());
|
|
|
|
});
|
2017-12-06 01:21:39 +01:00
|
|
|
}
|
2017-12-10 22:37:33 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \Amp\Parallel\Sync\PanicError
|
|
|
|
* @expectedExceptionMessage No string provided
|
|
|
|
*/
|
2018-10-07 16:50:45 +02:00
|
|
|
public function testFailingProcess()
|
|
|
|
{
|
2018-10-06 17:05:49 +02:00
|
|
|
Loop::run(function () {
|
|
|
|
$process = new Process(__DIR__ . "/test-process.php");
|
|
|
|
yield $process->start();
|
|
|
|
yield $process->join();
|
|
|
|
});
|
2017-12-10 22:37:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \Amp\Parallel\Sync\PanicError
|
2019-01-09 17:25:30 +01:00
|
|
|
* @expectedExceptionMessage No script found at '../test-process.php'
|
2017-12-10 22:37:33 +01:00
|
|
|
*/
|
2018-10-07 16:50:45 +02:00
|
|
|
public function testInvalidScriptPath()
|
|
|
|
{
|
2018-10-06 17:05:49 +02:00
|
|
|
Loop::run(function () {
|
2019-01-09 17:25:30 +01:00
|
|
|
$process = new Process("../test-process.php");
|
2018-10-06 17:05:49 +02:00
|
|
|
yield $process->start();
|
|
|
|
yield $process->join();
|
|
|
|
});
|
2017-12-10 22:37:33 +01:00
|
|
|
}
|
2017-12-13 20:56:11 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \Amp\Parallel\Sync\PanicError
|
|
|
|
* @expectedExceptionMessage The given data cannot be sent because it is not serializable
|
|
|
|
*/
|
2018-10-07 16:50:45 +02:00
|
|
|
public function testInvalidResult()
|
|
|
|
{
|
2018-10-06 17:05:49 +02:00
|
|
|
Loop::run(function () {
|
|
|
|
$process = new Process(__DIR__ . "/invalid-result-process.php");
|
|
|
|
yield $process->start();
|
2018-10-07 16:50:45 +02:00
|
|
|
\var_dump(yield $process->join());
|
2018-10-06 17:05:49 +02:00
|
|
|
});
|
2017-12-13 20:56:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \Amp\Parallel\Sync\PanicError
|
2018-03-21 13:56:19 +01:00
|
|
|
* @expectedExceptionMessage did not return a callable function
|
2017-12-13 20:56:11 +01:00
|
|
|
*/
|
2018-10-07 16:50:45 +02:00
|
|
|
public function testNoCallbackReturned()
|
|
|
|
{
|
2018-10-06 17:05:49 +02:00
|
|
|
Loop::run(function () {
|
|
|
|
$process = new Process(__DIR__ . "/no-callback-process.php");
|
|
|
|
yield $process->start();
|
2018-10-07 16:50:45 +02:00
|
|
|
\var_dump(yield $process->join());
|
2018-10-06 17:05:49 +02:00
|
|
|
});
|
2017-12-13 20:56:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \Amp\Parallel\Sync\PanicError
|
|
|
|
* @expectedExceptionMessage Uncaught ParseError in execution context
|
|
|
|
*/
|
2018-10-07 16:50:45 +02:00
|
|
|
public function testParseError()
|
|
|
|
{
|
2018-10-06 17:05:49 +02:00
|
|
|
Loop::run(function () {
|
|
|
|
$process = new Process(__DIR__ . "/parse-error-process.inc");
|
|
|
|
yield $process->start();
|
2018-10-07 16:50:45 +02:00
|
|
|
\var_dump(yield $process->join());
|
2018-10-06 17:05:49 +02:00
|
|
|
});
|
2017-12-13 20:56:11 +01:00
|
|
|
}
|
2019-01-09 17:25:30 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \Amp\Parallel\Context\ContextException
|
|
|
|
* @expectedExceptionMessage Failed to receive result from process
|
|
|
|
*/
|
|
|
|
public function testKillWhenJoining()
|
|
|
|
{
|
|
|
|
Loop::run(function () {
|
|
|
|
$process = new Process([
|
|
|
|
__DIR__ . "/sleep-process.php",
|
|
|
|
5,
|
|
|
|
]);
|
|
|
|
yield $process->start();
|
|
|
|
$promise = $process->join();
|
|
|
|
$process->kill();
|
|
|
|
$this->assertFalse($process->isRunning());
|
|
|
|
yield $promise;
|
|
|
|
});
|
|
|
|
}
|
2017-12-06 01:21:39 +01:00
|
|
|
}
|