Skip to content

[Messenger] Fix float value for worker memory limit #60759

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ private function convertToBytes(string $memoryLimit): int
} elseif (str_starts_with($max, '0')) {
$max = \intval($max, 8);
} else {
$max = (int) $max;
$max = (float) $max;
}

switch (substr(rtrim($memoryLimit, 'b'), -1)) {
Expand All @@ -302,6 +302,6 @@ private function convertToBytes(string $memoryLimit): int
case 'k': $max *= 1024;
}

return $max;
return (int) $max;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
namespace Symfony\Component\Messenger\Tests\Command;

use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;
use Psr\Log\LoggerTrait;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Exception\InvalidOptionException;
use Symfony\Component\Console\Tester\CommandCompletionTester;
Expand Down Expand Up @@ -214,6 +216,50 @@ public function testRunWithTimeLimit()
$this->assertStringContainsString('[OK] Consuming messages from transport "dummy-receiver"', $tester->getDisplay());
}

public function testRunWithMemoryLimit()
{
$envelope = new Envelope(new \stdClass(), [new BusNameStamp('dummy-bus')]);

$receiver = $this->createMock(ReceiverInterface::class);
$receiver->method('get')->willReturn([$envelope]);

$receiverLocator = $this->createMock(ContainerInterface::class);
$receiverLocator->method('has')->with('dummy-receiver')->willReturn(true);
$receiverLocator->method('get')->with('dummy-receiver')->willReturn($receiver);

$bus = $this->createMock(MessageBusInterface::class);

$busLocator = $this->createMock(ContainerInterface::class);
$busLocator->method('has')->with('dummy-bus')->willReturn(true);
$busLocator->method('get')->with('dummy-bus')->willReturn($bus);

$logger = new class() implements LoggerInterface {
use LoggerTrait;

public array $logs = [];

public function log(...$args): void
{
$this->logs[] = $args;
}
};
$command = new ConsumeMessagesCommand(new RoutableMessageBus($busLocator), $receiverLocator, new EventDispatcher(), $logger);

$application = new Application();
$application->add($command);
$tester = new CommandTester($application->get('messenger:consume'));
$tester->execute([
'receivers' => ['dummy-receiver'],
'--memory-limit' => '1.5M',
]);

$this->assertSame(0, $tester->getStatusCode());
$this->assertStringContainsString('[OK] Consuming messages from transport "dummy-receiver"', $tester->getDisplay());
$this->assertStringContainsString('The worker will automatically exit once it has exceeded 1.5M of memory', $tester->getDisplay());

$this->assertSame(1572864, $logger->logs[1][2]['limit']);
}

/**
* @dataProvider provideCompletionSuggestions
*/
Expand Down
Loading