2016-12-30 02:16:04 +01:00
|
|
|
<?php
|
2015-09-26 06:41:15 +02:00
|
|
|
|
2016-08-23 23:47:40 +02:00
|
|
|
namespace Amp\Parallel\Test;
|
2016-08-18 18:04:48 +02:00
|
|
|
|
2016-08-23 23:47:40 +02:00
|
|
|
use Amp\Parallel\Sync\Internal\ExitSuccess;
|
2015-09-26 06:41:15 +02:00
|
|
|
|
2016-08-19 00:36:58 +02:00
|
|
|
abstract class AbstractContextTest extends TestCase {
|
|
|
|
/**
|
|
|
|
* @param callable $function
|
|
|
|
*
|
2016-08-23 23:47:40 +02:00
|
|
|
* @return \Amp\Parallel\Context
|
2016-08-19 00:36:58 +02:00
|
|
|
*/
|
2015-09-26 06:41:15 +02:00
|
|
|
abstract public function createContext(callable $function);
|
|
|
|
|
2016-08-19 00:36:58 +02:00
|
|
|
public function testIsRunning() {
|
|
|
|
\Amp\execute(function () {
|
2015-09-26 06:41:15 +02:00
|
|
|
$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();
|
2015-09-26 06:41:15 +02:00
|
|
|
|
|
|
|
$this->assertFalse($context->isRunning());
|
2016-08-19 00:36:58 +02:00
|
|
|
});
|
2015-09-26 06:41:15 +02:00
|
|
|
}
|
|
|
|
|
2016-08-19 00:36:58 +02:00
|
|
|
public function testKill() {
|
2015-09-26 06:41:15 +02:00
|
|
|
$context = $this->createContext(function () {
|
|
|
|
usleep(1e6);
|
|
|
|
});
|
|
|
|
|
|
|
|
$context->start();
|
|
|
|
|
|
|
|
$this->assertRunTimeLessThan([$context, 'kill'], 0.1);
|
|
|
|
|
|
|
|
$this->assertFalse($context->isRunning());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-08-23 23:47:40 +02:00
|
|
|
* @expectedException \Amp\Parallel\StatusError
|
2015-09-26 06:41:15 +02:00
|
|
|
*/
|
2016-08-19 00:36:58 +02:00
|
|
|
public function testStartWhileRunningThrowsError() {
|
2015-09-26 06:41:15 +02:00
|
|
|
$context = $this->createContext(function () {
|
|
|
|
usleep(100);
|
|
|
|
});
|
|
|
|
|
|
|
|
$context->start();
|
|
|
|
$context->start();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-08-23 23:47:40 +02:00
|
|
|
* @expectedException \Amp\Parallel\StatusError
|
2015-09-26 06:41:15 +02:00
|
|
|
*/
|
2016-08-19 00:36:58 +02:00
|
|
|
public function testStartMultipleTimesThrowsError() {
|
2015-09-26 06:41:15 +02:00
|
|
|
$this->assertRunTimeGreaterThan(function () {
|
2016-08-19 00:36:58 +02:00
|
|
|
\Amp\execute(function () {
|
2015-09-26 06:41:15 +02:00
|
|
|
$context = $this->createContext(function () {
|
|
|
|
sleep(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
$context->start();
|
2016-08-19 00:36:58 +02:00
|
|
|
yield $context->join();
|
2015-09-26 06:41:15 +02:00
|
|
|
|
|
|
|
$context->start();
|
2016-08-19 00:36:58 +02:00
|
|
|
yield $context->join();
|
|
|
|
});
|
2015-09-26 06:41:15 +02:00
|
|
|
}, 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-08-23 23:47:40 +02:00
|
|
|
* @expectedException \Amp\Parallel\PanicError
|
2015-09-26 06:41:15 +02:00
|
|
|
*/
|
2016-08-19 00:36:58 +02:00
|
|
|
public function testExceptionInContextPanics() {
|
|
|
|
\Amp\execute(function () {
|
2015-09-26 06:41:15 +02:00
|
|
|
$context = $this->createContext(function () {
|
|
|
|
throw new \Exception('Exception in fork.');
|
|
|
|
});
|
|
|
|
|
|
|
|
$context->start();
|
2016-08-19 00:36:58 +02:00
|
|
|
yield $context->join();
|
|
|
|
});
|
2015-09-26 06:41:15 +02:00
|
|
|
}
|
|
|
|
|
2015-12-12 07:34:41 +01:00
|
|
|
/**
|
2016-08-23 23:47:40 +02:00
|
|
|
* @expectedException \Amp\Parallel\PanicError
|
2015-12-12 07:34:41 +01:00
|
|
|
*/
|
2016-08-19 00:36:58 +02:00
|
|
|
public function testReturnUnserializableDataPanics() {
|
|
|
|
\Amp\execute(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() {
|
2015-09-26 06:41:15 +02:00
|
|
|
$this->assertRunTimeGreaterThan(function () {
|
2016-08-19 00:36:58 +02:00
|
|
|
\Amp\execute(function () {
|
2015-09-26 06:41:15 +02:00
|
|
|
$context = $this->createContext(function () {
|
|
|
|
sleep(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
$context->start();
|
2016-08-19 00:36:58 +02:00
|
|
|
yield $context->join();
|
|
|
|
});
|
2015-09-26 06:41:15 +02:00
|
|
|
|
|
|
|
}, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-08-23 23:47:40 +02:00
|
|
|
* @expectedException \Amp\Parallel\StatusError
|
2015-09-26 06:41:15 +02:00
|
|
|
*/
|
2016-08-19 00:36:58 +02:00
|
|
|
public function testJoinWithoutStartThrowsError() {
|
|
|
|
\Amp\execute(function () {
|
2015-09-26 06:41:15 +02:00
|
|
|
$context = $this->createContext(function () {
|
|
|
|
usleep(100);
|
|
|
|
});
|
|
|
|
|
2016-08-19 00:36:58 +02:00
|
|
|
yield $context->join();
|
|
|
|
});
|
2015-09-26 06:41:15 +02:00
|
|
|
}
|
|
|
|
|
2016-08-19 00:36:58 +02:00
|
|
|
public function testJoinResolvesWithContextReturn() {
|
|
|
|
\Amp\execute(function () {
|
2015-09-26 06:41:15 +02:00
|
|
|
$context = $this->createContext(function () {
|
|
|
|
return 42;
|
|
|
|
});
|
|
|
|
|
|
|
|
$context->start();
|
2016-08-19 00:36:58 +02:00
|
|
|
$this->assertSame(42, yield $context->join());
|
|
|
|
});
|
2015-09-26 06:41:15 +02:00
|
|
|
}
|
|
|
|
|
2016-08-19 00:36:58 +02:00
|
|
|
public function testSendAndReceive() {
|
|
|
|
\Amp\execute(function () {
|
2015-09-26 06:41:15 +02:00
|
|
|
$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;
|
2015-09-26 06:41:15 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
$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());
|
|
|
|
});
|
2015-09-26 06:41:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testSendAndReceive
|
2016-08-23 23:47:40 +02:00
|
|
|
* @expectedException \Amp\Parallel\SynchronizationError
|
2015-09-26 06:41:15 +02:00
|
|
|
*/
|
2016-08-19 00:36:58 +02:00
|
|
|
public function testJoinWhenContextSendingData() {
|
|
|
|
\Amp\execute(function () {
|
2015-09-26 06:41:15 +02:00
|
|
|
$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;
|
2015-09-26 06:41:15 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
$context->start();
|
2016-08-19 00:36:58 +02:00
|
|
|
$value = yield $context->join();
|
|
|
|
});
|
2015-09-26 06:41:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testSendAndReceive
|
2016-08-23 23:47:40 +02:00
|
|
|
* @expectedException \Amp\Parallel\StatusError
|
2015-09-26 06:41:15 +02:00
|
|
|
*/
|
2016-08-19 00:36:58 +02:00
|
|
|
public function testReceiveBeforeContextHasStarted() {
|
|
|
|
\Amp\execute(function () {
|
2015-09-26 06:41:15 +02:00
|
|
|
$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;
|
2015-09-26 06:41:15 +02:00
|
|
|
});
|
|
|
|
|
2016-08-19 00:36:58 +02:00
|
|
|
$value = yield $context->receive();
|
|
|
|
});
|
2015-09-26 06:41:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testSendAndReceive
|
2016-08-23 23:47:40 +02:00
|
|
|
* @expectedException \Amp\Parallel\StatusError
|
2015-09-26 06:41:15 +02:00
|
|
|
*/
|
2016-08-19 00:36:58 +02:00
|
|
|
public function testSendBeforeContextHasStarted() {
|
|
|
|
\Amp\execute(function () {
|
2015-09-26 06:41:15 +02:00
|
|
|
$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;
|
2015-09-26 06:41:15 +02:00
|
|
|
});
|
|
|
|
|
2016-08-19 00:36:58 +02:00
|
|
|
yield $context->send(0);
|
|
|
|
});
|
2015-09-26 06:41:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testSendAndReceive
|
2016-08-23 23:47:40 +02:00
|
|
|
* @expectedException \Amp\Parallel\SynchronizationError
|
2015-09-26 06:41:15 +02:00
|
|
|
*/
|
2016-08-19 00:36:58 +02:00
|
|
|
public function testReceiveWhenContextHasReturned() {
|
|
|
|
\Amp\execute(function () {
|
2015-09-26 06:41:15 +02:00
|
|
|
$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;
|
2015-09-26 06:41:15 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
$context->start();
|
2016-08-19 00:36:58 +02:00
|
|
|
$value = yield $context->receive();
|
|
|
|
$value = yield $context->receive();
|
|
|
|
$value = yield $context->join();
|
|
|
|
});
|
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
|
|
|
*/
|
2016-08-19 00:36:58 +02:00
|
|
|
public function testSendExitStatus() {
|
|
|
|
\Amp\execute(function () {
|
2015-09-26 06:41:15 +02:00
|
|
|
$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;
|
2015-09-26 06:41:15 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
$context->start();
|
2016-08-19 00:36:58 +02:00
|
|
|
yield $context->send(new ExitSuccess(0));
|
|
|
|
$value = yield $context->join();
|
|
|
|
});
|
2015-09-26 06:41:15 +02:00
|
|
|
}
|
|
|
|
}
|