1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\TestFramework\MessageQueue;
use Magento\Framework\MessageQueue\PublisherInterface;
use Magento\Framework\OsInfo;
use Magento\TestFramework\Helper\Amqp;
/**
* Publisher Consumer Controller
*/
class PublisherConsumerController
{
/**
* @var string[]
*/
private $consumers = [];
/**
* @var PublisherInterface
*/
private $publisher;
/**
* @var string
*/
private $logFilePath;
/**
* @var int|null
*/
private $maxMessages = null;
/**
* @var OsInfo
*/
private $osInfo;
/**
* @var array
*/
private $appInitParams;
/**
* @var Amqp
*/
private $amqpHelper;
/**
* PublisherConsumerController constructor.
* @param PublisherInterface $publisher
* @param OsInfo $osInfo
* @param Amqp $amqpHelper
* @param string $logFilePath
* @param array $consumers
* @param array $appInitParams
* @param null|int $maxMessages
*/
public function __construct(
PublisherInterface $publisher,
OsInfo $osInfo,
Amqp $amqpHelper,
$logFilePath,
$consumers,
$appInitParams,
$maxMessages = null
) {
$this->consumers = $consumers;
$this->publisher = $publisher;
$this->logFilePath = $logFilePath;
$this->maxMessages = $maxMessages;
$this->osInfo = $osInfo;
$this->appInitParams = $appInitParams;
$this->amqpHelper = $amqpHelper;
}
/**
* Initialize Environment and Consumers
*
* @throws EnvironmentPreconditionException
* @throws PreconditionFailedException
*/
public function initialize()
{
$this->validateEnvironmentPreconditions();
$connections = $this->amqpHelper->getConnections();
foreach (array_keys($connections) as $connectionName) {
$this->amqpHelper->deleteConnection($connectionName);
}
$this->amqpHelper->clearQueue("async.operations.all");
foreach ($this->consumers as $consumer) {
foreach ($this->getConsumerProcessIds($consumer) as $consumerProcessId) {
exec("kill {$consumerProcessId}");
}
}
foreach ($this->consumers as $consumer) {
if (!$this->getConsumerProcessIds($consumer)) {
exec("{$this->getConsumerStartCommand($consumer, true)} > /dev/null &");
}
sleep(5);
}
if (file_exists($this->logFilePath)) {
// try to remove before failing the test
unlink($this->logFilePath);
if (file_exists($this->logFilePath)) {
throw new PreconditionFailedException(
"Precondition failed: test log ({$this->logFilePath}) cannot be deleted before test execution."
);
}
}
}
/**
* Validate environment preconditions
*
* @throws EnvironmentPreconditionException
* @throws PreconditionFailedException
*/
private function validateEnvironmentPreconditions()
{
if ($this->osInfo->isWindows()) {
throw new EnvironmentPreconditionException(
"This test relies on *nix shell and should be skipped in Windows environment."
);
}
if (!$this->amqpHelper->isAvailable()) {
throw new PreconditionFailedException(
'This test relies on RabbitMQ Management Plugin.'
);
}
}
/**
* Stop Consumers
*/
public function stopConsumers()
{
foreach ($this->consumers as $consumer) {
foreach ($this->getConsumerProcessIds($consumer) as $consumerProcessId) {
exec("kill {$consumerProcessId}");
}
}
}
/**
* Get Consumers ProcessIds
*
* @return array
*/
public function getConsumersProcessIds()
{
$consumers = [];
foreach ($this->consumers as $consumer) {
$consumers[$consumer] = $this->getConsumerProcessIds($consumer);
}
return $consumers;
}
/**
* Get Consumer ProcessIds
*
* @param string $consumer
* @return string[]
*/
private function getConsumerProcessIds($consumer)
{
exec("ps ax | grep -v grep | grep '{$this->getConsumerStartCommand($consumer)}' | awk '{print $1}'", $output);
return $output;
}
/**
* Get CLI command for starting specified consumer.
*
* @param string $consumer
* @param bool $withEnvVariables
* @return string
*/
private function getConsumerStartCommand($consumer, $withEnvVariables = false)
{
$binDirectory = realpath(INTEGRATION_TESTS_DIR . '/bin/');
$magentoCli = $binDirectory . '/magento';
$consumerStartCommand = "php {$magentoCli} queue:consumers:start -vvv " . $consumer;
if ($this->maxMessages) {
$consumerStartCommand .= " --max-messages={$this->maxMessages}";
}
if ($withEnvVariables) {
$params = $this->appInitParams;
$params['MAGE_DIRS']['base']['path'] = BP;
$params = 'INTEGRATION_TEST_PARAMS="' . urldecode(http_build_query($params)) . '"';
$consumerStartCommand = $params . ' ' . $consumerStartCommand;
}
return $consumerStartCommand;
}
/**
* Wait for asynchronous result
*
* @param callable $condition
* @param array $params
* @throws PreconditionFailedException
*/
public function waitForAsynchronousResult(callable $condition, $params)
{
$i = 0;
do {
sleep(1);
$assertion = call_user_func_array($condition, $params);
} while (!$assertion && ($i++ < 180));
if (!$assertion) {
throw new PreconditionFailedException("No asynchronous messages were processed.");
}
}
/**
* Get publisher
*
* @return PublisherInterface
*/
public function getPublisher()
{
return $this->publisher;
}
}