1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2025-01-11 07:11:04 +01:00

Add some test coverage for mailers configuration

Summary: Depends on D19005. Ref T12677. Ref T13053. Tests that turning `cluster.mailers` into an actual list of mailers more or less works as expected.

Test Plan: Ran unit tests.

Reviewers: amckinley

Reviewed By: amckinley

Maniphest Tasks: T13053, T12677

Differential Revision: https://secure.phabricator.com/D19006
This commit is contained in:
epriestley 2018-02-06 06:56:44 -08:00
parent 994d2e8e15
commit 9947eee182
3 changed files with 136 additions and 1 deletions

View file

@ -3178,6 +3178,7 @@ phutil_register_library_map(array(
'PhabricatorMacroTransactionQuery' => 'applications/macro/query/PhabricatorMacroTransactionQuery.php',
'PhabricatorMacroTransactionType' => 'applications/macro/xaction/PhabricatorMacroTransactionType.php',
'PhabricatorMacroViewController' => 'applications/macro/controller/PhabricatorMacroViewController.php',
'PhabricatorMailConfigTestCase' => 'applications/metamta/storage/__tests__/PhabricatorMailConfigTestCase.php',
'PhabricatorMailEmailHeraldField' => 'applications/metamta/herald/PhabricatorMailEmailHeraldField.php',
'PhabricatorMailEmailHeraldFieldGroup' => 'applications/metamta/herald/PhabricatorMailEmailHeraldFieldGroup.php',
'PhabricatorMailEmailSubjectHeraldField' => 'applications/metamta/herald/PhabricatorMailEmailSubjectHeraldField.php',
@ -8680,6 +8681,7 @@ phutil_register_library_map(array(
'PhabricatorMacroTransactionQuery' => 'PhabricatorApplicationTransactionQuery',
'PhabricatorMacroTransactionType' => 'PhabricatorModularTransactionType',
'PhabricatorMacroViewController' => 'PhabricatorMacroController',
'PhabricatorMailConfigTestCase' => 'PhabricatorTestCase',
'PhabricatorMailEmailHeraldField' => 'HeraldField',
'PhabricatorMailEmailHeraldFieldGroup' => 'HeraldFieldGroup',
'PhabricatorMailEmailSubjectHeraldField' => 'PhabricatorMailEmailHeraldField',

View file

@ -513,12 +513,14 @@ final class PhabricatorMetaMTAMail
$defaults = $mailer->newDefaultOptions();
$options = idx($spec, 'options', array()) + $defaults;
$mailer->setOptions($options);
$mailers[] = $mailer;
}
}
$sorted = array();
$groups = mgroup($mailers, 'getPriority');
ksort($groups);
krsort($groups);
foreach ($groups as $group) {
// Reorder services within the same priority group randomly.
shuffle($group);

View file

@ -0,0 +1,131 @@
<?php
final class PhabricatorMailConfigTestCase
extends PhabricatorTestCase {
public function testMailerPriorities() {
$mailers = $this->newMailersWithConfig(
array(
array(
'key' => 'A',
'type' => 'test',
),
array(
'key' => 'B',
'type' => 'test',
),
));
$this->assertEqual(
array('A', 'B'),
mpull($mailers, 'getKey'));
$mailers = $this->newMailersWithConfig(
array(
array(
'key' => 'A',
'priority' => 1,
'type' => 'test',
),
array(
'key' => 'B',
'priority' => 2,
'type' => 'test',
),
));
$this->assertEqual(
array('B', 'A'),
mpull($mailers, 'getKey'));
$mailers = $this->newMailersWithConfig(
array(
array(
'key' => 'A1',
'priority' => 300,
'type' => 'test',
),
array(
'key' => 'A2',
'priority' => 300,
'type' => 'test',
),
array(
'key' => 'B',
'type' => 'test',
),
array(
'key' => 'C',
'priority' => 400,
'type' => 'test',
),
array(
'key' => 'D',
'type' => 'test',
),
));
// The "A" servers should be shuffled randomly, so either outcome is
// acceptable.
$option_1 = array('C', 'A1', 'A2', 'B', 'D');
$option_2 = array('C', 'A2', 'A1', 'B', 'D');
$actual = mpull($mailers, 'getKey');
$this->assertTrue(($actual === $option_1) || ($actual === $option_2));
// Make sure that when we're load balancing we actually send traffic to
// both servers reasonably often.
$saw_a1 = false;
$saw_a2 = false;
$attempts = 0;
while (true) {
$mailers = $this->newMailersWithConfig(
array(
array(
'key' => 'A1',
'priority' => 300,
'type' => 'test',
),
array(
'key' => 'A2',
'priority' => 300,
'type' => 'test',
),
));
$first_key = head($mailers)->getKey();
if ($first_key == 'A1') {
$saw_a1 = true;
}
if ($first_key == 'A2') {
$saw_a2 = true;
}
if ($saw_a1 && $saw_a2) {
break;
}
if ($attempts++ > 1024) {
throw new Exception(
pht(
'Load balancing between two mail servers did not select both '.
'servers after an absurd number of attempts.'));
}
}
$this->assertTrue($saw_a1 && $saw_a2);
}
private function newMailersWithConfig(array $config) {
$env = PhabricatorEnv::beginScopedEnv();
$env->overrideEnvConfig('cluster.mailers', $config);
$mailers = PhabricatorMetaMTAMail::newMailers();
unset($env);
return $mailers;
}
}