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

110 lines
2.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
2016-08-23 23:47:40 +02:00
use Amp\Parallel\Test\TestCase;
use Interop\Async\Loop;
2016-08-19 00:36:58 +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
*/
2015-12-16 23:39:25 +01:00
abstract protected function createPool($min = null, $max = null);
2016-08-19 00:36:58 +02:00
public function testIsRunning() {
Loop::execute(\Amp\wrap(function () {
$pool = $this->createPool();
$this->assertFalse($pool->isRunning());
$pool->start();
$this->assertTrue($pool->isRunning());
2016-08-19 00:36:58 +02:00
yield $pool->shutdown();
$this->assertFalse($pool->isRunning());
}));
}
2016-08-19 00:36:58 +02:00
public function testIsIdleOnStart() {
Loop::execute(\Amp\wrap(function () {
$pool = $this->createPool();
$pool->start();
$this->assertTrue($pool->isIdle());
2016-08-19 00:36:58 +02:00
yield $pool->shutdown();
}));
}
2016-08-19 00:36:58 +02:00
public function testGetMinSize() {
$pool = $this->createPool(7, 24);
$this->assertEquals(7, $pool->getMinSize());
}
2016-08-19 00:36:58 +02:00
public function testGetMaxSize() {
$pool = $this->createPool(3, 17);
$this->assertEquals(17, $pool->getMaxSize());
}
2016-08-19 00:36:58 +02:00
public function testMinWorkersSpawnedOnStart() {
Loop::execute(\Amp\wrap(function () {
$pool = $this->createPool(8, 32);
$pool->start();
$this->assertEquals(8, $pool->getWorkerCount());
2016-08-19 00:36:58 +02:00
yield $pool->shutdown();
}));
}
2016-08-19 00:36:58 +02:00
public function testWorkersIdleOnStart() {
Loop::execute(\Amp\wrap(function () {
$pool = $this->createPool(8, 32);
$pool->start();
$this->assertEquals(8, $pool->getIdleWorkerCount());
2016-08-19 00:36:58 +02:00
yield $pool->shutdown();
}));
}
2016-08-19 00:36:58 +02:00
public function testEnqueue() {
Loop::execute(\Amp\wrap(function () {
$pool = $this->createPool();
$pool->start();
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();
}));
}
2016-08-19 00:36:58 +02:00
public function testEnqueueMultiple() {
Loop::execute(\Amp\wrap(function () {
$pool = $this->createPool();
$pool->start();
2016-08-19 00:36:58 +02:00
$values = yield \Amp\all([
$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();
}));
}
2016-08-19 00:36:58 +02:00
public function testKill() {
$pool = $this->createPool();
$pool->start();
2016-08-19 00:36:58 +02:00
$this->assertRunTimeLessThan([$pool, 'kill'], 1);
$this->assertFalse($pool->isRunning());
}
}