ConsumersRunnerTest.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\MessageQueue\Model\Cron;
  7. use Magento\Framework\MessageQueue\Consumer\ConfigInterface as ConsumerConfigInterface;
  8. use Magento\MessageQueue\Model\Cron\ConsumersRunner\PidConsumerManager;
  9. use Magento\Framework\App\DeploymentConfig\FileReader;
  10. use Magento\Framework\App\DeploymentConfig\Writer;
  11. use Magento\Framework\Config\File\ConfigFilePool;
  12. use Magento\Framework\ShellInterface;
  13. use Magento\Framework\Filesystem;
  14. use Magento\Framework\App\Filesystem\DirectoryList;
  15. use Magento\Framework\App\Config\ReinitableConfigInterface;
  16. /**
  17. * Tests the different cases of consumers running by ConsumersRunner
  18. *
  19. * {@inheritdoc}
  20. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  21. */
  22. class ConsumersRunnerTest extends \PHPUnit\Framework\TestCase
  23. {
  24. /**
  25. * @var \Magento\Framework\ObjectManagerInterface
  26. */
  27. private $objectManager;
  28. /**
  29. * Consumer config provider
  30. *
  31. * @var ConsumerConfigInterface
  32. */
  33. private $consumerConfig;
  34. /**
  35. * @var PidConsumerManager
  36. */
  37. private $pid;
  38. /**
  39. * @var FileReader
  40. */
  41. private $reader;
  42. /**
  43. * @var ConsumersRunner
  44. */
  45. private $consumersRunner;
  46. /**
  47. * @var Filesystem
  48. */
  49. private $filesystem;
  50. /**
  51. * @var ConfigFilePool
  52. */
  53. private $configFilePool;
  54. /**
  55. * @var ReinitableConfigInterface
  56. */
  57. private $appConfig;
  58. /**
  59. * @var ShellInterface|\PHPUnit_Framework_MockObject_MockObject
  60. */
  61. private $shellMock;
  62. /**
  63. * @var array
  64. */
  65. private $config;
  66. /**
  67. * @inheritdoc
  68. */
  69. protected function setUp()
  70. {
  71. $this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
  72. $this->shellMock = $this->getMockBuilder(ShellInterface::class)
  73. ->getMockForAbstractClass();
  74. $this->pid = $this->objectManager->get(PidConsumerManager::class);
  75. $this->consumerConfig = $this->objectManager->get(ConsumerConfigInterface::class);
  76. $this->reader = $this->objectManager->get(FileReader::class);
  77. $this->filesystem = $this->objectManager->get(Filesystem::class);
  78. $this->configFilePool = $this->objectManager->get(ConfigFilePool::class);
  79. $this->appConfig = $this->objectManager->get(ReinitableConfigInterface::class);
  80. $this->consumersRunner = $this->objectManager->create(
  81. ConsumersRunner::class,
  82. ['shellBackground' => $this->shellMock]
  83. );
  84. $this->config = $this->loadConfig();
  85. $this->shellMock->expects($this->any())
  86. ->method('execute')
  87. ->willReturnCallback(function ($command, $arguments) {
  88. $command = vsprintf($command, $arguments);
  89. $params = \Magento\TestFramework\Helper\Bootstrap::getInstance()->getAppInitParams();
  90. $params['MAGE_DIRS']['base']['path'] = BP;
  91. $params = 'INTEGRATION_TEST_PARAMS="' . urldecode(http_build_query($params)) . '"';
  92. $command = str_replace('bin/magento', 'dev/tests/integration/bin/magento', $command);
  93. $command = $params . ' ' . $command;
  94. return exec("{$command} >/dev/null &");
  95. });
  96. }
  97. /**
  98. * Tests running of specific consumer and his re-running when it is working
  99. *
  100. * @return void
  101. */
  102. public function testSpecificConsumerAndRerun()
  103. {
  104. $specificConsumer = 'quoteItemCleaner';
  105. $pidFilePath = $this->getPidFileName($specificConsumer);
  106. $config = $this->config;
  107. $config['cron_consumers_runner'] = ['consumers' => [$specificConsumer], 'max_messages' => 0];
  108. $this->writeConfig($config);
  109. $this->reRunConsumersAndCheckPidFiles($specificConsumer);
  110. $pid = $this->pid->getPid($pidFilePath);
  111. $this->reRunConsumersAndCheckPidFiles($specificConsumer);
  112. $this->assertSame($pid, $this->pid->getPid($pidFilePath));
  113. }
  114. /**
  115. * @param string $specificConsumer
  116. * @return void
  117. */
  118. private function reRunConsumersAndCheckPidFiles($specificConsumer)
  119. {
  120. $this->consumersRunner->run();
  121. sleep(20);
  122. foreach ($this->consumerConfig->getConsumers() as $consumer) {
  123. $consumerName = $consumer->getName();
  124. $pidFileFullPath = $this->getPidFileFullPath($consumerName);
  125. if ($consumerName === $specificConsumer) {
  126. $this->assertTrue(file_exists($pidFileFullPath));
  127. } else {
  128. $this->assertFalse(file_exists($pidFileFullPath));
  129. }
  130. }
  131. }
  132. /**
  133. * Tests disabling cron job which runs consumers
  134. *
  135. * @return void
  136. */
  137. public function testCronJobDisabled()
  138. {
  139. $config = $this->config;
  140. $config['cron_consumers_runner'] = ['cron_run' => false];
  141. $this->writeConfig($config);
  142. $this->consumersRunner->run();
  143. sleep(20);
  144. foreach ($this->consumerConfig->getConsumers() as $consumer) {
  145. $pidFileFullPath = $this->getPidFileFullPath($consumer->getName());
  146. $this->assertFalse(file_exists($pidFileFullPath));
  147. }
  148. }
  149. /**
  150. * @return array
  151. */
  152. private function loadConfig()
  153. {
  154. return $this->reader->load(ConfigFilePool::APP_ENV);
  155. }
  156. /**
  157. * @param array $config
  158. * @return void
  159. */
  160. private function writeConfig(array $config)
  161. {
  162. /** @var Writer $writer */
  163. $writer = $this->objectManager->get(Writer::class);
  164. $writer->saveConfig([ConfigFilePool::APP_ENV => $config]);
  165. }
  166. /**
  167. * @param string $consumerName
  168. * @return string
  169. */
  170. private function getPidFileFullPath($consumerName)
  171. {
  172. $directoryList = $this->objectManager->get(DirectoryList::class);
  173. return $directoryList->getPath(DirectoryList::VAR_DIR) . '/' . $this->getPidFileName($consumerName);
  174. }
  175. /**
  176. * @inheritdoc
  177. */
  178. protected function tearDown()
  179. {
  180. foreach ($this->consumerConfig->getConsumers() as $consumer) {
  181. $consumerName = $consumer->getName();
  182. $pidFileFullPath = $this->getPidFileFullPath($consumerName);
  183. $pidFilePath = $this->getPidFileName($consumerName);
  184. $pid = $this->pid->getPid($pidFilePath);
  185. if ($pid && $this->pid->isRun($pidFilePath)) {
  186. posix_kill($pid, SIGKILL);
  187. }
  188. if (file_exists($pidFileFullPath)) {
  189. unlink($pidFileFullPath);
  190. }
  191. }
  192. $this->filesystem->getDirectoryWrite(DirectoryList::CONFIG)->writeFile(
  193. $this->configFilePool->getPath(ConfigFilePool::APP_ENV),
  194. "<?php\n return array();\n"
  195. );
  196. $this->writeConfig($this->config);
  197. $this->appConfig->reinit();
  198. }
  199. /**
  200. * @param string $consumerName The consumers name
  201. * @return string The name to file with PID
  202. */
  203. private function getPidFileName($consumerName)
  204. {
  205. $sanitizedHostname = preg_replace('/[^a-z0-9]/i', '', gethostname());
  206. return $consumerName . '-' . $sanitizedHostname . ConsumersRunner::PID_FILE_EXT;
  207. }
  208. }