|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright 2025 Adobe |
| 4 | + * All Rights Reserved. |
| 5 | + */ |
| 6 | +namespace Magento\Framework\Stomp; |
| 7 | + |
| 8 | +use Magento\Framework\App\DeploymentConfig\FileReader; |
| 9 | +use Magento\Framework\App\DeploymentConfig\Writer; |
| 10 | +use Magento\Framework\Config\File\ConfigFilePool; |
| 11 | +use Magento\Framework\Filesystem; |
| 12 | +use Magento\Framework\MessageQueue\DefaultValueProvider; |
| 13 | +use Magento\Framework\MessageQueue\UseCase\QueueTestCaseAbstract; |
| 14 | +use Magento\TestFramework\Helper\Bootstrap; |
| 15 | +use Magento\TestModuleAsyncStomp\Model\AsyncTestData; |
| 16 | + |
| 17 | +class WaitAndNotWaitMessagesTest extends QueueTestCaseAbstract |
| 18 | +{ |
| 19 | + /** |
| 20 | + * @var FileReader |
| 21 | + */ |
| 22 | + private $reader; |
| 23 | + |
| 24 | + /** |
| 25 | + * @var Filesystem |
| 26 | + */ |
| 27 | + private $filesystem; |
| 28 | + |
| 29 | + /** |
| 30 | + * @var array |
| 31 | + */ |
| 32 | + private $config; |
| 33 | + |
| 34 | + /** |
| 35 | + * @var AsyncTestData |
| 36 | + */ |
| 37 | + protected $msgObject; |
| 38 | + |
| 39 | + /** |
| 40 | + * @var array |
| 41 | + */ |
| 42 | + protected $consumers = ['stomp.mixed.sync.and.async.queue.consumer']; |
| 43 | + |
| 44 | + /** |
| 45 | + * @var string[] |
| 46 | + */ |
| 47 | + protected $messages = ['message1', 'message2', 'message3']; |
| 48 | + |
| 49 | + /** |
| 50 | + * @var int|null |
| 51 | + */ |
| 52 | + protected $maxMessages = 4; |
| 53 | + |
| 54 | + /** |
| 55 | + * @var string |
| 56 | + */ |
| 57 | + private $connectionType; |
| 58 | + |
| 59 | + /** |
| 60 | + * @inheritdoc |
| 61 | + */ |
| 62 | + protected function setUp(): void |
| 63 | + { |
| 64 | + $this->objectManager = Bootstrap::getObjectManager(); |
| 65 | + /** @var DefaultValueProvider $defaultValueProvider */ |
| 66 | + $defaultValueProvider = $this->objectManager->get(DefaultValueProvider::class); |
| 67 | + $this->connectionType = $defaultValueProvider->getConnection(); |
| 68 | + |
| 69 | + if ($this->connectionType === 'stomp') { |
| 70 | + parent::setUp(); |
| 71 | + $this->reader = $this->objectManager->get(FileReader::class); |
| 72 | + $this->filesystem = $this->objectManager->get(Filesystem::class); |
| 73 | + $this->config = $this->loadConfig(); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Get message object, creating it lazily |
| 79 | + * |
| 80 | + * @return AsyncTestData |
| 81 | + */ |
| 82 | + private function getMsgObject(): AsyncTestData |
| 83 | + { |
| 84 | + if (!$this->msgObject) { |
| 85 | + // phpstan:ignore "Class Magento\TestModuleAsyncStomp\Model\AsyncTestData not found." |
| 86 | + $this->msgObject = $this->objectManager->create(AsyncTestData::class); |
| 87 | + } |
| 88 | + return $this->msgObject; |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Check if consumers wait for messages from the queue |
| 93 | + */ |
| 94 | + public function testWaitForMessages() |
| 95 | + { |
| 96 | + if ($this->connectionType === 'amqp') { |
| 97 | + $this->markTestSkipped('STOMP test skipped because AMQP connection is available. |
| 98 | + This test is STOMP-specific.'); |
| 99 | + } |
| 100 | + |
| 101 | + $this->publisherConsumerController->stopConsumers(); |
| 102 | + |
| 103 | + $config = $this->config; |
| 104 | + $config['queue']['consumers_wait_for_messages'] = 1; |
| 105 | + $this->writeConfig($config); |
| 106 | + |
| 107 | + $loadedConfig = $this->loadConfig(); |
| 108 | + $this->assertArrayHasKey('queue', $loadedConfig); |
| 109 | + $this->assertArrayHasKey('consumers_wait_for_messages', $loadedConfig['queue']); |
| 110 | + $this->assertEquals(1, $loadedConfig['queue']['consumers_wait_for_messages']); |
| 111 | + |
| 112 | + foreach ($this->messages as $message) { |
| 113 | + $this->publishMessage($message); |
| 114 | + } |
| 115 | + $this->publisherConsumerController->startConsumers(); |
| 116 | + $this->waitForAsynchronousResult(count($this->messages), $this->logFilePath); |
| 117 | + |
| 118 | + foreach ($this->messages as $item) { |
| 119 | + $this->assertStringContainsString($item, file_get_contents($this->logFilePath)); |
| 120 | + } |
| 121 | + |
| 122 | + $this->publishMessage('message4'); |
| 123 | + $this->waitForAsynchronousResult(count($this->messages) + 1, $this->logFilePath); |
| 124 | + $this->assertStringContainsString('message4', file_get_contents($this->logFilePath)); |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Check if consumers do not wait for messages from the queue and die |
| 129 | + */ |
| 130 | + public function testNotWaitForMessages(): void |
| 131 | + { |
| 132 | + if ($this->connectionType !== 'stomp') { |
| 133 | + $this->markTestSkipped('STOMP test skipped because AMQP connection is available. |
| 134 | + This test is STOMP-specific.'); |
| 135 | + } |
| 136 | + |
| 137 | + $this->publisherConsumerController->stopConsumers(); |
| 138 | + |
| 139 | + $config = $this->config; |
| 140 | + $config['queue']['consumers_wait_for_messages'] = 0; |
| 141 | + $this->writeConfig($config); |
| 142 | + |
| 143 | + $loadedConfig = $this->loadConfig(); |
| 144 | + $this->assertArrayHasKey('queue', $loadedConfig); |
| 145 | + $this->assertArrayHasKey('consumers_wait_for_messages', $loadedConfig['queue']); |
| 146 | + $this->assertEquals(0, $loadedConfig['queue']['consumers_wait_for_messages']); |
| 147 | + foreach ($this->messages as $message) { |
| 148 | + $this->publishMessage($message); |
| 149 | + } |
| 150 | + |
| 151 | + $this->publisherConsumerController->startConsumers(); |
| 152 | + sleep(5); |
| 153 | + $this->waitForAsynchronousResult(count($this->messages), $this->logFilePath); |
| 154 | + |
| 155 | + foreach ($this->messages as $item) { |
| 156 | + $this->assertStringContainsString($item, file_get_contents($this->logFilePath)); |
| 157 | + } |
| 158 | + |
| 159 | + // Checks that consumers do not wait 4th message and die |
| 160 | + $consumersProcessIds = $this->publisherConsumerController->getConsumersProcessIds(); |
| 161 | + $this->assertArrayHasKey('stomp.mixed.sync.and.async.queue.consumer', $consumersProcessIds); |
| 162 | + $this->assertEquals([], $consumersProcessIds['stomp.mixed.sync.and.async.queue.consumer']); |
| 163 | + } |
| 164 | + |
| 165 | + /** |
| 166 | + * @param string $message |
| 167 | + */ |
| 168 | + private function publishMessage(string $message): void |
| 169 | + { |
| 170 | + $this->getMsgObject()->setValue($message); |
| 171 | + $this->getMsgObject()->setTextFilePath($this->logFilePath); |
| 172 | + $this->publisher->publish('stomp.multi.topic.queue.topic.c', $this->getMsgObject()); |
| 173 | + } |
| 174 | + |
| 175 | + /** |
| 176 | + * @return array |
| 177 | + */ |
| 178 | + private function loadConfig(): array |
| 179 | + { |
| 180 | + return $this->reader->load(ConfigFilePool::APP_ENV); |
| 181 | + } |
| 182 | + |
| 183 | + /** |
| 184 | + * @param array $config |
| 185 | + */ |
| 186 | + private function writeConfig(array $config): void |
| 187 | + { |
| 188 | + $writer = $this->objectManager->get(Writer::class); |
| 189 | + $writer->saveConfig([ConfigFilePool::APP_ENV => $config], true); |
| 190 | + } |
| 191 | + |
| 192 | + /** |
| 193 | + * @inheritdoc |
| 194 | + */ |
| 195 | + protected function tearDown(): void |
| 196 | + { |
| 197 | + parent::tearDown(); |
| 198 | + $this->writeConfig($this->config); |
| 199 | + } |
| 200 | +} |
0 commit comments