1
0
mirror of https://github.com/danog/parallel.git synced 2024-12-11 16:49:51 +01:00
parallel/test/Context/AbstractContextTest.php

286 lines
7.4 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\Loop;
use Amp\Parallel\Sync\ExitSuccess;
use Amp\PHPUnit\TestCase;
2016-08-19 00:36:58 +02:00
abstract class AbstractContextTest extends TestCase {
/**
* @param callable $function
*
2017-12-08 04:26:55 +01:00
* @return \Amp\Parallel\Context\Context
2016-08-19 00:36:58 +02:00
*/
abstract public function createContext(callable $function);
2016-08-19 00:36:58 +02:00
public function testIsRunning() {
Loop::run(function () {
$context = $this->createContext(function () {
usleep(100);
});
$this->assertFalse($context->isRunning());
$context->start();
$this->assertTrue($context->isRunning());
2016-08-19 00:36:58 +02:00
yield $context->join();
$this->assertFalse($context->isRunning());
});
}
2016-08-19 00:36:58 +02:00
public function testKill() {
$context = $this->createContext(function () {
usleep(1e6);
});
$context->start();
2017-04-24 17:47:53 +02:00
$this->assertRunTimeLessThan([$context, 'kill'], 1000);
$this->assertFalse($context->isRunning());
}
/**
2017-12-08 04:26:55 +01:00
* @expectedException \Amp\Parallel\Context\StatusError
*/
2016-08-19 00:36:58 +02:00
public function testStartWhileRunningThrowsError() {
$context = $this->createContext(function () {
usleep(100);
});
$context->start();
$context->start();
}
/**
2017-12-08 04:26:55 +01:00
* @expectedException \Amp\Parallel\Context\StatusError
*/
2016-08-19 00:36:58 +02:00
public function testStartMultipleTimesThrowsError() {
$this->assertRunTimeGreaterThan(function () {
Loop::run(function () {
$context = $this->createContext(function () {
sleep(1);
});
$context->start();
2016-08-19 00:36:58 +02:00
yield $context->join();
$context->start();
2016-08-19 00:36:58 +02:00
yield $context->join();
});
}, 2000);
}
/**
2017-12-08 04:26:55 +01:00
* @expectedException \Amp\Parallel\Sync\PanicError
*/
2016-08-19 00:36:58 +02:00
public function testExceptionInContextPanics() {
Loop::run(function () {
$context = $this->createContext(function () {
throw new \Exception('Exception in fork.');
});
$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
2015-12-12 07:34:41 +01:00
*/
2016-08-19 00:36:58 +02:00
public function testReturnUnserializableDataPanics() {
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
});
$context->start();
2016-08-19 00:36:58 +02:00
yield $context->join();
});
2015-12-12 07:34:41 +01:00
}
2016-08-19 00:36:58 +02:00
public function testJoinWaitsForChild() {
$this->assertRunTimeGreaterThan(function () {
Loop::run(function () {
$context = $this->createContext(function () {
sleep(1);
});
$context->start();
2016-08-19 00:36:58 +02:00
yield $context->join();
});
}, 1000);
}
/**
2017-12-08 04:26:55 +01:00
* @expectedException \Amp\Parallel\Context\StatusError
*/
2016-08-19 00:36:58 +02:00
public function testJoinWithoutStartThrowsError() {
Loop::run(function () {
$context = $this->createContext(function () {
usleep(100);
});
2016-08-19 00:36:58 +02:00
yield $context->join();
});
}
2016-08-19 00:36:58 +02:00
public function testJoinResolvesWithContextReturn() {
Loop::run(function () {
$context = $this->createContext(function () {
return 42;
});
$context->start();
2016-08-19 00:36:58 +02:00
$this->assertSame(42, yield $context->join());
});
}
2016-08-19 00:36:58 +02:00
public function testSendAndReceive() {
Loop::run(function () {
$context = $this->createContext(function () {
2016-08-19 00:36:58 +02:00
yield $this->send(1);
$value = yield $this->receive();
2016-01-23 07:00:56 +01:00
return $value;
});
$value = 42;
$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());
});
}
/**
* @depends testSendAndReceive
2017-12-08 04:26:55 +01:00
* @expectedException \Amp\Parallel\Sync\SynchronizationError
*/
2016-08-19 00:36:58 +02:00
public function testJoinWhenContextSendingData() {
Loop::run(function () {
$context = $this->createContext(function () {
2016-08-19 00:36:58 +02:00
yield $this->send(0);
2016-01-23 07:00:56 +01:00
return 42;
});
$context->start();
2016-08-19 00:36:58 +02:00
$value = yield $context->join();
});
}
/**
* @depends testSendAndReceive
2017-12-08 04:26:55 +01:00
* @expectedException \Amp\Parallel\Context\StatusError
*/
2016-08-19 00:36:58 +02:00
public function testReceiveBeforeContextHasStarted() {
Loop::run(function () {
$context = $this->createContext(function () {
2016-08-19 00:36:58 +02:00
yield $this->send(0);
2016-01-23 07:00:56 +01:00
return 42;
});
2016-08-19 00:36:58 +02:00
$value = yield $context->receive();
});
}
/**
* @depends testSendAndReceive
2017-12-08 04:26:55 +01:00
* @expectedException \Amp\Parallel\Context\StatusError
*/
2016-08-19 00:36:58 +02:00
public function testSendBeforeContextHasStarted() {
Loop::run(function () {
$context = $this->createContext(function () {
2016-08-19 00:36:58 +02:00
yield $this->send(0);
2016-01-23 07:00:56 +01:00
return 42;
});
2016-08-19 00:36:58 +02:00
yield $context->send(0);
});
}
/**
* @depends testSendAndReceive
2017-12-08 04:26:55 +01:00
* @expectedException \Amp\Parallel\Sync\SynchronizationError
*/
2016-08-19 00:36:58 +02:00
public function testReceiveWhenContextHasReturned() {
Loop::run(function () {
$context = $this->createContext(function () {
2016-08-19 00:36:58 +02:00
yield $this->send(0);
2016-01-23 07:00:56 +01:00
return 42;
});
$context->start();
2016-08-19 00:36:58 +02:00
$value = yield $context->receive();
$value = yield $context->receive();
$value = yield $context->join();
});
}
/**
* @depends testSendAndReceive
2016-08-19 00:36:58 +02:00
* @expectedException \Error
*/
2017-01-17 06:24:59 +01:00
public function testSendExitResult() {
Loop::run(function () {
$context = $this->createContext(function () {
2016-08-19 00:36:58 +02:00
$value = yield $this->receive();
2016-01-23 07:00:56 +01:00
return 42;
});
$context->start();
2016-08-19 00:36:58 +02:00
yield $context->send(new ExitSuccess(0));
$value = yield $context->join();
});
}
/**
2017-12-08 04:26:55 +01:00
* @expectedException \Amp\Parallel\Context\ContextException
* @expectedExceptionMessage The context stopped responding
*/
public function testExitingContextOnJoin() {
Loop::run(function () {
$context = $this->createContext(function () {
exit;
});
$context->start();
$value = yield $context->join();
});
}
/**
2017-12-08 04:26:55 +01:00
* @expectedException \Amp\Parallel\Context\ContextException
* @expectedExceptionMessage The context stopped responding
*/
public function testExitingContextOnReceive() {
Loop::run(function () {
$context = $this->createContext(function () {
exit;
});
$context->start();
$value = yield $context->receive();
});
}
2017-05-28 07:09:13 +02:00
/**
2017-12-08 04:26:55 +01:00
* @expectedException \Amp\Parallel\Context\ContextException
2017-05-28 07:09:13 +02:00
* @expectedExceptionMessage The context went away
*/
public function testExitingContextOnSend() {
Loop::run(function () {
$context = $this->createContext(function () {
exit;
});
$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
});
}
}