6
6
namespace Magento \Framework \MessageQueue \UseCase ;
7
7
8
8
use Magento \Framework \App \DeploymentConfig \FileReader ;
9
+ use Magento \TestModuleAsyncAmqp \Model \AsyncTestData ;
10
+ use Magento \Framework \App \DeploymentConfig \Writer ;
9
11
use Magento \Framework \Config \File \ConfigFilePool ;
12
+ use Magento \Framework \Filesystem ;
10
13
11
14
class WaitAndNotWaitMessagesTest extends QueueTestCaseAbstract
12
15
{
@@ -15,25 +18,113 @@ class WaitAndNotWaitMessagesTest extends QueueTestCaseAbstract
15
18
*/
16
19
private $ reader ;
17
20
21
+ /**
22
+ * @var Filesystem
23
+ */
24
+ private $ filesystem ;
25
+
26
+ /**
27
+ * @var ConfigFilePool
28
+ */
29
+ private $ configFilePool ;
30
+
18
31
/**
19
32
* @var array
20
33
*/
21
34
private $ config ;
22
35
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
+
23
56
/**
24
57
* @inheritdoc
25
58
*/
26
59
protected function setUp ()
27
60
{
28
61
parent ::setUp ();
62
+ $ this ->msgObject = $ this ->objectManager ->create (AsyncTestData::class);
29
63
$ this ->reader = $ this ->objectManager ->get (FileReader::class);
30
-
64
+ $ this -> filesystem = $ this -> objectManager -> get (Filesystem::class);
31
65
$ this ->config = $ this ->loadConfig ();
32
66
}
33
67
34
- public function testDefaultConfiguration ()
68
+ /**
69
+ * Check if consumers wait for messages from the queue
70
+ */
71
+ public function testWaitForMessages ()
35
72
{
36
73
$ 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 );
37
128
}
38
129
39
130
/**
@@ -43,4 +134,22 @@ private function loadConfig(): array
43
134
{
44
135
return $ this ->reader ->load (ConfigFilePool::APP_ENV );
45
136
}
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
+ }
46
155
}
0 commit comments