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

FallbackMailer: shuffle mailers with given probability #35

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
10 changes: 8 additions & 2 deletions src/Mail/FallbackMailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,16 @@ class FallbackMailer implements IMailer

/**
* @param IMailer[]
* @param int $retryWaitTime in miliseconds
* @param int
* @param int $retryWaitTime in miliseconds
* @param float $shuffleProbability probability that mailers will be shuffled
*/
public function __construct(array $mailers, int $retryCount = 3, int $retryWaitTime = 1000)
public function __construct(array $mailers, int $retryCount = 3, int $retryWaitTime = 1000, float $shuffleProbability = 0.0)
{
if ($shuffleProbability > 0.0 && mt_rand() / mt_getrandmax() < $shuffleProbability) {
shuffle($mailers);
}

$this->mailers = $mailers;
$this->retryCount = $retryCount;
$this->retryWaitTime = $retryWaitTime;
Expand Down
15 changes: 15 additions & 0 deletions tests/Mail/FallbackMailer.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,18 @@ test(function () {
$mailer->send(new Message);
Assert::null($onFailureCalls);
});


test(function () {
$subMailerA = new FailingMailer(0);
$subMailerB = new FailingMailer(1);

mt_srand(5);
$mailer = new FallbackMailer([$subMailerA, $subMailerB], 3, 10, 0.5);
$mailer->onFailure[] = function (FallbackMailer $sender, SendException $e, IMailer $mailer, Message $mail) use (&$onFailureCalls) {
$onFailureCalls[] = $mailer;
};

$mailer->send(new Message);
Assert::same([$subMailerB], $onFailureCalls);
});