2016-12-30 02:16:04 +01:00
|
|
|
<?php
|
2015-09-15 01:59:33 +02:00
|
|
|
|
2016-08-23 23:47:40 +02:00
|
|
|
namespace Amp\Parallel\Test\Worker;
|
2016-08-18 18:04:48 +02:00
|
|
|
|
2017-03-16 23:03:59 +01:00
|
|
|
use Amp\Loop;
|
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;
|
2017-03-22 05:19:15 +01:00
|
|
|
use Amp\PHPUnit\TestCase;
|
2017-12-14 06:06:38 +01:00
|
|
|
use Amp\Promise;
|
2015-09-15 01:59:33 +02:00
|
|
|
|
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
|
|
|
*/
|
2017-12-13 21:14:31 +01:00
|
|
|
abstract protected function createPool($max = Pool::DEFAULT_MAX_SIZE): Pool;
|
2015-09-15 01:59:33 +02:00
|
|
|
|
2016-08-19 00:36:58 +02:00
|
|
|
public function testIsRunning() {
|
2017-03-16 23:03:59 +01:00
|
|
|
Loop::run(function () {
|
2015-11-11 09:59:22 +01:00
|
|
|
$pool = $this->createPool();
|
|
|
|
|
|
|
|
$this->assertTrue($pool->isRunning());
|
|
|
|
|
2016-08-19 00:36:58 +02:00
|
|
|
yield $pool->shutdown();
|
2015-11-11 09:59:22 +01:00
|
|
|
$this->assertFalse($pool->isRunning());
|
2017-03-16 23:03:59 +01:00
|
|
|
});
|
2015-11-11 09:59:22 +01:00
|
|
|
}
|
|
|
|
|
2016-08-19 00:36:58 +02:00
|
|
|
public function testIsIdleOnStart() {
|
2017-03-16 23:03:59 +01:00
|
|
|
Loop::run(function () {
|
2015-11-11 09:59:22 +01:00
|
|
|
$pool = $this->createPool();
|
|
|
|
|
|
|
|
$this->assertTrue($pool->isIdle());
|
|
|
|
|
2016-08-19 00:36:58 +02:00
|
|
|
yield $pool->shutdown();
|
2017-03-16 23:03:59 +01:00
|
|
|
});
|
2015-11-11 09:59:22 +01:00
|
|
|
}
|
|
|
|
|
2016-08-19 00:36:58 +02:00
|
|
|
public function testGetMaxSize() {
|
2017-12-13 21:14:31 +01:00
|
|
|
$pool = $this->createPool(17);
|
2015-11-11 09:59:22 +01:00
|
|
|
$this->assertEquals(17, $pool->getMaxSize());
|
|
|
|
}
|
|
|
|
|
2016-08-19 00:36:58 +02:00
|
|
|
public function testWorkersIdleOnStart() {
|
2017-03-16 23:03:59 +01:00
|
|
|
Loop::run(function () {
|
2017-12-14 06:06:38 +01:00
|
|
|
$pool = $this->createPool();
|
2015-11-11 09:59:22 +01:00
|
|
|
|
2017-12-13 21:14:31 +01:00
|
|
|
$this->assertEquals(0, $pool->getIdleWorkerCount());
|
2015-11-11 09:59:22 +01:00
|
|
|
|
2016-08-19 00:36:58 +02:00
|
|
|
yield $pool->shutdown();
|
2017-03-16 23:03:59 +01:00
|
|
|
});
|
2015-11-11 09:59:22 +01:00
|
|
|
}
|
|
|
|
|
2016-08-19 00:36:58 +02:00
|
|
|
public function testEnqueue() {
|
2017-03-16 23:03:59 +01:00
|
|
|
Loop::run(function () {
|
2015-09-15 01:59:33 +02:00
|
|
|
$pool = $this->createPool();
|
|
|
|
|
2016-08-19 00:36:58 +02:00
|
|
|
$returnValue = yield $pool->enqueue(new TestTask(42));
|
2015-09-15 01:59:33 +02:00
|
|
|
$this->assertEquals(42, $returnValue);
|
|
|
|
|
2016-08-19 00:36:58 +02:00
|
|
|
yield $pool->shutdown();
|
2017-03-16 23:03:59 +01:00
|
|
|
});
|
2015-11-11 09:59:22 +01:00
|
|
|
}
|
|
|
|
|
2016-08-19 00:36:58 +02:00
|
|
|
public function testEnqueueMultiple() {
|
2017-03-16 23:03:59 +01:00
|
|
|
Loop::run(function () {
|
2015-12-12 05:47:46 +01:00
|
|
|
$pool = $this->createPool();
|
|
|
|
|
2017-03-16 23:03:59 +01:00
|
|
|
$values = yield \Amp\Promise\all([
|
2016-08-19 00:36:58 +02:00
|
|
|
$pool->enqueue(new TestTask(42)),
|
|
|
|
$pool->enqueue(new TestTask(56)),
|
|
|
|
$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-08-19 00:36:58 +02:00
|
|
|
yield $pool->shutdown();
|
2017-03-16 23:03:59 +01:00
|
|
|
});
|
2015-12-12 05:47:46 +01:00
|
|
|
}
|
|
|
|
|
2016-08-19 00:36:58 +02:00
|
|
|
public function testKill() {
|
2015-11-11 09:59:22 +01:00
|
|
|
$pool = $this->createPool();
|
2015-09-15 01:59:33 +02:00
|
|
|
|
2017-03-22 05:19:15 +01:00
|
|
|
$this->assertRunTimeLessThan([$pool, 'kill'], 1000);
|
2015-11-11 09:59:22 +01:00
|
|
|
$this->assertFalse($pool->isRunning());
|
2015-09-15 01:59:33 +02:00
|
|
|
}
|
2017-12-14 06:06:38 +01:00
|
|
|
|
|
|
|
public function testGet() {
|
|
|
|
Loop::run(function () {
|
|
|
|
$pool = $this->createPool();
|
|
|
|
|
|
|
|
$worker = $pool->get();
|
|
|
|
$this->assertInstanceOf(Worker::class, $worker);
|
|
|
|
|
|
|
|
$this->assertFalse($worker->isRunning());
|
|
|
|
$this->assertTrue($worker->isIdle());
|
|
|
|
|
|
|
|
$this->assertSame(42, yield $worker->enqueue(new TestTask(42)));
|
|
|
|
|
|
|
|
yield $worker->shutdown();
|
|
|
|
|
|
|
|
$worker->kill();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testBusyPool() {
|
|
|
|
Loop::run(function () {
|
|
|
|
$pool = $this->createPool(2);
|
|
|
|
|
|
|
|
$values = [42, 56, 72];
|
|
|
|
$tasks = \array_map(function (int $value): Task {
|
|
|
|
return new TestTask($value);
|
|
|
|
}, $values);
|
|
|
|
|
|
|
|
$promises = \array_map(function (Task $task) use ($pool): Promise {
|
|
|
|
return $pool->enqueue($task);
|
|
|
|
}, $tasks);
|
|
|
|
|
|
|
|
$this->assertSame($values, yield $promises);
|
|
|
|
|
|
|
|
$promises = \array_map(function (Task $task) use ($pool): Promise {
|
|
|
|
return $pool->enqueue($task);
|
|
|
|
}, $tasks);
|
|
|
|
|
|
|
|
$this->assertSame($values, yield $promises);
|
|
|
|
|
|
|
|
yield $pool->shutdown();
|
|
|
|
});
|
|
|
|
}
|
2017-12-27 19:36:28 +01:00
|
|
|
|
|
|
|
public function testCleanGarbageCollection() {
|
|
|
|
// See https://github.com/amphp/parallel-functions/issues/5
|
|
|
|
Loop::run(function () {
|
2018-01-23 00:59:13 +01:00
|
|
|
for ($i = 0; $i < 3; $i++) {
|
2017-12-27 19:36:28 +01:00
|
|
|
$pool = $this->createPool(32);
|
|
|
|
|
|
|
|
$values = \range(1, 50);
|
|
|
|
$tasks = \array_map(function (int $value): Task {
|
|
|
|
return new TestTask($value);
|
|
|
|
}, $values);
|
|
|
|
|
|
|
|
$promises = \array_map(function (Task $task) use ($pool): Promise {
|
|
|
|
return $pool->enqueue($task);
|
|
|
|
}, $tasks);
|
|
|
|
|
|
|
|
$this->assertSame($values, yield $promises);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2015-09-15 01:59:33 +02:00
|
|
|
}
|