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

282 lines
6.9 KiB
PHP
Raw Normal View History

<?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;
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();
$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
*/
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
*/
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();
$context->start();
2016-01-23 07:00:56 +01:00
yield from $context->join();
})->done();
Loop\run();
}, 2);
}
/**
2016-08-18 18:04:48 +02:00
* @expectedException \Amp\Concurrent\Exception\PanicError
*/
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();
})->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();
}
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();
})->done();
Loop\run();
}, 1);
}
/**
2016-08-18 18:04:48 +02:00
* @expectedException \Amp\Concurrent\Exception\StatusError
*/
public function testJoinWithoutStartThrowsError()
{
Coroutine\create(function () {
$context = $this->createContext(function () {
usleep(100);
});
2016-01-23 07:00:56 +01:00
yield from $context->join();
})->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());
})->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;
});
$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());
})->done();
Loop\run();
}
/**
* @depends testSendAndReceive
2016-08-18 18:04:48 +02:00
* @expectedException \Amp\Concurrent\Exception\SynchronizationError
*/
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;
});
$context->start();
2016-01-23 07:00:56 +01:00
$value = yield from $context->join();
})->done();
Loop\run();
}
/**
* @depends testSendAndReceive
2016-08-18 18:04:48 +02:00
* @expectedException \Amp\Concurrent\Exception\StatusError
*/
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;
});
2016-01-23 07:00:56 +01:00
$value = yield from $context->receive();
})->done();
Loop\run();
}
/**
* @depends testSendAndReceive
2016-08-18 18:04:48 +02:00
* @expectedException \Amp\Concurrent\Exception\StatusError
*/
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;
});
2016-01-23 07:00:56 +01:00
yield from $context->send(0);
})->done();
Loop\run();
}
/**
* @depends testSendAndReceive
2016-08-18 18:04:48 +02:00
* @expectedException \Amp\Concurrent\Exception\SynchronizationError
*/
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;
});
$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();
})->done();
Loop\run();
}
/**
* @depends testSendAndReceive
2016-08-18 18:04:48 +02:00
* @expectedException \Amp\Exception\InvalidArgumentError
*/
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;
});
$context->start();
2016-01-23 07:00:56 +01:00
yield from $context->send(new ExitSuccess(0));
$value = yield from $context->join();
})->done();
Loop\run();
}
}