ParallelConfigFactoryTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4. * This file is part of PHP CS Fixer.
  5. *
  6. * (c) Fabien Potencier <fabien@symfony.com>
  7. * Dariusz Rumiński <dariusz.ruminski@gmail.com>
  8. *
  9. * This source file is subject to the MIT license that is bundled
  10. * with this source code in the file LICENSE.
  11. */
  12. namespace PhpCsFixer\Tests\Runner\Parallel;
  13. use Fidry\CpuCoreCounter\CpuCoreCounter;
  14. use Fidry\CpuCoreCounter\Finder\DummyCpuCoreFinder;
  15. use PhpCsFixer\Runner\Parallel\ParallelConfig;
  16. use PhpCsFixer\Runner\Parallel\ParallelConfigFactory;
  17. use PhpCsFixer\Tests\TestCase;
  18. /**
  19. * @internal
  20. *
  21. * @covers \PhpCsFixer\Runner\Parallel\ParallelConfigFactory
  22. */
  23. final class ParallelConfigFactoryTest extends TestCase
  24. {
  25. protected function tearDown(): void
  26. {
  27. $this->mockCpuCount(null);
  28. parent::tearDown();
  29. }
  30. public function testSequentialConfigHasExactlyOneProcess(): void
  31. {
  32. $config = ParallelConfigFactory::sequential();
  33. self::assertSame(1, $config->getMaxProcesses());
  34. }
  35. /**
  36. * @see https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/pull/7777#discussion_r1591623367
  37. */
  38. public function testDetectConfigurationWithoutParams(): void
  39. {
  40. $this->mockCpuCount(7);
  41. $config = ParallelConfigFactory::detect();
  42. self::assertSame(6, $config->getMaxProcesses());
  43. self::assertSame(ParallelConfig::DEFAULT_FILES_PER_PROCESS, $config->getFilesPerProcess());
  44. self::assertSame(ParallelConfig::DEFAULT_PROCESS_TIMEOUT, $config->getProcessTimeout());
  45. }
  46. public function testDetectConfigurationWithParams(): void
  47. {
  48. $this->mockCpuCount(7);
  49. $config1 = ParallelConfigFactory::detect(22, 2_200, 5);
  50. self::assertSame(5, $config1->getMaxProcesses());
  51. self::assertSame(22, $config1->getFilesPerProcess());
  52. self::assertSame(2_200, $config1->getProcessTimeout());
  53. $config2 = ParallelConfigFactory::detect(22, 2_200, 6);
  54. self::assertSame(6, $config2->getMaxProcesses());
  55. $config3 = ParallelConfigFactory::detect(22, 2_200, 10);
  56. self::assertSame(6, $config3->getMaxProcesses());
  57. }
  58. public function testDetectConfigurationWithDefaultValue(): void
  59. {
  60. $config = ParallelConfigFactory::detect(null, 60);
  61. self::assertSame(ParallelConfig::DEFAULT_FILES_PER_PROCESS, $config->getFilesPerProcess());
  62. self::assertSame(60, $config->getProcessTimeout());
  63. }
  64. /**
  65. * @requires PHP 8.0
  66. */
  67. public function testDetectConfigurationWithNamedArgs(): void
  68. {
  69. $this->mockCpuCount(7);
  70. // First argument omitted, second one provided via named argument
  71. $config1 = \call_user_func_array([ParallelConfigFactory::class, 'detect'], ['processTimeout' => 300]);
  72. self::assertSame(ParallelConfig::DEFAULT_FILES_PER_PROCESS, $config1->getFilesPerProcess());
  73. self::assertSame(300, $config1->getProcessTimeout());
  74. // Flipped order of arguments using named arguments syntax
  75. $config2 = \call_user_func_array([ParallelConfigFactory::class, 'detect'], [
  76. 'maxProcesses' => 1,
  77. 'processTimeout' => 300,
  78. 'filesPerProcess' => 5,
  79. ]);
  80. self::assertSame(1, $config2->getMaxProcesses());
  81. self::assertSame(5, $config2->getFilesPerProcess());
  82. self::assertSame(300, $config2->getProcessTimeout());
  83. // Only first argument provided, but via named argument
  84. $config3 = \call_user_func_array([ParallelConfigFactory::class, 'detect'], ['filesPerProcess' => 7]);
  85. self::assertSame(7, $config3->getFilesPerProcess());
  86. self::assertSame(ParallelConfig::DEFAULT_PROCESS_TIMEOUT, $config3->getProcessTimeout());
  87. // Only third argument provided, but via named argument
  88. $config3 = \call_user_func_array([ParallelConfigFactory::class, 'detect'], ['maxProcesses' => 1]);
  89. self::assertSame(1, $config3->getMaxProcesses());
  90. self::assertSame(ParallelConfig::DEFAULT_FILES_PER_PROCESS, $config3->getFilesPerProcess());
  91. self::assertSame(ParallelConfig::DEFAULT_PROCESS_TIMEOUT, $config3->getProcessTimeout());
  92. }
  93. /**
  94. * @param null|positive-int $count
  95. */
  96. private function mockCpuCount(?int $count): void
  97. {
  98. \Closure::bind(static function () use ($count): void {
  99. ParallelConfigFactory::$cpuDetector = null !== $count ?
  100. new CpuCoreCounter([
  101. new DummyCpuCoreFinder($count),
  102. ]) : null;
  103. }, null, ParallelConfigFactory::class)();
  104. }
  105. }