2022-12-17 22:52:21 +01:00
|
|
|
<?php declare(strict_types=1);
|
2020-06-30 21:45:09 +02:00
|
|
|
|
|
|
|
namespace Amp\File\Test\Driver;
|
|
|
|
|
|
|
|
use Amp\File;
|
2022-02-08 19:49:16 +01:00
|
|
|
use Amp\File\Driver\ParallelFilesystemDriver;
|
2020-06-30 21:45:09 +02:00
|
|
|
use Amp\File\Test\AsyncFileTest;
|
2023-02-18 16:49:06 +01:00
|
|
|
use Amp\Parallel\Worker\ContextWorkerPool;
|
2022-02-08 02:31:21 +01:00
|
|
|
use Amp\Parallel\Worker\WorkerPool;
|
2020-06-30 21:45:09 +02:00
|
|
|
|
|
|
|
class ParallelFileTest extends AsyncFileTest
|
|
|
|
{
|
2020-10-08 16:23:44 +02:00
|
|
|
private const DEFAULT_WORKER_LIMIT = 8;
|
2020-06-30 21:45:09 +02:00
|
|
|
|
2022-02-08 02:31:21 +01:00
|
|
|
private WorkerPool $pool;
|
2020-10-08 16:23:44 +02:00
|
|
|
|
2022-02-08 19:49:16 +01:00
|
|
|
protected function createDriver(int $workerLimit = self::DEFAULT_WORKER_LIMIT): File\FilesystemDriver
|
2020-06-30 21:45:09 +02:00
|
|
|
{
|
2023-02-18 16:49:06 +01:00
|
|
|
$this->pool = new ContextWorkerPool();
|
2020-06-30 21:45:09 +02:00
|
|
|
|
2022-02-08 19:49:16 +01:00
|
|
|
return new ParallelFilesystemDriver($this->pool, $workerLimit);
|
2020-06-30 21:45:09 +02:00
|
|
|
}
|
|
|
|
|
2023-02-25 02:16:12 +01:00
|
|
|
protected function tearDown(): void
|
2020-06-30 21:45:09 +02:00
|
|
|
{
|
|
|
|
$this->pool->shutdown();
|
|
|
|
}
|
2020-10-08 16:23:44 +02:00
|
|
|
|
|
|
|
public function getWorkerLimits(): iterable
|
|
|
|
{
|
2022-02-08 19:49:16 +01:00
|
|
|
return \array_map(fn (int $count): array => [$count], \range(4, 16, 4));
|
2020-10-08 16:23:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider getWorkerLimits
|
|
|
|
*/
|
|
|
|
public function testMultipleOpenFiles(int $maxCount)
|
|
|
|
{
|
|
|
|
$driver = $this->createDriver($maxCount);
|
|
|
|
|
|
|
|
$files = [];
|
|
|
|
for ($i = 0; $i < $maxCount * 2; ++$i) {
|
|
|
|
$files[] = $driver->openFile(__FILE__, 'r');
|
|
|
|
}
|
|
|
|
|
2023-02-25 02:16:12 +01:00
|
|
|
foreach ($files as $file) {
|
|
|
|
$file->close();
|
2020-10-08 16:23:44 +02:00
|
|
|
}
|
2023-02-25 02:16:12 +01:00
|
|
|
|
|
|
|
$this->assertSame($maxCount, $this->pool->getWorkerCount());
|
2020-10-08 16:23:44 +02:00
|
|
|
}
|
2020-06-30 21:45:09 +02:00
|
|
|
}
|