1
0
mirror of https://github.com/danog/parallel.git synced 2024-12-02 17:52:14 +01:00
parallel/test/Context/AbstractContextTest.php

147 lines
4.2 KiB
PHP
Raw Normal View History

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