2016-12-29 19:16:04 -06:00
|
|
|
<?php
|
2015-09-14 18:59:33 -05:00
|
|
|
|
2016-08-23 16:47:40 -05:00
|
|
|
namespace Amp\Parallel\Test\Worker;
|
2016-08-18 11:04:48 -05:00
|
|
|
|
2017-03-16 17:03:59 -05:00
|
|
|
use Amp\Loop;
|
2017-12-12 21:39:51 -06:00
|
|
|
use Amp\Parallel\Worker\Pool;
|
2017-03-21 23:19:15 -05:00
|
|
|
use Amp\PHPUnit\TestCase;
|
2015-09-14 18:59:33 -05:00
|
|
|
|
2016-08-18 17:36:58 -05:00
|
|
|
abstract class AbstractPoolTest extends TestCase {
|
2015-12-16 15:53:53 -06:00
|
|
|
/**
|
|
|
|
* @param int $min
|
|
|
|
* @param int $max
|
|
|
|
*
|
2016-08-23 16:47:40 -05:00
|
|
|
* @return \Amp\Parallel\Worker\Pool
|
2015-12-16 15:53:53 -06:00
|
|
|
*/
|
2017-12-13 14:14:31 -06:00
|
|
|
abstract protected function createPool($max = Pool::DEFAULT_MAX_SIZE): Pool;
|
2015-09-14 18:59:33 -05:00
|
|
|
|
2016-08-18 17:36:58 -05:00
|
|
|
public function testIsRunning() {
|
2017-03-16 17:03:59 -05:00
|
|
|
Loop::run(function () {
|
2015-11-11 02:59:22 -06:00
|
|
|
$pool = $this->createPool();
|
|
|
|
|
|
|
|
$this->assertTrue($pool->isRunning());
|
|
|
|
|
2016-08-18 17:36:58 -05:00
|
|
|
yield $pool->shutdown();
|
2015-11-11 02:59:22 -06:00
|
|
|
$this->assertFalse($pool->isRunning());
|
2017-03-16 17:03:59 -05:00
|
|
|
});
|
2015-11-11 02:59:22 -06:00
|
|
|
}
|
|
|
|
|
2016-08-18 17:36:58 -05:00
|
|
|
public function testIsIdleOnStart() {
|
2017-03-16 17:03:59 -05:00
|
|
|
Loop::run(function () {
|
2015-11-11 02:59:22 -06:00
|
|
|
$pool = $this->createPool();
|
|
|
|
|
|
|
|
$this->assertTrue($pool->isIdle());
|
|
|
|
|
2016-08-18 17:36:58 -05:00
|
|
|
yield $pool->shutdown();
|
2017-03-16 17:03:59 -05:00
|
|
|
});
|
2015-11-11 02:59:22 -06:00
|
|
|
}
|
|
|
|
|
2016-08-18 17:36:58 -05:00
|
|
|
public function testGetMaxSize() {
|
2017-12-13 14:14:31 -06:00
|
|
|
$pool = $this->createPool(17);
|
2015-11-11 02:59:22 -06:00
|
|
|
$this->assertEquals(17, $pool->getMaxSize());
|
|
|
|
}
|
|
|
|
|
2016-08-18 17:36:58 -05:00
|
|
|
public function testWorkersIdleOnStart() {
|
2017-03-16 17:03:59 -05:00
|
|
|
Loop::run(function () {
|
2017-12-13 14:14:31 -06:00
|
|
|
$pool = $this->createPool(32);
|
2015-11-11 02:59:22 -06:00
|
|
|
|
2017-12-13 14:14:31 -06:00
|
|
|
$this->assertEquals(0, $pool->getIdleWorkerCount());
|
2015-11-11 02:59:22 -06:00
|
|
|
|
2016-08-18 17:36:58 -05:00
|
|
|
yield $pool->shutdown();
|
2017-03-16 17:03:59 -05:00
|
|
|
});
|
2015-11-11 02:59:22 -06:00
|
|
|
}
|
|
|
|
|
2016-08-18 17:36:58 -05:00
|
|
|
public function testEnqueue() {
|
2017-03-16 17:03:59 -05:00
|
|
|
Loop::run(function () {
|
2015-09-14 18:59:33 -05:00
|
|
|
$pool = $this->createPool();
|
|
|
|
|
2016-08-18 17:36:58 -05:00
|
|
|
$returnValue = yield $pool->enqueue(new TestTask(42));
|
2015-09-14 18:59:33 -05:00
|
|
|
$this->assertEquals(42, $returnValue);
|
|
|
|
|
2016-08-18 17:36:58 -05:00
|
|
|
yield $pool->shutdown();
|
2017-03-16 17:03:59 -05:00
|
|
|
});
|
2015-11-11 02:59:22 -06:00
|
|
|
}
|
|
|
|
|
2016-08-18 17:36:58 -05:00
|
|
|
public function testEnqueueMultiple() {
|
2017-03-16 17:03:59 -05:00
|
|
|
Loop::run(function () {
|
2015-12-11 22:47:46 -06:00
|
|
|
$pool = $this->createPool();
|
|
|
|
|
2017-03-16 17:03:59 -05:00
|
|
|
$values = yield \Amp\Promise\all([
|
2016-08-18 17:36:58 -05:00
|
|
|
$pool->enqueue(new TestTask(42)),
|
|
|
|
$pool->enqueue(new TestTask(56)),
|
|
|
|
$pool->enqueue(new TestTask(72))
|
2016-01-23 00:00:56 -06:00
|
|
|
]);
|
2015-12-11 22:47:46 -06:00
|
|
|
|
|
|
|
$this->assertEquals([42, 56, 72], $values);
|
|
|
|
|
2016-08-18 17:36:58 -05:00
|
|
|
yield $pool->shutdown();
|
2017-03-16 17:03:59 -05:00
|
|
|
});
|
2015-12-11 22:47:46 -06:00
|
|
|
}
|
|
|
|
|
2016-08-18 17:36:58 -05:00
|
|
|
public function testKill() {
|
2015-11-11 02:59:22 -06:00
|
|
|
$pool = $this->createPool();
|
2015-09-14 18:59:33 -05:00
|
|
|
|
2017-03-21 23:19:15 -05:00
|
|
|
$this->assertRunTimeLessThan([$pool, 'kill'], 1000);
|
2015-11-11 02:59:22 -06:00
|
|
|
$this->assertFalse($pool->isRunning());
|
2015-09-14 18:59:33 -05:00
|
|
|
}
|
|
|
|
}
|