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
|
|
|
|
2017-03-16 23:03:59 +01:00
|
|
|
use Amp\Loop;
|
2017-12-27 05:38:22 +01:00
|
|
|
use Amp\Parallel\Sync\Channel;
|
2017-07-29 00:34:24 +02:00
|
|
|
use Amp\Parallel\Sync\ExitSuccess;
|
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
|
|
|
|
{
|
2016-08-19 00:36:58 +02:00
|
|
|
/**
|
|
|
|
* @param callable $function
|
|
|
|
*
|
2017-12-08 04:26:55 +01:00
|
|
|
* @return \Amp\Parallel\Context\Context
|
2016-08-19 00:36:58 +02:00
|
|
|
*/
|
2015-09-26 06:41:15 +02:00
|
|
|
abstract public function createContext(callable $function);
|
|
|
|
|
2018-10-07 16:50:45 +02:00
|
|
|
public function testIsRunning()
|
|
|
|
{
|
2017-03-16 23:03:59 +01:00
|
|
|
Loop::run(function () {
|
2015-09-26 06:41:15 +02:00
|
|
|
$context = $this->createContext(function () {
|
2018-10-07 16:50:45 +02:00
|
|
|
\usleep(100);
|
2015-09-26 06:41:15 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
$this->assertFalse($context->isRunning());
|
|
|
|
|
2018-10-06 17:05:49 +02:00
|
|
|
yield $context->start();
|
2015-09-26 06:41:15 +02:00
|
|
|
|
|
|
|
$this->assertTrue($context->isRunning());
|
|
|
|
|
2016-08-19 00:36:58 +02:00
|
|
|
yield $context->join();
|
2015-09-26 06:41:15 +02:00
|
|
|
|
|
|
|
$this->assertFalse($context->isRunning());
|
2017-03-16 23:03:59 +01:00
|
|
|
});
|
2015-09-26 06:41:15 +02:00
|
|
|
}
|
|
|
|
|
2018-10-07 16:50:45 +02:00
|
|
|
public function testKill()
|
|
|
|
{
|
2018-10-06 17:05:49 +02:00
|
|
|
Loop::run(function () {
|
|
|
|
$context = $this->createContext(function () {
|
2018-10-07 16:50:45 +02:00
|
|
|
\usleep(1e6);
|
2018-10-06 17:05:49 +02:00
|
|
|
});
|
2015-09-26 06:41:15 +02:00
|
|
|
|
2018-10-06 17:05:49 +02:00
|
|
|
yield $context->start();
|
2015-09-26 06:41:15 +02:00
|
|
|
|
2018-10-06 17:05:49 +02:00
|
|
|
$this->assertRunTimeLessThan([$context, 'kill'], 1000);
|
2015-09-26 06:41:15 +02:00
|
|
|
|
2018-10-06 17:05:49 +02:00
|
|
|
$this->assertFalse($context->isRunning());
|
|
|
|
});
|
2015-09-26 06:41:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-12-08 04:26:55 +01:00
|
|
|
* @expectedException \Amp\Parallel\Context\StatusError
|
2015-09-26 06:41:15 +02:00
|
|
|
*/
|
2018-10-07 16:50:45 +02:00
|
|
|
public function testStartWhileRunningThrowsError()
|
|
|
|
{
|
2018-10-06 17:05:49 +02:00
|
|
|
Loop::run(function () {
|
|
|
|
$context = $this->createContext(function () {
|
2018-10-07 16:50:45 +02:00
|
|
|
\usleep(100);
|
2018-10-06 17:05:49 +02:00
|
|
|
});
|
2015-09-26 06:41:15 +02:00
|
|
|
|
2018-10-06 17:05:49 +02:00
|
|
|
yield $context->start();
|
|
|
|
yield $context->start();
|
|
|
|
});
|
2015-09-26 06:41:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-12-08 04:26:55 +01:00
|
|
|
* @expectedException \Amp\Parallel\Context\StatusError
|
2015-09-26 06:41:15 +02:00
|
|
|
*/
|
2018-10-07 16:50:45 +02:00
|
|
|
public function testStartMultipleTimesThrowsError()
|
|
|
|
{
|
2015-09-26 06:41:15 +02:00
|
|
|
$this->assertRunTimeGreaterThan(function () {
|
2017-03-16 23:03:59 +01:00
|
|
|
Loop::run(function () {
|
2015-09-26 06:41:15 +02:00
|
|
|
$context = $this->createContext(function () {
|
2018-10-07 16:50:45 +02:00
|
|
|
\sleep(1);
|
2015-09-26 06:41:15 +02:00
|
|
|
});
|
|
|
|
|
2018-10-06 17:05:49 +02:00
|
|
|
yield $context->start();
|
2016-08-19 00:36:58 +02:00
|
|
|
yield $context->join();
|
2015-09-26 06:41:15 +02:00
|
|
|
|
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
|
|
|
});
|
2017-03-22 05:19:15 +01:00
|
|
|
}, 2000);
|
2015-09-26 06:41:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-12-08 04:26:55 +01:00
|
|
|
* @expectedException \Amp\Parallel\Sync\PanicError
|
2015-09-26 06:41:15 +02:00
|
|
|
*/
|
2018-10-07 16:50:45 +02:00
|
|
|
public function testExceptionInContextPanics()
|
|
|
|
{
|
2017-03-16 23:03:59 +01:00
|
|
|
Loop::run(function () {
|
2015-09-26 06:41:15 +02:00
|
|
|
$context = $this->createContext(function () {
|
|
|
|
throw new \Exception('Exception in fork.');
|
|
|
|
});
|
|
|
|
|
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
|
2015-12-12 07:34:41 +01:00
|
|
|
*/
|
2018-10-07 16:50:45 +02:00
|
|
|
public function testReturnUnserializableDataPanics()
|
|
|
|
{
|
2017-03-16 23:03:59 +01:00
|
|
|
Loop::run(function () {
|
2015-12-12 07:34:41 +01:00
|
|
|
$context = $this->createContext(function () {
|
2016-01-23 07:00:56 +01:00
|
|
|
return yield function () {};
|
2015-12-12 07:34:41 +01:00
|
|
|
});
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-10-07 16:50:45 +02:00
|
|
|
public function testJoinWaitsForChild()
|
|
|
|
{
|
2015-09-26 06:41:15 +02:00
|
|
|
$this->assertRunTimeGreaterThan(function () {
|
2017-03-16 23:03:59 +01:00
|
|
|
Loop::run(function () {
|
2015-09-26 06:41:15 +02:00
|
|
|
$context = $this->createContext(function () {
|
2018-10-07 16:50:45 +02:00
|
|
|
\sleep(1);
|
2015-09-26 06:41:15 +02:00
|
|
|
});
|
|
|
|
|
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
|
|
|
});
|
2017-03-22 05:19:15 +01:00
|
|
|
}, 1000);
|
2015-09-26 06:41:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-12-08 04:26:55 +01:00
|
|
|
* @expectedException \Amp\Parallel\Context\StatusError
|
2015-09-26 06:41:15 +02:00
|
|
|
*/
|
2018-10-07 16:50:45 +02:00
|
|
|
public function testJoinWithoutStartThrowsError()
|
|
|
|
{
|
2017-03-16 23:03:59 +01:00
|
|
|
Loop::run(function () {
|
2015-09-26 06:41:15 +02:00
|
|
|
$context = $this->createContext(function () {
|
2018-10-07 16:50:45 +02:00
|
|
|
\usleep(100);
|
2015-09-26 06:41:15 +02:00
|
|
|
});
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-10-07 16:50:45 +02:00
|
|
|
public function testJoinResolvesWithContextReturn()
|
|
|
|
{
|
2017-03-16 23:03:59 +01:00
|
|
|
Loop::run(function () {
|
2015-09-26 06:41:15 +02:00
|
|
|
$context = $this->createContext(function () {
|
|
|
|
return 42;
|
|
|
|
});
|
|
|
|
|
2018-10-06 17:05:49 +02:00
|
|
|
yield $context->start();
|
2016-08-19 00:36:58 +02:00
|
|
|
$this->assertSame(42, yield $context->join());
|
2017-03-16 23:03:59 +01:00
|
|
|
});
|
2015-09-26 06:41:15 +02:00
|
|
|
}
|
|
|
|
|
2018-10-07 16:50:45 +02:00
|
|
|
public function testSendAndReceive()
|
|
|
|
{
|
2017-03-16 23:03:59 +01:00
|
|
|
Loop::run(function () {
|
2017-12-27 05:38:22 +01:00
|
|
|
$context = $this->createContext(function (Channel $channel) {
|
|
|
|
yield $channel->send(1);
|
|
|
|
$value = yield $channel->receive();
|
2016-01-23 07:00:56 +01:00
|
|
|
return $value;
|
2015-09-26 06:41:15 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
$value = 42;
|
|
|
|
|
2018-10-06 17:05:49 +02:00
|
|
|
yield $context->start();
|
2016-08-19 00:36:58 +02:00
|
|
|
$this->assertSame(1, yield $context->receive());
|
|
|
|
yield $context->send($value);
|
|
|
|
$this->assertSame($value, yield $context->join());
|
2017-03-16 23:03:59 +01:00
|
|
|
});
|
2015-09-26 06:41:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testSendAndReceive
|
2017-12-08 04:26:55 +01:00
|
|
|
* @expectedException \Amp\Parallel\Sync\SynchronizationError
|
2015-09-26 06:41:15 +02:00
|
|
|
*/
|
2018-10-07 16:50:45 +02:00
|
|
|
public function testJoinWhenContextSendingData()
|
|
|
|
{
|
2017-03-16 23:03:59 +01:00
|
|
|
Loop::run(function () {
|
2017-12-27 05:38:22 +01:00
|
|
|
$context = $this->createContext(function (Channel $channel) {
|
|
|
|
yield $channel->send(0);
|
2016-01-23 07:00:56 +01:00
|
|
|
return 42;
|
2015-09-26 06:41:15 +02:00
|
|
|
});
|
|
|
|
|
2018-10-06 17:05:49 +02:00
|
|
|
yield $context->start();
|
2016-08-19 00:36:58 +02:00
|
|
|
$value = yield $context->join();
|
2017-03-16 23:03:59 +01:00
|
|
|
});
|
2015-09-26 06:41:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testSendAndReceive
|
2017-12-08 04:26:55 +01:00
|
|
|
* @expectedException \Amp\Parallel\Context\StatusError
|
2015-09-26 06:41:15 +02:00
|
|
|
*/
|
2018-10-07 16:50:45 +02:00
|
|
|
public function testReceiveBeforeContextHasStarted()
|
|
|
|
{
|
2017-03-16 23:03:59 +01:00
|
|
|
Loop::run(function () {
|
2017-12-27 05:38:22 +01:00
|
|
|
$context = $this->createContext(function (Channel $channel) {
|
|
|
|
yield $channel->send(0);
|
2016-01-23 07:00:56 +01:00
|
|
|
return 42;
|
2015-09-26 06:41:15 +02:00
|
|
|
});
|
|
|
|
|
2016-08-19 00:36:58 +02:00
|
|
|
$value = yield $context->receive();
|
2017-03-16 23:03:59 +01:00
|
|
|
});
|
2015-09-26 06:41:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testSendAndReceive
|
2017-12-08 04:26:55 +01:00
|
|
|
* @expectedException \Amp\Parallel\Context\StatusError
|
2015-09-26 06:41:15 +02:00
|
|
|
*/
|
2018-10-07 16:50:45 +02:00
|
|
|
public function testSendBeforeContextHasStarted()
|
|
|
|
{
|
2017-03-16 23:03:59 +01:00
|
|
|
Loop::run(function () {
|
2017-12-27 05:38:22 +01:00
|
|
|
$context = $this->createContext(function (Channel $channel) {
|
|
|
|
yield $channel->send(0);
|
2016-01-23 07:00:56 +01:00
|
|
|
return 42;
|
2015-09-26 06:41:15 +02:00
|
|
|
});
|
|
|
|
|
2016-08-19 00:36:58 +02:00
|
|
|
yield $context->send(0);
|
2017-03-16 23:03:59 +01:00
|
|
|
});
|
2015-09-26 06:41:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testSendAndReceive
|
2017-12-08 04:26:55 +01:00
|
|
|
* @expectedException \Amp\Parallel\Sync\SynchronizationError
|
2015-09-26 06:41:15 +02:00
|
|
|
*/
|
2018-10-07 16:50:45 +02:00
|
|
|
public function testReceiveWhenContextHasReturned()
|
|
|
|
{
|
2017-03-16 23:03:59 +01:00
|
|
|
Loop::run(function () {
|
2017-12-27 05:38:22 +01:00
|
|
|
$context = $this->createContext(function (Channel $channel) {
|
|
|
|
yield $channel->send(0);
|
2016-01-23 07:00:56 +01:00
|
|
|
return 42;
|
2015-09-26 06:41:15 +02:00
|
|
|
});
|
|
|
|
|
2018-10-06 17:05:49 +02:00
|
|
|
yield $context->start();
|
2016-08-19 00:36:58 +02:00
|
|
|
$value = yield $context->receive();
|
|
|
|
$value = yield $context->receive();
|
|
|
|
$value = yield $context->join();
|
2017-03-16 23:03:59 +01:00
|
|
|
});
|
2015-09-26 06:41:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testSendAndReceive
|
2016-08-19 00:36:58 +02:00
|
|
|
* @expectedException \Error
|
2015-09-26 06:41:15 +02:00
|
|
|
*/
|
2018-10-07 16:50:45 +02:00
|
|
|
public function testSendExitResult()
|
|
|
|
{
|
2017-03-16 23:03:59 +01:00
|
|
|
Loop::run(function () {
|
2017-12-27 05:38:22 +01:00
|
|
|
$context = $this->createContext(function (Channel $channel) {
|
|
|
|
$value = yield $channel->receive();
|
2016-01-23 07:00:56 +01:00
|
|
|
return 42;
|
2015-09-26 06:41:15 +02:00
|
|
|
});
|
|
|
|
|
2018-10-06 17:05:49 +02:00
|
|
|
yield $context->start();
|
2016-08-19 00:36:58 +02:00
|
|
|
yield $context->send(new ExitSuccess(0));
|
|
|
|
$value = 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
|
2017-03-09 23:15:30 +01:00
|
|
|
* @expectedExceptionMessage The context stopped responding
|
|
|
|
*/
|
2018-10-07 16:50:45 +02:00
|
|
|
public function testExitingContextOnJoin()
|
|
|
|
{
|
2017-03-16 23:03:59 +01:00
|
|
|
Loop::run(function () {
|
2017-03-09 23:15:30 +01:00
|
|
|
$context = $this->createContext(function () {
|
|
|
|
exit;
|
|
|
|
});
|
|
|
|
|
2018-10-06 17:05:49 +02:00
|
|
|
yield $context->start();
|
2017-03-09 23:15:30 +01:00
|
|
|
$value = yield $context->join();
|
2017-03-16 23:03:59 +01:00
|
|
|
});
|
2017-03-09 23:15:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-12-27 05:38:22 +01:00
|
|
|
* @expectedException \Amp\Parallel\Sync\ChannelException
|
|
|
|
* @expectedExceptionMessage The channel closed unexpectedly
|
2017-03-09 23:15:30 +01:00
|
|
|
*/
|
2018-10-07 16:50:45 +02:00
|
|
|
public function testExitingContextOnReceive()
|
|
|
|
{
|
2017-03-16 23:03:59 +01:00
|
|
|
Loop::run(function () {
|
2017-03-09 23:15:30 +01:00
|
|
|
$context = $this->createContext(function () {
|
|
|
|
exit;
|
|
|
|
});
|
|
|
|
|
2018-10-06 17:05:49 +02:00
|
|
|
yield $context->start();
|
2017-03-09 23:15:30 +01:00
|
|
|
$value = yield $context->receive();
|
2017-03-16 23:03:59 +01:00
|
|
|
});
|
2017-03-09 23:15:30 +01:00
|
|
|
}
|
2017-05-28 07:09:13 +02:00
|
|
|
|
|
|
|
/**
|
2017-12-27 05:38:22 +01:00
|
|
|
* @expectedException \Amp\Parallel\Sync\ChannelException
|
|
|
|
* @expectedExceptionMessage Sending on the channel failed
|
2017-05-28 07:09:13 +02:00
|
|
|
*/
|
2018-10-07 16:50:45 +02:00
|
|
|
public function testExitingContextOnSend()
|
|
|
|
{
|
2017-05-28 07:09:13 +02:00
|
|
|
Loop::run(function () {
|
|
|
|
$context = $this->createContext(function () {
|
|
|
|
exit;
|
|
|
|
});
|
|
|
|
|
2018-10-22 16:40:19 +02:00
|
|
|
$context->start();
|
2017-06-19 18:14:38 +02:00
|
|
|
yield $context->send(\str_pad("", 1024 * 1024, "-"));
|
2017-05-28 07:09:13 +02:00
|
|
|
});
|
|
|
|
}
|
2015-09-26 06:41:15 +02:00
|
|
|
}
|