Skip to content
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

Fix isRamAlmostOverloaded function in Consumer #435

Merged
merged 1 commit into from
Jun 21, 2017
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
2 changes: 1 addition & 1 deletion Command/BaseConsumerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ protected function configure()
->addArgument('name', InputArgument::REQUIRED, 'Consumer Name')
->addOption('messages', 'm', InputOption::VALUE_OPTIONAL, 'Messages to consume', 0)
->addOption('route', 'r', InputOption::VALUE_OPTIONAL, 'Routing Key', '')
->addOption('memory-limit', 'l', InputOption::VALUE_OPTIONAL, 'Allowed memory for this process', null)
->addOption('memory-limit', 'l', InputOption::VALUE_OPTIONAL, 'Allowed memory for this process (MB)', null)
->addOption('debug', 'd', InputOption::VALUE_NONE, 'Enable Debugging')
->addOption('without-signals', 'w', InputOption::VALUE_NONE, 'Disable catching of system signals')
;
Expand Down
2 changes: 1 addition & 1 deletion MemoryChecker/MemoryConsumptionChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function __construct(NativeMemoryUsageProvider $memoryUsageProvider) {
*
* @return bool
*/
public function isRamAlmostOverloaded($allowedConsumptionUntil, $maxConsumptionAllowed)
public function isRamAlmostOverloaded($maxConsumptionAllowed, $allowedConsumptionUntil = 0)
{
$allowedConsumptionUntil = $this->convertHumanUnitToNumerical($allowedConsumptionUntil);
$maxConsumptionAllowed = $this->convertHumanUnitToNumerical($maxConsumptionAllowed);
Expand Down
2 changes: 1 addition & 1 deletion RabbitMq/Consumer.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,6 @@ protected function isRamAlmostOverloaded()
{
$memoryManager = new MemoryConsumptionChecker(new NativeMemoryUsageProvider());

return $memoryManager->isRamAlmostOverloaded($this->getMemoryLimit(), '5M');
return $memoryManager->isRamAlmostOverloaded($this->getMemoryLimit().'M', '5M');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to check & adjust if already set memory limit is not in such format.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This check is already done by convertHumanUnitToNumerical() method.

Just a suggestion:
Alter/better documentation in the ReadMe
Its unclear the memory-limit / -l option is not in MB's by default.

The example -l 256 is 256 bytes and not 256 mega bytes.
so for reference -l=256M will be the correct notation or -l=268435456

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@adm-bome Ok, I'll do it as soon as I have time. Thanks/

}
}
30 changes: 28 additions & 2 deletions Tests/Manager/MemoryCheckerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function testMemoryIsNotAlmostOverloaded()

$memoryManager = new MemoryConsumptionChecker($memoryUsageProvider);

$this->assertFalse($memoryManager->isRamAlmostOverloaded($allowedConsumptionUntil, $maxConsumptionAllowed));
$this->assertFalse($memoryManager->isRamAlmostOverloaded($maxConsumptionAllowed, $allowedConsumptionUntil));
}

public function testMemoryIsAlmostOverloaded()
Expand All @@ -37,6 +37,32 @@ public function testMemoryIsAlmostOverloaded()

$memoryManager = new MemoryConsumptionChecker($memoryUsageProvider);

$this->assertTrue($memoryManager->isRamAlmostOverloaded($allowedConsumptionUntil, $maxConsumptionAllowed));
$this->assertTrue($memoryManager->isRamAlmostOverloaded($maxConsumptionAllowed, $allowedConsumptionUntil));
}

public function testMemoryExactValueIsNotAlmostOverloaded()
{
$currentMemoryUsage = '7M';
$maxConsumptionAllowed = '10M';

$memoryUsageProvider = $this->getMockBuilder('OldSound\\RabbitMqBundle\\MemoryChecker\\NativeMemoryUsageProvider')->getMock();
$memoryUsageProvider->expects($this->any())->method('getMemoryUsage')->willReturn($currentMemoryUsage);

$memoryManager = new MemoryConsumptionChecker($memoryUsageProvider);

$this->assertFalse($memoryManager->isRamAlmostOverloaded($maxConsumptionAllowed));
}

public function testMemoryExactValueIsAlmostOverloaded()
{
$currentMemoryUsage = '11M';
$maxConsumptionAllowed = '10M';

$memoryUsageProvider = $this->getMockBuilder('OldSound\\RabbitMqBundle\\MemoryChecker\\NativeMemoryUsageProvider')->getMock();
$memoryUsageProvider->expects($this->any())->method('getMemoryUsage')->willReturn($currentMemoryUsage);

$memoryManager = new MemoryConsumptionChecker($memoryUsageProvider);

$this->assertTrue($memoryManager->isRamAlmostOverloaded($maxConsumptionAllowed));
}
}