-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Messenger] Add a memory limit option for ConsumeMessagesCommand
#26975
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
src/Symfony/Component/Messenger/Tests/Fixtures/CallbackReceiver.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
namespace Symfony\Component\Messenger\Tests\Fixtures; | ||
|
||
use Symfony\Component\Messenger\Transport\ReceiverInterface; | ||
|
||
class CallbackReceiver implements ReceiverInterface | ||
{ | ||
private $callable; | ||
|
||
public function __construct(callable $callable) | ||
{ | ||
$this->callable = $callable; | ||
} | ||
|
||
public function receive(callable $handler): void | ||
{ | ||
$callable = $this->callable; | ||
$callable($handler); | ||
} | ||
|
||
public function stop(): void | ||
{ | ||
} | ||
} |
83 changes: 83 additions & 0 deletions
83
...mponent/Messenger/Tests/Transport/Enhancers/StopWhenMemoryUsageIsExceededReceiverTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Messenger\Tests\Transport\Enhancers; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Psr\Log\LoggerInterface; | ||
use Symfony\Component\Messenger\Tests\Fixtures\CallbackReceiver; | ||
use Symfony\Component\Messenger\Tests\Fixtures\DummyMessage; | ||
use Symfony\Component\Messenger\Transport\Enhancers\StopWhenMemoryUsageIsExceededReceiver; | ||
|
||
class StopWhenMemoryUsageIsExceededReceiverTest extends TestCase | ||
{ | ||
/** | ||
* @dataProvider memoryProvider | ||
*/ | ||
public function testReceiverStopsWhenMemoryLimitExceeded(int $memoryUsage, int $memoryLimit, bool $shouldStop) | ||
{ | ||
$callable = function ($handler) { | ||
$handler(new DummyMessage('API')); | ||
}; | ||
|
||
$decoratedReceiver = $this->getMockBuilder(CallbackReceiver::class) | ||
->setConstructorArgs(array($callable)) | ||
->enableProxyingToOriginalMethods() | ||
->getMock(); | ||
|
||
$decoratedReceiver->expects($this->once())->method('receive'); | ||
if (true === $shouldStop) { | ||
$decoratedReceiver->expects($this->once())->method('stop'); | ||
} else { | ||
$decoratedReceiver->expects($this->never())->method('stop'); | ||
} | ||
|
||
$memoryResolver = function () use ($memoryUsage) { | ||
return $memoryUsage; | ||
}; | ||
|
||
$memoryLimitReceiver = new StopWhenMemoryUsageIsExceededReceiver($decoratedReceiver, $memoryLimit, null, $memoryResolver); | ||
$memoryLimitReceiver->receive(function () {}); | ||
} | ||
|
||
public function memoryProvider() | ||
{ | ||
yield array(2048, 1024, true); | ||
yield array(1024, 1024, false); | ||
yield array(1024, 2048, false); | ||
} | ||
|
||
public function testReceiverLogsMemoryExceededWhenLoggerIsGiven() | ||
{ | ||
$callable = function ($handler) { | ||
$handler(new DummyMessage('API')); | ||
}; | ||
|
||
$decoratedReceiver = $this->getMockBuilder(CallbackReceiver::class) | ||
->setConstructorArgs(array($callable)) | ||
->enableProxyingToOriginalMethods() | ||
->getMock(); | ||
|
||
$decoratedReceiver->expects($this->once())->method('receive'); | ||
$decoratedReceiver->expects($this->once())->method('stop'); | ||
|
||
$logger = $this->createMock(LoggerInterface::class); | ||
$logger->expects($this->once())->method('info') | ||
->with('Receiver stopped due to memory limit of {limit} exceeded', array('limit' => 64 * 1024 * 1024)); | ||
|
||
$memoryResolver = function () { | ||
return 70 * 1024 * 1024; | ||
}; | ||
|
||
$memoryLimitReceiver = new StopWhenMemoryUsageIsExceededReceiver($decoratedReceiver, 64 * 1024 * 1024, $logger, $memoryResolver); | ||
$memoryLimitReceiver->receive(function () {}); | ||
} | ||
} |
102 changes: 102 additions & 0 deletions
102
...ponent/Messenger/Tests/Transport/Enhancers/StopWhenMessageCountIsExceededReceiverTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Messenger\Tests\Transport\Enhancers; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Psr\Log\LoggerInterface; | ||
use Symfony\Component\Messenger\Tests\Fixtures\CallbackReceiver; | ||
use Symfony\Component\Messenger\Tests\Fixtures\DummyMessage; | ||
use Symfony\Component\Messenger\Transport\Enhancers\StopWhenMessageCountIsExceededReceiver; | ||
|
||
class StopWhenMessageCountIsExceededReceiverTest extends TestCase | ||
{ | ||
/** | ||
* @dataProvider countProvider | ||
*/ | ||
public function testReceiverStopsWhenMaximumCountExceeded($max, $shouldStop) | ||
{ | ||
$callable = function ($handler) { | ||
$handler(new DummyMessage('First message')); | ||
$handler(new DummyMessage('Second message')); | ||
$handler(new DummyMessage('Third message')); | ||
}; | ||
|
||
$decoratedReceiver = $this->getMockBuilder(CallbackReceiver::class) | ||
->setConstructorArgs(array($callable)) | ||
->enableProxyingToOriginalMethods() | ||
->getMock(); | ||
|
||
$decoratedReceiver->expects($this->once())->method('receive'); | ||
if (true === $shouldStop) { | ||
$decoratedReceiver->expects($this->any())->method('stop'); | ||
} else { | ||
$decoratedReceiver->expects($this->never())->method('stop'); | ||
} | ||
|
||
$maximumCountReceiver = new StopWhenMessageCountIsExceededReceiver($decoratedReceiver, $max); | ||
$maximumCountReceiver->receive(function () {}); | ||
} | ||
|
||
public function countProvider() | ||
{ | ||
yield array(1, true); | ||
yield array(2, true); | ||
yield array(3, true); | ||
yield array(4, false); | ||
} | ||
|
||
public function testReceiverDoesntIncreaseItsCounterWhenReceiveNullMessage() | ||
{ | ||
$callable = function ($handler) { | ||
$handler(null); | ||
$handler(null); | ||
$handler(null); | ||
$handler(null); | ||
}; | ||
|
||
$decoratedReceiver = $this->getMockBuilder(CallbackReceiver::class) | ||
->setConstructorArgs(array($callable)) | ||
->enableProxyingToOriginalMethods() | ||
->getMock(); | ||
|
||
$decoratedReceiver->expects($this->once())->method('receive'); | ||
$decoratedReceiver->expects($this->never())->method('stop'); | ||
|
||
$maximumCountReceiver = new StopWhenMessageCountIsExceededReceiver($decoratedReceiver, 1); | ||
$maximumCountReceiver->receive(function () {}); | ||
} | ||
|
||
public function testReceiverLogsMaximumCountExceededWhenLoggerIsGiven() | ||
{ | ||
$callable = function ($handler) { | ||
$handler(new DummyMessage('First message')); | ||
}; | ||
|
||
$decoratedReceiver = $this->getMockBuilder(CallbackReceiver::class) | ||
->setConstructorArgs(array($callable)) | ||
->enableProxyingToOriginalMethods() | ||
->getMock(); | ||
|
||
$decoratedReceiver->expects($this->once())->method('receive'); | ||
$decoratedReceiver->expects($this->once())->method('stop'); | ||
|
||
$logger = $this->createMock(LoggerInterface::class); | ||
$logger->expects($this->once())->method('info') | ||
->with( | ||
$this->equalTo('Receiver stopped due to maximum count of {count} exceeded'), | ||
$this->equalTo(array('count' => 1)) | ||
); | ||
|
||
$maximumCountReceiver = new StopWhenMessageCountIsExceededReceiver($decoratedReceiver, 1, $logger); | ||
$maximumCountReceiver->receive(function () {}); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
($this->callable)($handler);