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

164 lines
5.0 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\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;
2019-08-27 19:17:41 +02:00
abstract class AbstractContextTest extends AsyncTestCase
2018-10-07 16:50:45 +02:00
{
abstract public function createContext($script): Context;
public function testBasicProcess()
2018-10-07 16:50:45 +02:00
{
2019-08-27 19:17:41 +02:00
$context = $this->createContext([
__DIR__ . "/Fixtures/test-process.php",
"Test"
]);
2019-08-27 19:17:41 +02:00
yield $context->start();
$this->assertSame("Test", yield $context->join());
}
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();
}
public function testThrowingProcessOnReceive()
{
2020-02-11 18:06:30 +01:00
$this->expectException(ContextPanicError::class);
$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);
$this->expectExceptionMessage('Test message');
$context = $this->createContext(__DIR__ . "/Fixtures/throwing-process.php");
yield $context->start();
yield new Delayed(100);
yield $context->send(1);
}
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
}
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());
}
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());
}
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();
}
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([
__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-05-28 07:09:13 +02: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([
__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
}
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();
yield new Delayed(500);
yield $context->send(1);
}
}