66namespace Magento \Framework \MessageQueue \UseCase ;
77
88use Magento \Framework \App \DeploymentConfig \FileReader ;
9+ use Magento \TestModuleAsyncAmqp \Model \AsyncTestData ;
10+ use Magento \Framework \App \DeploymentConfig \Writer ;
911use Magento \Framework \Config \File \ConfigFilePool ;
12+ use Magento \Framework \Filesystem ;
1013
1114class WaitAndNotWaitMessagesTest extends QueueTestCaseAbstract
1215{
@@ -15,25 +18,113 @@ class WaitAndNotWaitMessagesTest extends QueueTestCaseAbstract
1518 */
1619 private $ reader ;
1720
21+ /**
22+ * @var Filesystem
23+ */
24+ private $ filesystem ;
25+
26+ /**
27+ * @var ConfigFilePool
28+ */
29+ private $ configFilePool ;
30+
1831 /**
1932 * @var array
2033 */
2134 private $ config ;
2235
36+ /**
37+ * @var \Magento\TestModuleAsyncAmqp\Model\AsyncTestData
38+ */
39+ protected $ msgObject ;
40+
41+ /**
42+ * {@inheritdoc}
43+ */
44+ protected $ consumers = ['mixed.sync.and.async.queue.consumer ' ];
45+
46+ /**
47+ * @var string[]
48+ */
49+ protected $ messages = ['message1 ' , 'message2 ' , 'message3 ' ];
50+
51+ /**
52+ * @var int|null
53+ */
54+ protected $ maxMessages = 4 ;
55+
2356 /**
2457 * @inheritdoc
2558 */
2659 protected function setUp ()
2760 {
2861 parent ::setUp ();
62+ $ this ->msgObject = $ this ->objectManager ->create (AsyncTestData::class);
2963 $ this ->reader = $ this ->objectManager ->get (FileReader::class);
30-
64+ $ this -> filesystem = $ this -> objectManager -> get (Filesystem::class);
3165 $ this ->config = $ this ->loadConfig ();
3266 }
3367
34- public function testDefaultConfiguration ()
68+ /**
69+ * Check if consumers wait for messages from the queue
70+ */
71+ public function testWaitForMessages ()
3572 {
3673 $ this ->assertArraySubset (['queue ' => ['consumers_wait_for_messages ' => 1 ]], $ this ->config );
74+
75+ foreach ($ this ->messages as $ msg ) {
76+ $ this ->publishMessage ($ msg );
77+ }
78+
79+ $ this ->waitForAsynchronousResult (count ($ this ->messages ), $ this ->logFilePath );
80+
81+ foreach ($ this ->messages as $ item ) {
82+ $ this ->assertContains ($ item , file_get_contents ($ this ->logFilePath ));
83+ }
84+
85+ $ this ->publishMessage ('message4 ' );
86+ $ this ->waitForAsynchronousResult (count ($ this ->messages ) + 1 , $ this ->logFilePath );
87+ $ this ->assertContains ('message4 ' , file_get_contents ($ this ->logFilePath ));
88+ }
89+
90+ /**
91+ * Check if consumers do not wait for messages from the queue and die
92+ */
93+ public function testNotWaitForMessages (): void
94+ {
95+ $ this ->publisherConsumerController ->stopConsumers ();
96+
97+ $ config = $ this ->config ;
98+ $ config ['queue ' ]['consumers_wait_for_messages ' ] = 0 ;
99+ $ this ->writeConfig ($ config );
100+
101+ $ this ->assertArraySubset (['queue ' => ['consumers_wait_for_messages ' => 0 ]], $ this ->loadConfig ());
102+ foreach ($ this ->messages as $ msg ) {
103+ $ this ->publishMessage ($ msg );
104+ }
105+
106+ $ this ->publisherConsumerController ->startConsumers ();
107+ $ this ->waitForAsynchronousResult (count ($ this ->messages ), $ this ->logFilePath );
108+
109+ foreach ($ this ->messages as $ item ) {
110+ $ this ->assertContains ($ item , file_get_contents ($ this ->logFilePath ));
111+ }
112+
113+ // Checks that consumers do not wait 4th message and die
114+ $ this ->assertArraySubset (
115+ ['mixed.sync.and.async.queue.consumer ' => []],
116+ $ this ->publisherConsumerController ->getConsumersProcessIds ()
117+ );
118+ }
119+
120+ /**
121+ * @param string $msg
122+ */
123+ private function publishMessage (string $ msg ): void
124+ {
125+ $ this ->msgObject ->setValue ($ msg );
126+ $ this ->msgObject ->setTextFilePath ($ this ->logFilePath );
127+ $ this ->publisher ->publish ('multi.topic.queue.topic.c ' , $ this ->msgObject );
37128 }
38129
39130 /**
@@ -43,4 +134,22 @@ private function loadConfig(): array
43134 {
44135 return $ this ->reader ->load (ConfigFilePool::APP_ENV );
45136 }
137+
138+ /**
139+ * @param array $config
140+ */
141+ private function writeConfig (array $ config ): void
142+ {
143+ $ writer = $ this ->objectManager ->get (Writer::class);
144+ $ writer ->saveConfig ([ConfigFilePool::APP_ENV => $ config ], true );
145+ }
146+
147+ /**
148+ * @inheritdoc
149+ */
150+ protected function tearDown ()
151+ {
152+ parent ::tearDown ();
153+ $ this ->writeConfig ($ this ->config );
154+ }
46155}
0 commit comments