2015-09-15 01:59:33 +02:00
|
|
|
<?php
|
|
|
|
namespace Icicle\Tests\Concurrent\Worker;
|
|
|
|
|
2015-12-05 06:50:32 +01:00
|
|
|
use Icicle\Concurrent\Worker\DefaultPool;
|
2015-09-15 01:59:33 +02:00
|
|
|
use Icicle\Coroutine;
|
|
|
|
use Icicle\Loop;
|
|
|
|
use Icicle\Tests\Concurrent\TestCase;
|
|
|
|
|
|
|
|
class PoolTest extends TestCase
|
|
|
|
{
|
2015-11-11 09:59:22 +01:00
|
|
|
public function createPool($min = null, $max = null)
|
2015-09-15 01:59:33 +02:00
|
|
|
{
|
2015-12-05 06:50:32 +01:00
|
|
|
return new DefaultPool($min, $max);
|
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());
|
|
|
|
|
|
|
|
yield $pool->shutdown();
|
|
|
|
$this->assertFalse($pool->isRunning());
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testIsIdleOnStart()
|
|
|
|
{
|
|
|
|
Coroutine\run(function () {
|
|
|
|
$pool = $this->createPool();
|
|
|
|
$pool->start();
|
|
|
|
|
|
|
|
$this->assertTrue($pool->isIdle());
|
|
|
|
|
|
|
|
yield $pool->shutdown();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
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());
|
|
|
|
|
|
|
|
yield $pool->shutdown();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testWorkersIdleOnStart()
|
|
|
|
{
|
|
|
|
Coroutine\run(function () {
|
|
|
|
$pool = $this->createPool(8, 32);
|
|
|
|
$pool->start();
|
|
|
|
|
|
|
|
$this->assertEquals(8, $pool->getIdleWorkerCount());
|
|
|
|
|
|
|
|
yield $pool->shutdown();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
$returnValue = (yield $pool->enqueue(new TestTask(42)));
|
|
|
|
$this->assertEquals(42, $returnValue);
|
|
|
|
|
|
|
|
yield $pool->shutdown();
|
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
|
|
|
}
|
|
|
|
}
|