1
0
mirror of https://github.com/danog/parallel.git synced 2024-12-12 09:09:35 +01:00
parallel/test/Worker/AbstractPoolTest.php

209 lines
5.2 KiB
PHP
Raw Normal View History

2016-12-30 02:16:04 +01:00
<?php
2016-08-23 23:47:40 +02:00
namespace Amp\Parallel\Test\Worker;
2016-08-18 18:04:48 +02:00
use Amp\Loop;
2017-12-13 04:39:51 +01:00
use Amp\Parallel\Worker\Pool;
2017-12-14 06:06:38 +01:00
use Amp\Parallel\Worker\Task;
use Amp\Parallel\Worker\Worker;
use Amp\PHPUnit\TestCase;
2017-12-14 06:06:38 +01:00
use Amp\Promise;
2018-10-07 16:50:45 +02:00
abstract class AbstractPoolTest extends TestCase
{
2015-12-16 22:53:53 +01:00
/**
* @param int $min
* @param int $max
*
2016-08-23 23:47:40 +02:00
* @return \Amp\Parallel\Worker\Pool
2015-12-16 22:53:53 +01:00
*/
abstract protected function createPool($max = Pool::DEFAULT_MAX_SIZE): Pool;
2018-10-07 16:50:45 +02:00
public function testIsRunning()
{
Loop::run(function () {
$pool = $this->createPool();
$this->assertTrue($pool->isRunning());
2016-08-19 00:36:58 +02:00
yield $pool->shutdown();
$this->assertFalse($pool->isRunning());
});
}
2018-10-07 16:50:45 +02:00
public function testIsIdleOnStart()
{
Loop::run(function () {
$pool = $this->createPool();
$this->assertTrue($pool->isIdle());
2016-08-19 00:36:58 +02:00
yield $pool->shutdown();
});
}
2018-10-07 17:14:27 +02:00
/**
* @expectedException \Amp\Parallel\Context\StatusError
* @expectedExceptionMessage The pool was shutdown
*/
public function testShutdownShouldThrowStatusError()
{
Loop::run(function () {
$pool = $this->createPool();
$this->assertTrue($pool->isIdle());
yield $pool->shutdown();
yield $pool->shutdown();
});
}
/**
* @expectedException \Amp\Parallel\Context\StatusError
* @expectedExceptionMessage The pool was shutdown
*/
public function testPullShouldThrowStatusError()
{
Loop::run(function () {
$pool = $this->createPool();
$this->assertTrue($pool->isIdle());
yield $pool->shutdown();
$pool->get();
});
}
2018-10-07 16:50:45 +02:00
public function testGetMaxSize()
{
$pool = $this->createPool(17);
$this->assertEquals(17, $pool->getMaxSize());
}
2018-10-07 16:50:45 +02:00
public function testWorkersIdleOnStart()
{
Loop::run(function () {
2017-12-14 06:06:38 +01:00
$pool = $this->createPool();
$this->assertEquals(0, $pool->getIdleWorkerCount());
2016-08-19 00:36:58 +02:00
yield $pool->shutdown();
});
}
2018-10-07 16:50:45 +02:00
public function testEnqueue()
{
Loop::run(function () {
$pool = $this->createPool();
2016-08-19 00:36:58 +02:00
$returnValue = yield $pool->enqueue(new TestTask(42));
$this->assertEquals(42, $returnValue);
2016-08-19 00:36:58 +02:00
yield $pool->shutdown();
});
}
2018-10-07 16:50:45 +02:00
public function testEnqueueMultiple()
{
Loop::run(function () {
$pool = $this->createPool();
$values = yield \Amp\Promise\all([
2016-08-19 00:36:58 +02:00
$pool->enqueue(new TestTask(42)),
$pool->enqueue(new TestTask(56)),
$pool->enqueue(new TestTask(72))
2016-01-23 07:00:56 +01:00
]);
$this->assertEquals([42, 56, 72], $values);
2016-08-19 00:36:58 +02:00
yield $pool->shutdown();
});
}
2018-10-07 16:50:45 +02:00
public function testKill()
{
$pool = $this->createPool();
$this->assertRunTimeLessThan([$pool, 'kill'], 1000);
$this->assertFalse($pool->isRunning());
}
2017-12-14 06:06:38 +01:00
2018-10-07 16:50:45 +02:00
public function testGet()
{
2017-12-14 06:06:38 +01:00
Loop::run(function () {
$pool = $this->createPool();
$worker = $pool->get();
$this->assertInstanceOf(Worker::class, $worker);
$this->assertTrue($worker->isRunning());
2017-12-14 06:06:38 +01:00
$this->assertTrue($worker->isIdle());
$this->assertSame(42, yield $worker->enqueue(new TestTask(42)));
yield $worker->shutdown();
$worker->kill();
});
}
2018-10-07 16:50:45 +02:00
public function testBusyPool()
{
2017-12-14 06:06:38 +01:00
Loop::run(function () {
$pool = $this->createPool(2);
$values = [42, 56, 72];
$tasks = \array_map(function (int $value): Task {
return new TestTask($value);
}, $values);
$promises = \array_map(function (Task $task) use ($pool): Promise {
return $pool->enqueue($task);
}, $tasks);
$this->assertSame($values, yield $promises);
$promises = \array_map(function (Task $task) use ($pool): Promise {
return $pool->enqueue($task);
}, $tasks);
$this->assertSame($values, yield $promises);
yield $pool->shutdown();
});
}
/**
* @expectedException \Error
* @expectedExceptionMessage Maximum size must be a non-negative integer
*/
public function testCreatePoolShouldThrowError()
{
Loop::run(function () {
$this->createPool(-1);
});
}
2018-10-07 16:50:45 +02:00
public function testCleanGarbageCollection()
{
// See https://github.com/amphp/parallel-functions/issues/5
Loop::run(function () {
for ($i = 0; $i < 3; $i++) {
$pool = $this->createPool(32);
$values = \range(1, 50);
$tasks = \array_map(function (int $value): Task {
return new TestTask($value);
}, $values);
$promises = \array_map(function (Task $task) use ($pool): Promise {
return $pool->enqueue($task);
}, $tasks);
$this->assertSame($values, yield $promises);
}
});
}
}