Skip to content

Commit fc8219c

Browse files
committed
MCP-771: [MCP-608] Multiple consumers prototype development
- Update unit tests;
1 parent bee4cf2 commit fc8219c

File tree

3 files changed

+173
-21
lines changed

3 files changed

+173
-21
lines changed

app/code/Magento/MessageQueue/Model/Cron/ConsumersRunner.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,10 @@ public function __construct(
105105

106106
/**
107107
* Runs consumers processes
108+
*
109+
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
108110
*/
109-
public function run()
111+
public function run(): void
110112
{
111113
$runByCron = $this->deploymentConfig->get('cron_consumers_runner/cron_run', true);
112114
$multipleProcesses = $this->deploymentConfig->get('cron_consumers_runner/multiple_processes', []);

app/code/Magento/MessageQueue/Test/Unit/Console/StartConsumerCommandTest.php

Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -90,24 +90,27 @@ protected function setUp(): void
9090
*
9191
* @param string|null $pidFilePath
9292
* @param bool $singleThread
93+
* @param int|null $multiProcess
9394
* @param int $lockExpects
94-
* @param int $isLockedExpects
9595
* @param bool $isLocked
9696
* @param int $unlockExpects
9797
* @param int $runProcessExpects
9898
* @param int $expectedReturn
9999
* @return void
100+
* @throws \Exception
100101
* @dataProvider executeDataProvider
101102
*/
102103
public function testExecute(
103-
$pidFilePath,
104-
$singleThread,
105-
$lockExpects,
106-
$isLocked,
107-
$unlockExpects,
108-
$runProcessExpects,
109-
$expectedReturn
110-
) {
104+
?string $pidFilePath,
105+
bool $singleThread,
106+
?int $multiProcess,
107+
int $lockExpects,
108+
bool $isLocked,
109+
int $unlockExpects,
110+
int $runProcessExpects,
111+
int $expectedReturn
112+
): void
113+
{
111114
$areaCode = 'area_code';
112115
$numberOfMessages = 10;
113116
$batchSize = null;
@@ -121,19 +124,21 @@ public function testExecute(
121124
$input->expects($this->once())->method('getArgument')
122125
->with(StartConsumerCommand::ARGUMENT_CONSUMER)
123126
->willReturn($consumerName);
124-
$input->expects($this->exactly(5))->method('getOption')
127+
$input->expects($this->exactly(6))->method('getOption')
125128
->withConsecutive(
126129
[StartConsumerCommand::OPTION_NUMBER_OF_MESSAGES],
127130
[StartConsumerCommand::OPTION_BATCH_SIZE],
128131
[StartConsumerCommand::OPTION_AREACODE],
129132
[StartConsumerCommand::PID_FILE_PATH],
130-
[StartConsumerCommand::OPTION_SINGLE_THREAD]
133+
[StartConsumerCommand::OPTION_SINGLE_THREAD],
134+
[StartConsumerCommand::OPTION_MULTI_PROCESS]
131135
)->willReturnOnConsecutiveCalls(
132136
$numberOfMessages,
133137
$batchSize,
134138
$areaCode,
135139
$pidFilePath,
136-
$singleThread
140+
$singleThread,
141+
$multiProcess
137142
);
138143
$this->appState->expects($this->exactly($runProcessExpects))->method('setAreaCode')->with($areaCode);
139144
$consumer = $this->getMockBuilder(ConsumerInterface::class)
@@ -143,6 +148,10 @@ public function testExecute(
143148
->method('get')->with($consumerName, $batchSize)->willReturn($consumer);
144149
$consumer->expects($this->exactly($runProcessExpects))->method('process')->with($numberOfMessages);
145150

151+
if ($multiProcess !== null) {
152+
$consumerName .= '-' . $multiProcess;
153+
}
154+
146155
$this->lockManagerMock->expects($this->exactly($lockExpects))
147156
->method('lock')
148157
->with(md5($consumerName))//phpcs:ignore
@@ -161,12 +170,13 @@ public function testExecute(
161170
/**
162171
* @return array
163172
*/
164-
public function executeDataProvider()
173+
public function executeDataProvider(): array
165174
{
166175
return [
167176
[
168177
'pidFilePath' => null,
169178
'singleThread' => false,
179+
'multiProcess' => null,
170180
'lockExpects' => 0,
171181
'isLocked' => true,
172182
'unlockExpects' => 0,
@@ -176,6 +186,7 @@ public function executeDataProvider()
176186
[
177187
'pidFilePath' => '/var/consumer.pid',
178188
'singleThread' => true,
189+
'multiProcess' => null,
179190
'lockExpects' => 1,
180191
'isLocked' => true,
181192
'unlockExpects' => 1,
@@ -185,6 +196,27 @@ public function executeDataProvider()
185196
[
186197
'pidFilePath' => '/var/consumer.pid',
187198
'singleThread' => true,
199+
'multiProcess' => null,
200+
'lockExpects' => 1,
201+
'isLocked' => false,
202+
'unlockExpects' => 0,
203+
'runProcessExpects' => 0,
204+
'expectedReturn' => Cli::RETURN_FAILURE,
205+
],
206+
[
207+
'pidFilePath' => null,
208+
'singleThread' => false,
209+
'multiProcess' => 3,
210+
'lockExpects' => 1,
211+
'isLocked' => true,
212+
'unlockExpects' => 1,
213+
'runProcessExpects' => 1,
214+
'expectedReturn' => Cli::RETURN_SUCCESS,
215+
],
216+
[
217+
'pidFilePath' => null,
218+
'singleThread' => false,
219+
'multiProcess' => 3,
188220
'lockExpects' => 1,
189221
'isLocked' => false,
190222
'unlockExpects' => 0,

app/code/Magento/MessageQueue/Test/Unit/Model/Cron/ConsumersRunnerTest.php

Lines changed: 125 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -103,13 +103,12 @@ protected function setUp(): void
103103

104104
public function testRunDisabled()
105105
{
106-
$this->deploymentConfigMock->expects($this->once())
106+
$this->deploymentConfigMock->expects($this->atLeastOnce())
107107
->method('get')
108108
->willReturnMap(
109109
[
110110
['cron_consumers_runner/cron_run', true, false],
111-
['cron_consumers_runner/max_messages', 10000, 10000],
112-
['cron_consumers_runner/consumers', [], []],
111+
['cron_consumers_runner/multiple_processes', [], []],
113112
]
114113
);
115114

@@ -154,6 +153,7 @@ public function testRun(
154153
['cron_consumers_runner/max_messages', 10000, $maxMessages],
155154
['cron_consumers_runner/consumers', [], $allowedConsumers],
156155
['queue/only_spawn_when_message_available', null, 0],
156+
['cron_consumers_runner/multiple_processes', [], []]
157157
]
158158
);
159159

@@ -271,6 +271,125 @@ public function runDataProvider()
271271
];
272272
}
273273

274+
/**
275+
* @param int $maxMessages
276+
* @param array $isLocked
277+
* @param string $php
278+
* @param array $returnMap
279+
* @param array $allowedConsumers
280+
* @param int $shellBackgroundExpects
281+
* @dataProvider runMultiProcessesDataProvider
282+
*/
283+
public function testRunMultiProcesses(
284+
int $maxMessages,
285+
array $isLocked,
286+
string $php,
287+
array $returnMap,
288+
array $allowedConsumers,
289+
int $shellBackgroundExpects
290+
): void
291+
{
292+
$consumerName = 'consumerName';
293+
294+
$this->deploymentConfigMock
295+
->method('get')
296+
->willReturnMap(
297+
[
298+
['cron_consumers_runner/cron_run', true, true],
299+
['cron_consumers_runner/max_messages', 10000, $maxMessages],
300+
['cron_consumers_runner/consumers', [], $allowedConsumers],
301+
['queue/only_spawn_when_message_available', null, 0],
302+
['cron_consumers_runner/multiple_processes',
303+
[],
304+
['consumerName' => 2]
305+
]
306+
]
307+
);
308+
309+
/** @var ConsumerConfigInterface|MockObject $firstCunsumer */
310+
$consumer = $this->getMockBuilder(ConsumerConfigItemInterface::class)
311+
->getMockForAbstractClass();
312+
$consumer->method('getName')->willReturn($consumerName);
313+
314+
$this->phpExecutableFinderMock->expects($this->once())
315+
->method('find')
316+
->willReturn($php);
317+
318+
$this->consumerConfigMock->expects($this->once())
319+
->method('getConsumers')
320+
->willReturn([$consumer]);
321+
322+
$this->lockManagerMock->expects(self::exactly(2))
323+
->method('isLocked')
324+
->withConsecutive(
325+
[md5($consumerName . '-' . 1)], //phpcs:ignore
326+
[md5($consumerName . '-' . 2)] //phpcs:ignore
327+
)
328+
->willReturnOnConsecutiveCalls($isLocked[0], $isLocked[1]);
329+
330+
$this->shellBackgroundMock->expects(self::exactly($shellBackgroundExpects))
331+
->method('execute')
332+
->willReturnMap($returnMap);
333+
334+
$this->consumersRunner->run();
335+
}
336+
337+
/**
338+
* @return array
339+
*/
340+
public function runMultiProcessesDataProvider()
341+
{
342+
return [
343+
[
344+
'maxMessages' => 20000,
345+
'isLocked' => [false, false],
346+
'php' => '',
347+
'returnMap' => [
348+
[
349+
'php ' . BP . '/bin/magento queue:consumers:start %s %s %s',
350+
['consumerName', '--multi-process=1', '--max-messages=20000'],
351+
'value1'
352+
],
353+
[
354+
'php ' . BP . '/bin/magento queue:consumers:start %s %s %s',
355+
['consumerName', '--multi-process=2', '--max-messages=20000'],
356+
'value2'
357+
]
358+
],
359+
'allowedConsumers' => [],
360+
'shellBackgroundExpects' => 2
361+
],
362+
[
363+
'maxMessages' => 20000,
364+
'isLocked' => [true, false],
365+
'php' => '',
366+
'returnMap' => [
367+
[
368+
'php ' . BP . '/bin/magento queue:consumers:start %s %s %s',
369+
['consumerName', '--multi-process=2', '--max-messages=20000'],
370+
'value2'
371+
]
372+
],
373+
'allowedConsumers' => [],
374+
'shellBackgroundExpects' => 1
375+
],
376+
[
377+
'maxMessages' => 20000,
378+
'isLocked' => [true, true],
379+
'php' => '',
380+
'returnMap' => [
381+
[
382+
'php ' . BP . '/bin/magento queue:consumers:start %s %s %s',
383+
['consumerName', '--multi-process=2', '--max-messages=20000'],
384+
'value2'
385+
]
386+
],
387+
'allowedConsumers' => [],
388+
'shellBackgroundExpects' => 0
389+
],
390+
];
391+
}
392+
274393
/**
275394
* @param boolean $onlySpawnWhenMessageAvailable
276395
* @param boolean $isMassagesAvailableInTheQueue
@@ -291,14 +410,15 @@ public function testRunBasedOnOnlySpawnWhenMessageAvailableConsumerConfiguration
291410
$consumerName = 'consumerName';
292411
$connectionName = 'connectionName';
293412
$queueName = 'queueName';
294-
$this->deploymentConfigMock->expects($this->exactly(4))
413+
$this->deploymentConfigMock->expects($this->exactly(5))
295414
->method('get')
296415
->willReturnMap(
297416
[
298417
['cron_consumers_runner/cron_run', true, true],
299418
['cron_consumers_runner/max_messages', 10000, 1000],
300419
['cron_consumers_runner/consumers', [], []],
301420
['queue/only_spawn_when_message_available', true, $globalOnlySpawnWhenMessageAvailable],
421+
['cron_consumers_runner/multiple_processes', [], []]
302422
]
303423
);
304424

@@ -321,9 +441,7 @@ public function testRunBasedOnOnlySpawnWhenMessageAvailableConsumerConfiguration
321441
->method('find')
322442
->willReturn('');
323443

324-
$this->lockManagerMock->expects($this->once())
325-
->method('isLocked')
326-
->willReturn(false);
444+
$this->lockManagerMock->method('isLocked')->willReturn(false);
327445

328446
$this->checkIsAvailableMessagesMock->expects($this->exactly($isMassagesAvailableInTheQueueCallCount))
329447
->method('execute')

0 commit comments

Comments
 (0)