-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReloadConsumersCommandTest.php
More file actions
97 lines (81 loc) · 3.22 KB
/
Copy pathReloadConsumersCommandTest.php
File metadata and controls
97 lines (81 loc) · 3.22 KB
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
<?php
namespace Tests\Integration;
use ADT\BackgroundQueue\BackgroundQueue;
use ADT\BackgroundQueue\Broker\Producer;
use ADT\BackgroundQueue\Console\ReloadConsumersCommand;
use Codeception\Test\Unit;
use Doctrine\DBAL\DriverManager;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\NullOutput;
use Tests\Support\IntegrationTester;
/**
* Ověřuje, že reload-consumers pošle DIE zprávy do správných (label-specific) front
* a ve správném počtu - tedy jádro cíleného restartu konzumerů.
*/
class ReloadConsumersCommandTest extends Unit
{
protected IntegrationTester $tester;
/**
* Spustí executeCommand() přímo (obejde zámek z abstraktního Command) a vrátí
* seznam volání publishDie() ve tvaru [['queue' => ..., 'label' => ...], ...].
*/
private function runReload(array $input): array
{
$producer = new class implements Producer {
public array $dieCalls = [];
public function publish(string $id, string $queue, string $priority, ?int $expiration = null): void {}
public function publishDie(string $queue, ?string $consumerLabel = null): void
{
$this->dieCalls[] = ['queue' => $queue, 'label' => $consumerLabel];
}
public function publishShutdown(string $queue, ?string $consumerLabel = null): void {}
};
$backgroundQueue = new BackgroundQueue([
'queue' => 'general',
'priorities' => [10],
'connection' => DriverManager::getConnection(BackgroundQueue::parseDsn(self::getDsn())),
'logger' => null,
'producer' => $producer,
]);
$command = new ReloadConsumersCommand($backgroundQueue, $producer);
$arrayInput = new ArrayInput($input);
$arrayInput->bind($command->getDefinition());
$method = (new \ReflectionObject($command))->getMethod('executeCommand');
$method->setAccessible(true);
$method->invoke($command, $arrayInput, new NullOutput());
return $producer->dieCalls;
}
public function testWithoutLabelSendsToSharedQueue()
{
// Bez labelu se posílá NUMBER DIE zpráv do sdílené DIE fronty (label = null) - původní chování.
$calls = $this->runReload(['number' => 3]);
$this->tester->assertCount(3, $calls);
foreach ($calls as $call) {
$this->tester->assertSame('general', $call['queue']);
$this->tester->assertNull($call['label']);
}
}
public function testWithLabelsSendsNumberPerLabel()
{
// NUMBER zpráv na každý vyjmenovaný label - cílený restart konkrétních konzumerů.
$calls = $this->runReload(['number' => 2, '--label' => 'a,b']);
$this->tester->assertSame([
['queue' => 'general', 'label' => 'a'],
['queue' => 'general', 'label' => 'a'],
['queue' => 'general', 'label' => 'b'],
['queue' => 'general', 'label' => 'b'],
], $calls);
}
public function testRespectsNamedQueue()
{
// Volitelný argument queue rozšíří základní frontu (general -> general_myqueue).
$calls = $this->runReload(['number' => 1, 'queue' => 'myqueue', '--label' => 'x']);
$this->tester->assertSame([
['queue' => 'general_myqueue', 'label' => 'x'],
], $calls);
}
private static function getDsn(): string
{
return 'mysql://' . $_ENV['PROJECT_DB_USER'] . ':' . $_ENV['PROJECT_DB_PASSWORD'] . '@' . $_ENV['PROJECT_DB_HOST'] . ':' . $_ENV['PROJECT_DB_PORT'] . '/' . $_ENV['PROJECT_DB_DBNAME'];
}
}