2015-09-26 06:41:15 +02:00
|
|
|
<?php
|
|
|
|
|
2016-08-18 18:04:48 +02:00
|
|
|
namespace Amp\Tests\Concurrent;
|
|
|
|
|
|
|
|
use Amp\Concurrent\Sync\Internal\ExitSuccess;
|
|
|
|
use Amp\Coroutine;
|
|
|
|
use Amp\Loop;
|
2015-09-26 06:41:15 +02:00
|
|
|
|
|
|
|
abstract class AbstractContextTest extends TestCase
|
|
|
|
{
|
|
|
|
abstract public function createContext(callable $function);
|
|
|
|
|
|
|
|
public function testIsRunning()
|
|
|
|
{
|
|
|
|
Coroutine\create(function () {
|
|
|
|
$context = $this->createContext(function () {
|
|
|
|
usleep(100);
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->assertFalse($context->isRunning());
|
|
|
|
|
|
|
|
$context->start();
|
|
|
|
|
|
|
|
$this->assertTrue($context->isRunning());
|
|
|
|
|
2016-01-23 07:00:56 +01:00
|
|
|
yield from $context->join();
|
2015-09-26 06:41:15 +02:00
|
|
|
|
|
|
|
$this->assertFalse($context->isRunning());
|
|
|
|
})->done();
|
|
|
|
|
|
|
|
Loop\run();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testKill()
|
|
|
|
{
|
|
|
|
$context = $this->createContext(function () {
|
|
|
|
usleep(1e6);
|
|
|
|
});
|
|
|
|
|
|
|
|
$context->start();
|
|
|
|
|
|
|
|
$this->assertRunTimeLessThan([$context, 'kill'], 0.1);
|
|
|
|
|
|
|
|
$this->assertFalse($context->isRunning());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-08-18 18:04:48 +02:00
|
|
|
* @expectedException \Amp\Concurrent\Exception\StatusError
|
2015-09-26 06:41:15 +02:00
|
|
|
*/
|
|
|
|
public function testStartWhileRunningThrowsError()
|
|
|
|
{
|
|
|
|
$context = $this->createContext(function () {
|
|
|
|
usleep(100);
|
|
|
|
});
|
|
|
|
|
|
|
|
$context->start();
|
|
|
|
$context->start();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-08-18 18:04:48 +02:00
|
|
|
* @expectedException \Amp\Concurrent\Exception\StatusError
|
2015-09-26 06:41:15 +02:00
|
|
|
*/
|
|
|
|
public function testStartMultipleTimesThrowsError()
|
|
|
|
{
|
|
|
|
Loop\loop();
|
|
|
|
|
|
|
|
$this->assertRunTimeGreaterThan(function () {
|
|
|
|
Coroutine\create(function () {
|
|
|
|
$context = $this->createContext(function () {
|
|
|
|
sleep(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
$context->start();
|
2016-01-23 07:00:56 +01:00
|
|
|
yield from $context->join();
|
2015-09-26 06:41:15 +02:00
|
|
|
|
|
|
|
$context->start();
|
2016-01-23 07:00:56 +01:00
|
|
|
yield from $context->join();
|
2015-09-26 06:41:15 +02:00
|
|
|
})->done();
|
|
|
|
|
|
|
|
Loop\run();
|
|
|
|
}, 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-08-18 18:04:48 +02:00
|
|
|
* @expectedException \Amp\Concurrent\Exception\PanicError
|
2015-09-26 06:41:15 +02:00
|
|
|
*/
|
|
|
|
public function testExceptionInContextPanics()
|
|
|
|
{
|
|
|
|
Coroutine\create(function () {
|
|
|
|
$context = $this->createContext(function () {
|
|
|
|
throw new \Exception('Exception in fork.');
|
|
|
|
});
|
|
|
|
|
|
|
|
$context->start();
|
2016-01-23 07:00:56 +01:00
|
|
|
yield from $context->join();
|
2015-09-26 06:41:15 +02:00
|
|
|
})->done();
|
|
|
|
|
|
|
|
Loop\run();
|
|
|
|
}
|
|
|
|
|
2015-12-12 07:34:41 +01:00
|
|
|
/**
|
2016-08-18 18:04:48 +02:00
|
|
|
* @expectedException \Amp\Concurrent\Exception\PanicError
|
2015-12-12 07:34:41 +01:00
|
|
|
*/
|
|
|
|
public function testReturnUnserializableDataPanics()
|
|
|
|
{
|
|
|
|
Coroutine\create(function () {
|
|
|
|
$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-01-23 07:00:56 +01:00
|
|
|
yield from $context->join();
|
2015-12-12 07:34:41 +01:00
|
|
|
})->done();
|
|
|
|
|
|
|
|
Loop\run();
|
|
|
|
}
|
|
|
|
|
2015-09-26 06:41:15 +02:00
|
|
|
public function testJoinWaitsForChild()
|
|
|
|
{
|
|
|
|
Loop\loop();
|
|
|
|
|
|
|
|
$this->assertRunTimeGreaterThan(function () {
|
|
|
|
Coroutine\create(function () {
|
|
|
|
$context = $this->createContext(function () {
|
|
|
|
sleep(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
$context->start();
|
2016-01-23 07:00:56 +01:00
|
|
|
yield from $context->join();
|
2015-09-26 06:41:15 +02:00
|
|
|
})->done();
|
|
|
|
|
|
|
|
Loop\run();
|
|
|
|
}, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-08-18 18:04:48 +02:00
|
|
|
* @expectedException \Amp\Concurrent\Exception\StatusError
|
2015-09-26 06:41:15 +02:00
|
|
|
*/
|
|
|
|
public function testJoinWithoutStartThrowsError()
|
|
|
|
{
|
|
|
|
Coroutine\create(function () {
|
|
|
|
$context = $this->createContext(function () {
|
|
|
|
usleep(100);
|
|
|
|
});
|
|
|
|
|
2016-01-23 07:00:56 +01:00
|
|
|
yield from $context->join();
|
2015-09-26 06:41:15 +02:00
|
|
|
})->done();
|
|
|
|
|
|
|
|
Loop\run();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testJoinResolvesWithContextReturn()
|
|
|
|
{
|
|
|
|
Coroutine\create(function () {
|
|
|
|
$context = $this->createContext(function () {
|
|
|
|
return 42;
|
|
|
|
});
|
|
|
|
|
|
|
|
$context->start();
|
2016-01-23 07:00:56 +01:00
|
|
|
$this->assertSame(42, yield from $context->join());
|
2015-09-26 06:41:15 +02:00
|
|
|
})->done();
|
|
|
|
|
|
|
|
Loop\run();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSendAndReceive()
|
|
|
|
{
|
|
|
|
Coroutine\create(function () {
|
|
|
|
$context = $this->createContext(function () {
|
2016-01-23 07:00:56 +01:00
|
|
|
yield from $this->send(1);
|
|
|
|
$value = yield from $this->receive();
|
|
|
|
return $value;
|
2015-09-26 06:41:15 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
$value = 42;
|
|
|
|
|
|
|
|
$context->start();
|
2016-01-23 07:00:56 +01:00
|
|
|
$this->assertSame(1, yield from $context->receive());
|
2016-01-25 06:03:45 +01:00
|
|
|
yield from $context->send($value);
|
2016-01-23 07:00:56 +01:00
|
|
|
$this->assertSame($value, yield from $context->join());
|
2015-09-26 06:41:15 +02:00
|
|
|
})->done();
|
|
|
|
|
|
|
|
Loop\run();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testSendAndReceive
|
2016-08-18 18:04:48 +02:00
|
|
|
* @expectedException \Amp\Concurrent\Exception\SynchronizationError
|
2015-09-26 06:41:15 +02:00
|
|
|
*/
|
|
|
|
public function testJoinWhenContextSendingData()
|
|
|
|
{
|
|
|
|
Coroutine\create(function () {
|
|
|
|
$context = $this->createContext(function () {
|
2016-01-23 07:00:56 +01:00
|
|
|
yield from $this->send(0);
|
|
|
|
return 42;
|
2015-09-26 06:41:15 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
$context->start();
|
2016-01-23 07:00:56 +01:00
|
|
|
$value = yield from $context->join();
|
2015-09-26 06:41:15 +02:00
|
|
|
})->done();
|
|
|
|
|
|
|
|
Loop\run();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testSendAndReceive
|
2016-08-18 18:04:48 +02:00
|
|
|
* @expectedException \Amp\Concurrent\Exception\StatusError
|
2015-09-26 06:41:15 +02:00
|
|
|
*/
|
|
|
|
public function testReceiveBeforeContextHasStarted()
|
|
|
|
{
|
|
|
|
Coroutine\create(function () {
|
|
|
|
$context = $this->createContext(function () {
|
2016-01-23 07:00:56 +01:00
|
|
|
yield from $this->send(0);
|
|
|
|
return 42;
|
2015-09-26 06:41:15 +02:00
|
|
|
});
|
|
|
|
|
2016-01-23 07:00:56 +01:00
|
|
|
$value = yield from $context->receive();
|
2015-09-26 06:41:15 +02:00
|
|
|
})->done();
|
|
|
|
|
|
|
|
Loop\run();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testSendAndReceive
|
2016-08-18 18:04:48 +02:00
|
|
|
* @expectedException \Amp\Concurrent\Exception\StatusError
|
2015-09-26 06:41:15 +02:00
|
|
|
*/
|
|
|
|
public function testSendBeforeContextHasStarted()
|
|
|
|
{
|
|
|
|
Coroutine\create(function () {
|
|
|
|
$context = $this->createContext(function () {
|
2016-01-23 07:00:56 +01:00
|
|
|
yield from $this->send(0);
|
|
|
|
return 42;
|
2015-09-26 06:41:15 +02:00
|
|
|
});
|
|
|
|
|
2016-01-23 07:00:56 +01:00
|
|
|
yield from $context->send(0);
|
2015-09-26 06:41:15 +02:00
|
|
|
})->done();
|
|
|
|
|
|
|
|
Loop\run();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testSendAndReceive
|
2016-08-18 18:04:48 +02:00
|
|
|
* @expectedException \Amp\Concurrent\Exception\SynchronizationError
|
2015-09-26 06:41:15 +02:00
|
|
|
*/
|
|
|
|
public function testReceiveWhenContextHasReturned()
|
|
|
|
{
|
|
|
|
Coroutine\create(function () {
|
|
|
|
$context = $this->createContext(function () {
|
2016-01-23 07:00:56 +01:00
|
|
|
yield from $this->send(0);
|
|
|
|
return 42;
|
2015-09-26 06:41:15 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
$context->start();
|
2016-01-23 07:00:56 +01:00
|
|
|
$value = yield from $context->receive();
|
|
|
|
$value = yield from $context->receive();
|
|
|
|
$value = yield from $context->join();
|
2015-09-26 06:41:15 +02:00
|
|
|
})->done();
|
|
|
|
|
|
|
|
Loop\run();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testSendAndReceive
|
2016-08-18 18:04:48 +02:00
|
|
|
* @expectedException \Amp\Exception\InvalidArgumentError
|
2015-09-26 06:41:15 +02:00
|
|
|
*/
|
|
|
|
public function testSendExitStatus()
|
|
|
|
{
|
|
|
|
Coroutine\create(function () {
|
|
|
|
$context = $this->createContext(function () {
|
2016-01-23 07:00:56 +01:00
|
|
|
$value = yield from $this->receive();
|
|
|
|
return 42;
|
2015-09-26 06:41:15 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
$context->start();
|
2016-01-23 07:00:56 +01:00
|
|
|
yield from $context->send(new ExitSuccess(0));
|
|
|
|
$value = yield from $context->join();
|
2015-09-26 06:41:15 +02:00
|
|
|
})->done();
|
|
|
|
|
|
|
|
Loop\run();
|
|
|
|
}
|
|
|
|
}
|