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