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;
|
2019-02-18 06:50:41 +01:00
|
|
|
use Amp\Parallel\Context\Context;
|
2019-08-27 19:17:41 +02:00
|
|
|
use Amp\Parallel\Context\ContextException;
|
2020-02-11 18:06:30 +01:00
|
|
|
use Amp\Parallel\Sync\ContextPanicError;
|
2019-08-27 19:17:41 +02:00
|
|
|
use Amp\PHPUnit\AsyncTestCase;
|
2015-09-26 06:41:15 +02:00
|
|
|
|
2019-08-27 19:17:41 +02:00
|
|
|
abstract class AbstractContextTest extends AsyncTestCase
|
2018-10-07 16:50:45 +02:00
|
|
|
{
|
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
|
|
|
{
|
2019-08-27 19:17:41 +02:00
|
|
|
$context = $this->createContext([
|
2019-02-18 06:50:41 +01:00
|
|
|
__DIR__ . "/Fixtures/test-process.php",
|
|
|
|
"Test"
|
|
|
|
]);
|
2019-08-27 19:17:41 +02:00
|
|
|
yield $context->start();
|
|
|
|
$this->assertSame("Test", yield $context->join());
|
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
|
|
|
{
|
2020-02-11 18:06:30 +01:00
|
|
|
$this->expectException(ContextPanicError::class);
|
2019-08-27 19:17:41 +02:00
|
|
|
$this->expectExceptionMessage('No string provided');
|
|
|
|
|
|
|
|
$context = $this->createContext(__DIR__ . "/Fixtures/test-process.php");
|
|
|
|
yield $context->start();
|
|
|
|
yield $context->join();
|
2015-09-26 06:41:15 +02:00
|
|
|
}
|
|
|
|
|
2020-02-10 19:11:45 +01:00
|
|
|
public function testThrowingProcessOnReceive()
|
|
|
|
{
|
2020-02-11 18:06:30 +01:00
|
|
|
$this->expectException(ContextPanicError::class);
|
2020-02-10 19:11:45 +01:00
|
|
|
$this->expectExceptionMessage('Test message');
|
|
|
|
|
|
|
|
$context = $this->createContext(__DIR__ . "/Fixtures/throwing-process.php");
|
|
|
|
yield $context->start();
|
|
|
|
yield $context->receive();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testThrowingProcessOnSend()
|
|
|
|
{
|
2020-02-11 18:06:30 +01:00
|
|
|
$this->expectException(ContextPanicError::class);
|
2020-02-10 19:11:45 +01:00
|
|
|
$this->expectExceptionMessage('Test message');
|
|
|
|
|
|
|
|
$context = $this->createContext(__DIR__ . "/Fixtures/throwing-process.php");
|
|
|
|
yield $context->start();
|
2020-02-11 00:40:34 +01:00
|
|
|
yield new Delayed(100);
|
2020-02-10 19:11:45 +01:00
|
|
|
yield $context->send(1);
|
|
|
|
}
|
|
|
|
|
2019-02-18 06:50:41 +01:00
|
|
|
public function testInvalidScriptPath()
|
2018-10-07 16:50:45 +02:00
|
|
|
{
|
2020-02-11 18:06:30 +01:00
|
|
|
$this->expectException(ContextPanicError::class);
|
2019-08-27 19:17:41 +02:00
|
|
|
$this->expectExceptionMessage("No script found at '../test-process.php'");
|
|
|
|
|
|
|
|
$context = $this->createContext("../test-process.php");
|
|
|
|
yield $context->start();
|
|
|
|
yield $context->join();
|
2015-12-12 07:34:41 +01:00
|
|
|
}
|
|
|
|
|
2019-02-18 06:50:41 +01:00
|
|
|
public function testInvalidResult()
|
2018-10-07 16:50:45 +02:00
|
|
|
{
|
2020-02-11 18:06:30 +01:00
|
|
|
$this->expectException(ContextPanicError::class);
|
2019-08-27 19:17:41 +02:00
|
|
|
$this->expectExceptionMessage('The given data cannot be sent because it is not serializable');
|
|
|
|
|
|
|
|
$context = $this->createContext(__DIR__ . "/Fixtures/invalid-result-process.php");
|
|
|
|
yield $context->start();
|
|
|
|
\var_dump(yield $context->join());
|
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
|
|
|
{
|
2020-02-11 18:06:30 +01:00
|
|
|
$this->expectException(ContextPanicError::class);
|
2019-08-27 19:17:41 +02:00
|
|
|
$this->expectExceptionMessage('did not return a callable function');
|
|
|
|
|
|
|
|
$context = $this->createContext(__DIR__ . "/Fixtures/no-callback-process.php");
|
|
|
|
yield $context->start();
|
|
|
|
\var_dump(yield $context->join());
|
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
|
|
|
{
|
2020-02-11 18:06:30 +01:00
|
|
|
$this->expectException(ContextPanicError::class);
|
2019-08-27 19:17:41 +02:00
|
|
|
$this->expectExceptionMessage('contains a parse error');
|
|
|
|
|
|
|
|
$context = $this->createContext(__DIR__ . "/Fixtures/parse-error-process.inc");
|
|
|
|
yield $context->start();
|
|
|
|
yield $context->join();
|
2015-09-26 06:41:15 +02:00
|
|
|
}
|
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
|
|
|
{
|
2019-08-27 19:17:41 +02:00
|
|
|
$this->expectException(ContextException::class);
|
|
|
|
$this->expectExceptionMessage('Failed to receive result');
|
|
|
|
|
|
|
|
$context = $this->createContext([
|
2019-02-18 06:50:41 +01:00
|
|
|
__DIR__ . "/Fixtures/delayed-process.php",
|
|
|
|
5,
|
|
|
|
]);
|
2019-08-27 19:17:41 +02:00
|
|
|
yield $context->start();
|
|
|
|
yield new Delayed(100);
|
|
|
|
$promise = $context->join();
|
|
|
|
$context->kill();
|
|
|
|
$this->assertFalse($context->isRunning());
|
|
|
|
yield $promise;
|
2017-03-09 23:15:30 +01:00
|
|
|
}
|
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
|
|
|
{
|
2019-08-27 19:17:41 +02:00
|
|
|
$this->expectException(ContextException::class);
|
|
|
|
$this->expectExceptionMessage('Failed to receive result');
|
|
|
|
|
|
|
|
$context = $this->createContext([
|
2019-02-18 06:50:41 +01:00
|
|
|
__DIR__ . "/Fixtures/sleep-process.php",
|
|
|
|
5,
|
|
|
|
]);
|
2019-08-27 19:17:41 +02:00
|
|
|
yield $context->start();
|
|
|
|
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
|
|
|
|
|
|
|
public function testExitingProcess()
|
|
|
|
{
|
2019-08-27 19:17:41 +02:00
|
|
|
$this->expectException(ContextException::class);
|
|
|
|
$this->expectExceptionMessage('Failed to receive result');
|
|
|
|
|
|
|
|
$context = $this->createContext([
|
2019-04-30 21:47:37 +02:00
|
|
|
__DIR__ . "/Fixtures/exiting-process.php",
|
|
|
|
5,
|
|
|
|
]);
|
2019-08-27 19:17:41 +02:00
|
|
|
yield $context->start();
|
|
|
|
yield $context->join();
|
2019-04-30 21:47:37 +02:00
|
|
|
}
|
2020-02-10 19:11:45 +01:00
|
|
|
|
|
|
|
public function testExitingProcessOnReceive()
|
|
|
|
{
|
|
|
|
$this->expectException(ContextException::class);
|
|
|
|
$this->expectExceptionMessage('stopped responding');
|
|
|
|
|
|
|
|
$context = $this->createContext(__DIR__ . "/Fixtures/exiting-process.php");
|
|
|
|
yield $context->start();
|
|
|
|
yield $context->receive();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testExitingProcessOnSend()
|
|
|
|
{
|
|
|
|
$this->expectException(ContextException::class);
|
|
|
|
$this->expectExceptionMessage('stopped responding');
|
|
|
|
|
|
|
|
$context = $this->createContext(__DIR__ . "/Fixtures/exiting-process.php");
|
|
|
|
yield $context->start();
|
2020-02-11 00:40:34 +01:00
|
|
|
yield new Delayed(500);
|
2020-02-10 19:11:45 +01:00
|
|
|
yield $context->send(1);
|
|
|
|
}
|
2015-09-26 06:41:15 +02:00
|
|
|
}
|