2016-12-30 02:16:04 +01:00
|
|
|
<?php
|
2015-09-26 06:41:15 +02:00
|
|
|
|
2017-12-08 04:26:55 +01:00
|
|
|
namespace Amp\Parallel\Test\Context;
|
2016-08-18 18:04:48 +02:00
|
|
|
|
2018-10-24 18:13:42 +02:00
|
|
|
use Amp\Delayed;
|
2017-03-16 23:03:59 +01:00
|
|
|
use Amp\Loop;
|
2019-02-18 06:50:41 +01:00
|
|
|
use Amp\Parallel\Context\Context;
|
2017-03-22 05:19:15 +01:00
|
|
|
use Amp\PHPUnit\TestCase;
|
2015-09-26 06:41:15 +02:00
|
|
|
|
2018-10-07 16:50:45 +02:00
|
|
|
abstract class AbstractContextTest extends TestCase
|
|
|
|
{
|
2019-02-18 06:50:41 +01:00
|
|
|
abstract public function createContext($script): Context;
|
2015-09-26 06:41:15 +02:00
|
|
|
|
2019-02-18 06:50:41 +01:00
|
|
|
public function testBasicProcess()
|
2018-10-07 16:50:45 +02:00
|
|
|
{
|
2018-10-06 17:05:49 +02:00
|
|
|
Loop::run(function () {
|
2019-02-18 06:50:41 +01:00
|
|
|
$context = $this->createContext([
|
|
|
|
__DIR__ . "/Fixtures/test-process.php",
|
|
|
|
"Test"
|
|
|
|
]);
|
2018-10-06 17:05:49 +02:00
|
|
|
yield $context->start();
|
2019-02-18 06:50:41 +01:00
|
|
|
$this->assertSame("Test", yield $context->join());
|
2018-10-06 17:05:49 +02:00
|
|
|
});
|
2015-09-26 06:41:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-12-08 04:26:55 +01:00
|
|
|
* @expectedException \Amp\Parallel\Sync\PanicError
|
2019-02-18 06:50:41 +01:00
|
|
|
* @expectedExceptionMessage No string provided
|
2015-09-26 06:41:15 +02:00
|
|
|
*/
|
2019-02-18 06:50:41 +01:00
|
|
|
public function testFailingProcess()
|
2018-10-07 16:50:45 +02:00
|
|
|
{
|
2017-03-16 23:03:59 +01:00
|
|
|
Loop::run(function () {
|
2019-02-18 06:50:41 +01:00
|
|
|
$context = $this->createContext(__DIR__ . "/Fixtures/test-process.php");
|
2018-10-06 17:05:49 +02:00
|
|
|
yield $context->start();
|
2016-08-19 00:36:58 +02:00
|
|
|
yield $context->join();
|
2017-03-16 23:03:59 +01:00
|
|
|
});
|
2015-09-26 06:41:15 +02:00
|
|
|
}
|
|
|
|
|
2015-12-12 07:34:41 +01:00
|
|
|
/**
|
2017-12-08 04:26:55 +01:00
|
|
|
* @expectedException \Amp\Parallel\Sync\PanicError
|
2019-02-18 06:50:41 +01:00
|
|
|
* @expectedExceptionMessage No script found at '../test-process.php'
|
2015-12-12 07:34:41 +01:00
|
|
|
*/
|
2019-02-18 06:50:41 +01:00
|
|
|
public function testInvalidScriptPath()
|
2018-10-07 16:50:45 +02:00
|
|
|
{
|
2017-03-16 23:03:59 +01:00
|
|
|
Loop::run(function () {
|
2019-02-18 06:50:41 +01:00
|
|
|
$context = $this->createContext("../test-process.php");
|
2018-10-06 17:05:49 +02:00
|
|
|
yield $context->start();
|
2016-08-19 00:36:58 +02:00
|
|
|
yield $context->join();
|
2017-03-16 23:03:59 +01:00
|
|
|
});
|
2015-12-12 07:34:41 +01:00
|
|
|
}
|
|
|
|
|
2015-09-26 06:41:15 +02:00
|
|
|
/**
|
2019-02-18 06:50:41 +01:00
|
|
|
* @expectedException \Amp\Parallel\Sync\PanicError
|
|
|
|
* @expectedExceptionMessage The given data cannot be sent because it is not serializable
|
2015-09-26 06:41:15 +02:00
|
|
|
*/
|
2019-02-18 06:50:41 +01:00
|
|
|
public function testInvalidResult()
|
2018-10-07 16:50:45 +02:00
|
|
|
{
|
2017-03-16 23:03:59 +01:00
|
|
|
Loop::run(function () {
|
2019-02-18 06:50:41 +01:00
|
|
|
$context = $this->createContext(__DIR__ . "/Fixtures/invalid-result-process.php");
|
2018-10-06 17:05:49 +02:00
|
|
|
yield $context->start();
|
2019-02-18 06:50:41 +01:00
|
|
|
\var_dump(yield $context->join());
|
2017-03-16 23:03:59 +01:00
|
|
|
});
|
2015-09-26 06:41:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-02-18 06:50:41 +01:00
|
|
|
* @expectedException \Amp\Parallel\Sync\PanicError
|
|
|
|
* @expectedExceptionMessage did not return a callable function
|
2015-09-26 06:41:15 +02:00
|
|
|
*/
|
2019-02-18 06:50:41 +01:00
|
|
|
public function testNoCallbackReturned()
|
2018-10-07 16:50:45 +02:00
|
|
|
{
|
2017-03-16 23:03:59 +01:00
|
|
|
Loop::run(function () {
|
2019-02-18 06:50:41 +01:00
|
|
|
$context = $this->createContext(__DIR__ . "/Fixtures/no-callback-process.php");
|
2018-10-06 17:05:49 +02:00
|
|
|
yield $context->start();
|
2019-02-18 06:50:41 +01:00
|
|
|
\var_dump(yield $context->join());
|
2017-03-16 23:03:59 +01:00
|
|
|
});
|
2015-09-26 06:41:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-02-18 06:50:41 +01:00
|
|
|
* @expectedException \Amp\Parallel\Sync\PanicError
|
|
|
|
* @expectedExceptionMessage contains a parse error
|
2015-09-26 06:41:15 +02:00
|
|
|
*/
|
2019-02-18 06:50:41 +01:00
|
|
|
public function testParseError()
|
2018-10-07 16:50:45 +02:00
|
|
|
{
|
2017-03-16 23:03:59 +01:00
|
|
|
Loop::run(function () {
|
2019-02-18 06:50:41 +01:00
|
|
|
$context = $this->createContext(__DIR__ . "/Fixtures/parse-error-process.inc");
|
2018-10-06 17:05:49 +02:00
|
|
|
yield $context->start();
|
2019-02-18 06:50:41 +01:00
|
|
|
\var_dump(yield $context->join());
|
2017-03-16 23:03:59 +01:00
|
|
|
});
|
2015-09-26 06:41:15 +02:00
|
|
|
}
|
2017-03-09 23:15:30 +01:00
|
|
|
|
|
|
|
/**
|
2017-12-08 04:26:55 +01:00
|
|
|
* @expectedException \Amp\Parallel\Context\ContextException
|
2019-02-18 06:50:41 +01:00
|
|
|
* @expectedExceptionMessage Failed to receive result
|
2017-03-09 23:15:30 +01:00
|
|
|
*/
|
2019-02-18 06:50:41 +01:00
|
|
|
public function testKillWhenJoining()
|
2018-10-07 16:50:45 +02:00
|
|
|
{
|
2017-03-16 23:03:59 +01:00
|
|
|
Loop::run(function () {
|
2019-02-18 06:50:41 +01:00
|
|
|
$context = $this->createContext([
|
|
|
|
__DIR__ . "/Fixtures/delayed-process.php",
|
|
|
|
5,
|
|
|
|
]);
|
2018-10-06 17:05:49 +02:00
|
|
|
yield $context->start();
|
2019-02-18 06:50:41 +01:00
|
|
|
yield new Delayed(100);
|
|
|
|
$promise = $context->join();
|
|
|
|
$context->kill();
|
|
|
|
$this->assertFalse($context->isRunning());
|
|
|
|
yield $promise;
|
2017-03-16 23:03:59 +01:00
|
|
|
});
|
2017-03-09 23:15:30 +01:00
|
|
|
}
|
2017-05-28 07:09:13 +02:00
|
|
|
|
|
|
|
/**
|
2019-02-18 06:50:41 +01:00
|
|
|
* @expectedException \Amp\Parallel\Context\ContextException
|
|
|
|
* @expectedExceptionMessage Failed to receive result
|
2017-05-28 07:09:13 +02:00
|
|
|
*/
|
2019-02-18 06:50:41 +01:00
|
|
|
public function testKillBusyContext()
|
2018-10-07 16:50:45 +02:00
|
|
|
{
|
2017-05-28 07:09:13 +02:00
|
|
|
Loop::run(function () {
|
2019-02-18 06:50:41 +01:00
|
|
|
$context = $this->createContext([
|
|
|
|
__DIR__ . "/Fixtures/sleep-process.php",
|
|
|
|
5,
|
|
|
|
]);
|
2018-10-24 18:13:42 +02:00
|
|
|
yield $context->start();
|
2019-02-18 06:50:41 +01:00
|
|
|
yield new Delayed(100);
|
|
|
|
$promise = $context->join();
|
|
|
|
$context->kill();
|
|
|
|
$this->assertFalse($context->isRunning());
|
|
|
|
yield $promise;
|
2017-05-28 07:09:13 +02:00
|
|
|
});
|
|
|
|
}
|
2019-04-30 21:47:37 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \Amp\Parallel\Context\ContextException
|
|
|
|
* @expectedExceptionMessage Failed to receive result
|
|
|
|
*/
|
|
|
|
public function testExitingProcess()
|
|
|
|
{
|
|
|
|
Loop::run(function () {
|
|
|
|
$context = $this->createContext([
|
|
|
|
__DIR__ . "/Fixtures/exiting-process.php",
|
|
|
|
5,
|
|
|
|
]);
|
|
|
|
yield $context->start();
|
|
|
|
yield $context->join();
|
|
|
|
});
|
|
|
|
}
|
2015-09-26 06:41:15 +02:00
|
|
|
}
|