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

195 lines
4.8 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
2019-08-27 19:17:41 +02:00
use Amp\Parallel\Context\StatusError;
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;
2019-08-27 19:17:41 +02:00
use Amp\PHPUnit\AsyncTestCase;
2017-12-14 06:06:38 +01:00
use Amp\Promise;
2019-08-27 19:17:41 +02:00
abstract class AbstractPoolTest extends AsyncTestCase
2018-10-07 16:50:45 +02:00
{
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()
{
2019-08-27 19:17:41 +02:00
$pool = $this->createPool();
2019-08-27 19:17:41 +02:00
$this->assertTrue($pool->isRunning());
2019-08-27 19:17:41 +02:00
yield $pool->shutdown();
$this->assertFalse($pool->isRunning());
}
2018-10-07 16:50:45 +02:00
public function testIsIdleOnStart()
{
2019-08-27 19:17:41 +02:00
$pool = $this->createPool();
2019-08-27 19:17:41 +02:00
$this->assertTrue($pool->isIdle());
2019-08-27 19:17:41 +02:00
yield $pool->shutdown();
}
public function testShutdownShouldReturnSameResult()
2018-10-07 17:14:27 +02:00
{
2019-08-27 19:17:41 +02:00
$pool = $this->createPool();
2018-10-07 17:14:27 +02:00
2019-08-27 19:17:41 +02:00
$this->assertTrue($pool->isIdle());
2018-10-07 17:14:27 +02:00
2019-08-27 19:17:41 +02:00
$result = yield $pool->shutdown();
$this->assertSame($result, yield $pool->shutdown());
2018-10-07 17:14:27 +02:00
}
public function testPullShouldThrowStatusError()
{
2019-08-27 19:17:41 +02:00
$this->expectException(StatusError::class);
$this->expectExceptionMessage('The pool was shutdown');
$pool = $this->createPool();
2018-10-07 17:14:27 +02:00
2019-08-27 19:17:41 +02:00
$this->assertTrue($pool->isIdle());
2018-10-07 17:14:27 +02:00
2019-08-27 19:17:41 +02:00
yield $pool->shutdown();
2018-10-07 17:14:27 +02:00
2019-08-27 19:17:41 +02:00
$pool->getWorker();
2018-10-07 17:14:27 +02:00
}
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()
{
2019-08-27 19:17:41 +02:00
$pool = $this->createPool();
2019-08-27 19:17:41 +02:00
$this->assertEquals(0, $pool->getIdleWorkerCount());
2019-08-27 19:17:41 +02:00
yield $pool->shutdown();
}
2018-10-07 16:50:45 +02:00
public function testEnqueue()
{
2019-08-27 19:17:41 +02:00
$pool = $this->createPool();
2019-08-27 19:17:41 +02:00
$returnValue = yield $pool->enqueue(new Fixtures\TestTask(42));
$this->assertEquals(42, $returnValue);
2019-08-27 19:17:41 +02:00
yield $pool->shutdown();
}
2018-10-07 16:50:45 +02:00
public function testEnqueueMultiple()
{
2019-08-27 19:17:41 +02:00
$pool = $this->createPool();
2019-08-27 19:17:41 +02:00
$values = yield \Amp\Promise\all([
$pool->enqueue(new Fixtures\TestTask(42)),
$pool->enqueue(new Fixtures\TestTask(56)),
$pool->enqueue(new Fixtures\TestTask(72))
2016-01-23 07:00:56 +01:00
]);
2019-08-27 19:17:41 +02:00
$this->assertEquals([42, 56, 72], $values);
2019-08-27 19:17:41 +02:00
yield $pool->shutdown();
}
2018-10-07 16:50:45 +02:00
public function testKill()
{
2019-08-27 19:17:41 +02:00
$this->setTimeout(1000);
$pool = $this->createPool();
2019-08-27 19:17:41 +02:00
$pool->kill();
$this->assertFalse($pool->isRunning());
}
2017-12-14 06:06:38 +01:00
2018-10-07 16:50:45 +02:00
public function testGet()
{
2019-08-27 19:17:41 +02:00
$pool = $this->createPool();
2017-12-14 06:06:38 +01:00
2019-08-27 19:17:41 +02:00
$worker = $pool->getWorker();
$this->assertInstanceOf(Worker::class, $worker);
2017-12-14 06:06:38 +01:00
2019-08-27 19:17:41 +02:00
$this->assertTrue($worker->isRunning());
$this->assertTrue($worker->isIdle());
2017-12-14 06:06:38 +01:00
2019-08-27 19:17:41 +02:00
$this->assertSame(42, yield $worker->enqueue(new Fixtures\TestTask(42)));
2017-12-14 06:06:38 +01:00
2019-08-27 19:17:41 +02:00
yield $worker->shutdown();
2017-12-14 06:06:38 +01:00
2019-08-27 19:17:41 +02:00
$worker->kill();
2017-12-14 06:06:38 +01:00
}
2018-10-07 16:50:45 +02:00
public function testBusyPool()
{
2019-08-27 19:17:41 +02:00
$pool = $this->createPool(2);
2017-12-14 06:06:38 +01:00
2019-08-27 19:17:41 +02:00
$values = [42, 56, 72];
$tasks = \array_map(function (int $value): Task {
return new Fixtures\TestTask($value);
}, $values);
2017-12-14 06:06:38 +01:00
2019-08-27 19:17:41 +02:00
$promises = \array_map(function (Task $task) use ($pool): Promise {
return $pool->enqueue($task);
}, $tasks);
2017-12-14 06:06:38 +01:00
2019-08-27 19:17:41 +02:00
$this->assertSame($values, yield $promises);
2017-12-14 06:06:38 +01:00
2019-08-27 19:17:41 +02:00
$promises = \array_map(function (Task $task) use ($pool): Promise {
return $pool->enqueue($task);
}, $tasks);
2017-12-14 06:06:38 +01:00
2019-08-27 19:17:41 +02:00
$this->assertSame($values, yield $promises);
2017-12-14 06:06:38 +01:00
2019-08-27 19:17:41 +02:00
yield $pool->shutdown();
2017-12-14 06:06:38 +01:00
}
public function testCreatePoolShouldThrowError()
{
2019-08-27 19:17:41 +02:00
$this->expectException(\Error::class);
$this->expectExceptionMessage('Maximum size must be a non-negative integer');
$this->createPool(-1);
}
2018-10-07 16:50:45 +02:00
public function testCleanGarbageCollection()
{
// See https://github.com/amphp/parallel-functions/issues/5
2019-08-27 19:17:41 +02:00
for ($i = 0; $i < 3; $i++) {
$pool = $this->createPool(32);
$values = \range(1, 50);
$tasks = \array_map(function (int $value): Task {
return new Fixtures\TestTask($value);
}, $values);
$promises = \array_map(function (Task $task) use ($pool): Promise {
return $pool->enqueue($task);
}, $tasks);
$this->assertSame($values, yield $promises);
}
}
2018-12-14 17:16:11 +01:00
public function testPooledKill()
{
2018-12-30 20:32:25 +01:00
// See https://github.com/amphp/parallel/issues/66
2019-08-27 19:17:41 +02:00
$pool = $this->createPool(1);
$worker = $pool->getWorker();
$worker->kill();
$worker2 = $pool->getWorker();
unset($worker); // Invoke destructor.
$this->assertTrue($worker2->isRunning());
2018-12-14 17:16:11 +01:00
}
}