123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- <?php
- declare(strict_types=1);
- namespace PhpCsFixer\Tests\Runner\Parallel;
- use Fidry\CpuCoreCounter\CpuCoreCounter;
- use Fidry\CpuCoreCounter\Finder\DummyCpuCoreFinder;
- use PhpCsFixer\Runner\Parallel\ParallelConfig;
- use PhpCsFixer\Runner\Parallel\ParallelConfigFactory;
- use PhpCsFixer\Tests\TestCase;
- final class ParallelConfigFactoryTest extends TestCase
- {
- protected function tearDown(): void
- {
- $this->mockCpuCount(null);
- parent::tearDown();
- }
- public function testSequentialConfigHasExactlyOneProcess(): void
- {
- $config = ParallelConfigFactory::sequential();
- self::assertSame(1, $config->getMaxProcesses());
- }
-
- public function testDetectConfigurationWithoutParams(): void
- {
- $this->mockCpuCount(7);
- $config = ParallelConfigFactory::detect();
- self::assertSame(6, $config->getMaxProcesses());
- self::assertSame(ParallelConfig::DEFAULT_FILES_PER_PROCESS, $config->getFilesPerProcess());
- self::assertSame(ParallelConfig::DEFAULT_PROCESS_TIMEOUT, $config->getProcessTimeout());
- }
- public function testDetectConfigurationWithParams(): void
- {
- $this->mockCpuCount(7);
- $config1 = ParallelConfigFactory::detect(22, 2_200, 5);
- self::assertSame(5, $config1->getMaxProcesses());
- self::assertSame(22, $config1->getFilesPerProcess());
- self::assertSame(2_200, $config1->getProcessTimeout());
- $config2 = ParallelConfigFactory::detect(22, 2_200, 6);
- self::assertSame(6, $config2->getMaxProcesses());
- $config3 = ParallelConfigFactory::detect(22, 2_200, 10);
- self::assertSame(6, $config3->getMaxProcesses());
- }
- public function testDetectConfigurationWithDefaultValue(): void
- {
- $config = ParallelConfigFactory::detect(null, 60);
- self::assertSame(ParallelConfig::DEFAULT_FILES_PER_PROCESS, $config->getFilesPerProcess());
- self::assertSame(60, $config->getProcessTimeout());
- }
-
- public function testDetectConfigurationWithNamedArgs(): void
- {
- $this->mockCpuCount(7);
-
- $config1 = \call_user_func_array([ParallelConfigFactory::class, 'detect'], ['processTimeout' => 300]);
- self::assertSame(ParallelConfig::DEFAULT_FILES_PER_PROCESS, $config1->getFilesPerProcess());
- self::assertSame(300, $config1->getProcessTimeout());
-
- $config2 = \call_user_func_array([ParallelConfigFactory::class, 'detect'], [
- 'maxProcesses' => 1,
- 'processTimeout' => 300,
- 'filesPerProcess' => 5,
- ]);
- self::assertSame(1, $config2->getMaxProcesses());
- self::assertSame(5, $config2->getFilesPerProcess());
- self::assertSame(300, $config2->getProcessTimeout());
-
- $config3 = \call_user_func_array([ParallelConfigFactory::class, 'detect'], ['filesPerProcess' => 7]);
- self::assertSame(7, $config3->getFilesPerProcess());
- self::assertSame(ParallelConfig::DEFAULT_PROCESS_TIMEOUT, $config3->getProcessTimeout());
-
- $config3 = \call_user_func_array([ParallelConfigFactory::class, 'detect'], ['maxProcesses' => 1]);
- self::assertSame(1, $config3->getMaxProcesses());
- self::assertSame(ParallelConfig::DEFAULT_FILES_PER_PROCESS, $config3->getFilesPerProcess());
- self::assertSame(ParallelConfig::DEFAULT_PROCESS_TIMEOUT, $config3->getProcessTimeout());
- }
-
- private function mockCpuCount(?int $count): void
- {
- \Closure::bind(static function () use ($count): void {
- ParallelConfigFactory::$cpuDetector = null !== $count ?
- new CpuCoreCounter([
- new DummyCpuCoreFinder($count),
- ]) : null;
- }, null, ParallelConfigFactory::class)();
- }
- }
|