ProcessPoolTest.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 PhpCsFixer\Console\Command\FixCommand;
  14. use PhpCsFixer\Runner\Parallel\ParallelConfigFactory;
  15. use PhpCsFixer\Runner\Parallel\ParallelisationException;
  16. use PhpCsFixer\Runner\Parallel\Process;
  17. use PhpCsFixer\Runner\Parallel\ProcessFactory;
  18. use PhpCsFixer\Runner\Parallel\ProcessIdentifier;
  19. use PhpCsFixer\Runner\Parallel\ProcessPool;
  20. use PhpCsFixer\Runner\RunnerConfig;
  21. use PhpCsFixer\Tests\TestCase;
  22. use PhpCsFixer\ToolInfo;
  23. use React\EventLoop\StreamSelectLoop;
  24. use React\Socket\ServerInterface;
  25. use Symfony\Component\Console\Application;
  26. use Symfony\Component\Console\Input\ArrayInput;
  27. /**
  28. * @internal
  29. *
  30. * @covers \PhpCsFixer\Runner\Parallel\ProcessPool
  31. */
  32. final class ProcessPoolTest extends TestCase
  33. {
  34. public bool $serverClosed = false;
  35. private ProcessFactory $processFactory;
  36. protected function setUp(): void
  37. {
  38. $fixCommand = new FixCommand(new ToolInfo());
  39. $application = new Application();
  40. $application->add($fixCommand);
  41. // In order to have full list of options supported by the command (e.g. `--verbose`)
  42. $fixCommand->mergeApplicationDefinition(false);
  43. $this->processFactory = new ProcessFactory(new ArrayInput([], $fixCommand->getDefinition()));
  44. }
  45. public function testGetProcessWithInvalidIdentifier(): void
  46. {
  47. self::expectException(ParallelisationException::class);
  48. $this->getProcessPool()->getProcess(ProcessIdentifier::create());
  49. }
  50. public function testGetProcessWithValidIdentifier(): void
  51. {
  52. $identifier = ProcessIdentifier::create();
  53. $processPool = $this->getProcessPool();
  54. $process = $this->createProcess($identifier);
  55. $processPool->addProcess($identifier, $process);
  56. self::assertSame($process, $processPool->getProcess($identifier));
  57. }
  58. public function testEndProcessIfKnownWithUnknownIdentifier(): void
  59. {
  60. $identifier1 = ProcessIdentifier::create();
  61. $identifier2 = ProcessIdentifier::create();
  62. $processPool = $this->getProcessPool();
  63. $process = $this->createProcess($identifier1);
  64. $processPool->addProcess($identifier1, $process);
  65. // This is unregistered process, so it does nothing
  66. $processPool->endProcessIfKnown($identifier2);
  67. self::assertFalse($this->serverClosed);
  68. }
  69. public function testEndProcessIfKnownWithKnownIdentifier(): void
  70. {
  71. $identifier = ProcessIdentifier::create();
  72. $processPool = $this->getProcessPool();
  73. $process = $this->createProcess($identifier);
  74. $processPool->addProcess($identifier, $process);
  75. $processPool->endProcessIfKnown($identifier);
  76. self::assertTrue($this->serverClosed);
  77. }
  78. public function testEndAll(): void
  79. {
  80. $callbackExecuted = false;
  81. $processPool = $this->getProcessPool(static function () use (&$callbackExecuted): void {
  82. $callbackExecuted = true;
  83. });
  84. $identifier1 = ProcessIdentifier::create();
  85. $process1 = $this->createProcess($identifier1);
  86. $processPool->addProcess($identifier1, $process1);
  87. $identifier2 = ProcessIdentifier::create();
  88. $process2 = $this->createProcess($identifier2);
  89. $processPool->addProcess($identifier2, $process2);
  90. $processPool->endAll();
  91. self::assertTrue($this->serverClosed);
  92. self::assertTrue($callbackExecuted, 'Callback was not executed on server close.');
  93. }
  94. private function createProcess(ProcessIdentifier $identifier): Process
  95. {
  96. return $this->processFactory->create(
  97. new StreamSelectLoop(),
  98. new RunnerConfig(
  99. true,
  100. false,
  101. ParallelConfigFactory::sequential()
  102. ),
  103. $identifier,
  104. 10_000
  105. );
  106. }
  107. private function getProcessPool(?callable $onServerClose = null): ProcessPool
  108. {
  109. $this->serverClosed = false;
  110. $test = $this;
  111. return new ProcessPool(
  112. new class($test) implements ServerInterface {
  113. private ProcessPoolTest $test;
  114. public function __construct(ProcessPoolTest $test)
  115. {
  116. $this->test = $test;
  117. }
  118. public function close(): void
  119. {
  120. $this->test->serverClosed = true;
  121. }
  122. public function getAddress(): ?string
  123. {
  124. return null;
  125. }
  126. public function pause(): void {}
  127. public function resume(): void {}
  128. /** @phpstan-ignore-next-line */
  129. public function on($event, $listener): void {}
  130. /** @phpstan-ignore-next-line */
  131. public function once($event, $listener): void {}
  132. /** @phpstan-ignore-next-line */
  133. public function removeListener($event, $listener): void {}
  134. /** @phpstan-ignore-next-line */
  135. public function removeAllListeners($event = null): void {}
  136. /** @phpstan-ignore-next-line */
  137. public function listeners($event = null): void {}
  138. /** @phpstan-ignore-next-line */
  139. public function emit($event, array $arguments = []): void {}
  140. },
  141. $onServerClose
  142. );
  143. }
  144. }