2015-09-15 01:59:33 +02:00
|
|
|
<?php
|
|
|
|
|
2016-08-18 18:04:48 +02:00
|
|
|
namespace Amp\Tests\Concurrent\Worker;
|
|
|
|
|
|
|
|
use Amp\Awaitable;
|
|
|
|
use Amp\Coroutine;
|
|
|
|
use Amp\Loop;
|
|
|
|
use Amp\Tests\Concurrent\TestCase;
|
2015-09-15 01:59:33 +02:00
|
|
|
|
2015-12-12 05:47:46 +01:00
|
|
|
abstract class AbstractPoolTest extends TestCase
|
2015-09-15 01:59:33 +02:00
|
|
|
{
|
2015-12-16 22:53:53 +01:00
|
|
|
/**
|
|
|
|
* @param int $min
|
|
|
|
* @param int $max
|
|
|
|
*
|
2016-08-18 18:04:48 +02:00
|
|
|
* @return \Amp\Concurrent\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);
|
2015-09-15 01:59:33 +02:00
|
|
|
|
2015-11-11 09:59:22 +01:00
|
|
|
public function testIsRunning()
|
|
|
|
{
|
|
|
|
Coroutine\run(function () {
|
|
|
|
$pool = $this->createPool();
|
|
|
|
$this->assertFalse($pool->isRunning());
|
|
|
|
|
|
|
|
$pool->start();
|
|
|
|
$this->assertTrue($pool->isRunning());
|
|
|
|
|
2016-01-25 06:03:45 +01:00
|
|
|
yield from $pool->shutdown();
|
2015-11-11 09:59:22 +01:00
|
|
|
$this->assertFalse($pool->isRunning());
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testIsIdleOnStart()
|
|
|
|
{
|
|
|
|
Coroutine\run(function () {
|
|
|
|
$pool = $this->createPool();
|
|
|
|
$pool->start();
|
|
|
|
|
|
|
|
$this->assertTrue($pool->isIdle());
|
|
|
|
|
2016-01-25 06:03:45 +01:00
|
|
|
yield from $pool->shutdown();
|
2015-11-11 09:59:22 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetMinSize()
|
|
|
|
{
|
|
|
|
$pool = $this->createPool(7, 24);
|
|
|
|
$this->assertEquals(7, $pool->getMinSize());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetMaxSize()
|
|
|
|
{
|
|
|
|
$pool = $this->createPool(3, 17);
|
|
|
|
$this->assertEquals(17, $pool->getMaxSize());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testMinWorkersSpawnedOnStart()
|
|
|
|
{
|
|
|
|
Coroutine\run(function () {
|
|
|
|
$pool = $this->createPool(8, 32);
|
|
|
|
$pool->start();
|
|
|
|
|
|
|
|
$this->assertEquals(8, $pool->getWorkerCount());
|
|
|
|
|
2016-01-25 06:03:45 +01:00
|
|
|
yield from $pool->shutdown();
|
2015-11-11 09:59:22 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testWorkersIdleOnStart()
|
|
|
|
{
|
|
|
|
Coroutine\run(function () {
|
|
|
|
$pool = $this->createPool(8, 32);
|
|
|
|
$pool->start();
|
|
|
|
|
|
|
|
$this->assertEquals(8, $pool->getIdleWorkerCount());
|
|
|
|
|
2016-01-25 06:03:45 +01:00
|
|
|
yield from $pool->shutdown();
|
2015-11-11 09:59:22 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-09-15 01:59:33 +02:00
|
|
|
public function testEnqueue()
|
|
|
|
{
|
2015-11-11 09:59:22 +01:00
|
|
|
Coroutine\run(function () {
|
2015-09-15 01:59:33 +02:00
|
|
|
$pool = $this->createPool();
|
|
|
|
$pool->start();
|
|
|
|
|
2016-01-23 07:00:56 +01:00
|
|
|
$returnValue = yield from $pool->enqueue(new TestTask(42));
|
2015-09-15 01:59:33 +02:00
|
|
|
$this->assertEquals(42, $returnValue);
|
|
|
|
|
2016-01-25 06:03:45 +01:00
|
|
|
yield from $pool->shutdown();
|
2015-11-11 09:59:22 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-12-12 05:47:46 +01:00
|
|
|
public function testEnqueueMultiple()
|
|
|
|
{
|
|
|
|
Coroutine\run(function () {
|
|
|
|
$pool = $this->createPool();
|
|
|
|
$pool->start();
|
|
|
|
|
2016-01-23 07:00:56 +01:00
|
|
|
$values = yield Awaitable\all([
|
2015-12-12 05:47:46 +01:00
|
|
|
new Coroutine\Coroutine($pool->enqueue(new TestTask(42))),
|
|
|
|
new Coroutine\Coroutine($pool->enqueue(new TestTask(56))),
|
|
|
|
new Coroutine\Coroutine($pool->enqueue(new TestTask(72)))
|
2016-01-23 07:00:56 +01:00
|
|
|
]);
|
2015-12-12 05:47:46 +01:00
|
|
|
|
|
|
|
$this->assertEquals([42, 56, 72], $values);
|
|
|
|
|
2016-01-25 06:03:45 +01:00
|
|
|
yield from $pool->shutdown();
|
2015-12-12 05:47:46 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-11-11 09:59:22 +01:00
|
|
|
public function testKill()
|
|
|
|
{
|
|
|
|
$pool = $this->createPool();
|
|
|
|
$pool->start();
|
2015-09-15 01:59:33 +02:00
|
|
|
|
2015-11-11 09:59:22 +01:00
|
|
|
$this->assertRunTimeLessThan([$pool, 'kill'], 0.5);
|
|
|
|
$this->assertFalse($pool->isRunning());
|
2015-09-15 01:59:33 +02:00
|
|
|
}
|
|
|
|
}
|