SignalRegistryTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Console\Tests\SignalRegistry;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\Console\SignalRegistry\SignalRegistry;
  13. /**
  14. * @requires extension pcntl
  15. */
  16. class SignalRegistryTest extends TestCase
  17. {
  18. protected function tearDown(): void
  19. {
  20. pcntl_async_signals(false);
  21. pcntl_signal(\SIGUSR1, \SIG_DFL);
  22. pcntl_signal(\SIGUSR2, \SIG_DFL);
  23. }
  24. public function testOneCallbackForASignalSignalIsHandled()
  25. {
  26. $signalRegistry = new SignalRegistry();
  27. $isHandled = false;
  28. $signalRegistry->register(\SIGUSR1, function () use (&$isHandled) {
  29. $isHandled = true;
  30. });
  31. posix_kill(posix_getpid(), \SIGUSR1);
  32. $this->assertTrue($isHandled);
  33. }
  34. public function testTwoCallbacksForASignalBothCallbacksAreCalled()
  35. {
  36. $signalRegistry = new SignalRegistry();
  37. $isHandled1 = false;
  38. $signalRegistry->register(\SIGUSR1, function () use (&$isHandled1) {
  39. $isHandled1 = true;
  40. });
  41. $isHandled2 = false;
  42. $signalRegistry->register(\SIGUSR1, function () use (&$isHandled2) {
  43. $isHandled2 = true;
  44. });
  45. posix_kill(posix_getpid(), \SIGUSR1);
  46. $this->assertTrue($isHandled1);
  47. $this->assertTrue($isHandled2);
  48. }
  49. public function testTwoSignalsSignalsAreHandled()
  50. {
  51. $signalRegistry = new SignalRegistry();
  52. $isHandled1 = false;
  53. $isHandled2 = false;
  54. $signalRegistry->register(\SIGUSR1, function () use (&$isHandled1) {
  55. $isHandled1 = true;
  56. });
  57. posix_kill(posix_getpid(), \SIGUSR1);
  58. $this->assertTrue($isHandled1);
  59. $this->assertFalse($isHandled2);
  60. $signalRegistry->register(\SIGUSR2, function () use (&$isHandled2) {
  61. $isHandled2 = true;
  62. });
  63. posix_kill(posix_getpid(), \SIGUSR2);
  64. $this->assertTrue($isHandled2);
  65. }
  66. public function testTwoCallbacksForASignalPreviousAndRegisteredCallbacksWereCalled()
  67. {
  68. $signalRegistry = new SignalRegistry();
  69. $isHandled1 = false;
  70. pcntl_signal(\SIGUSR1, function () use (&$isHandled1) {
  71. $isHandled1 = true;
  72. });
  73. $isHandled2 = false;
  74. $signalRegistry->register(\SIGUSR1, function () use (&$isHandled2) {
  75. $isHandled2 = true;
  76. });
  77. posix_kill(posix_getpid(), \SIGUSR1);
  78. $this->assertTrue($isHandled1);
  79. $this->assertTrue($isHandled2);
  80. }
  81. public function testTwoCallbacksForASignalPreviousCallbackFromAnotherRegistry()
  82. {
  83. $signalRegistry1 = new SignalRegistry();
  84. $isHandled1 = false;
  85. $signalRegistry1->register(\SIGUSR1, function () use (&$isHandled1) {
  86. $isHandled1 = true;
  87. });
  88. $signalRegistry2 = new SignalRegistry();
  89. $isHandled2 = false;
  90. $signalRegistry2->register(\SIGUSR1, function () use (&$isHandled2) {
  91. $isHandled2 = true;
  92. });
  93. posix_kill(posix_getpid(), \SIGUSR1);
  94. $this->assertTrue($isHandled1);
  95. $this->assertTrue($isHandled2);
  96. }
  97. }