1
0
mirror of https://github.com/danog/file.git synced 2024-12-04 02:07:46 +01:00
file/test/Driver/ParallelFileTest.php

55 lines
1.3 KiB
PHP
Raw Normal View History

2020-06-30 21:45:09 +02:00
<?php
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;
2022-02-08 02:31:21 +01:00
use Amp\Parallel\Worker\DefaultWorkerPool;
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
{
2022-02-08 02:31:21 +01:00
$this->pool = new DefaultWorkerPool();
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
}
protected function tearDownAsync(): void
{
$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');
}
try {
$this->assertSame($maxCount, $this->pool->getWorkerCount());
} finally {
foreach ($files as $file) {
$file->close();
}
}
}
2020-06-30 21:45:09 +02:00
}