Correct Aphlict websocket URI construction after PHP8 compatibility changes
[phabricator.git] / src / applications / metamta / storage / __tests__ / PhabricatorMailConfigTestCase.php
blobfb48925ed4b8f1714ff147fb432db43a99485eb6
1 <?php
3 final class PhabricatorMailConfigTestCase
4 extends PhabricatorTestCase {
6 public function testMailerPriorities() {
7 $mailers = $this->newMailersWithConfig(
8 array(
9 array(
10 'key' => 'A',
11 'type' => 'test',
13 array(
14 'key' => 'B',
15 'type' => 'test',
17 ));
19 $this->assertEqual(
20 array('A', 'B'),
21 mpull($mailers, 'getKey'));
23 $mailers = $this->newMailersWithConfig(
24 array(
25 array(
26 'key' => 'A',
27 'priority' => 1,
28 'type' => 'test',
30 array(
31 'key' => 'B',
32 'priority' => 2,
33 'type' => 'test',
35 ));
37 $this->assertEqual(
38 array('B', 'A'),
39 mpull($mailers, 'getKey'));
41 $mailers = $this->newMailersWithConfig(
42 array(
43 array(
44 'key' => 'A1',
45 'priority' => 300,
46 'type' => 'test',
48 array(
49 'key' => 'A2',
50 'priority' => 300,
51 'type' => 'test',
53 array(
54 'key' => 'B',
55 'type' => 'test',
57 array(
58 'key' => 'C',
59 'priority' => 400,
60 'type' => 'test',
62 array(
63 'key' => 'D',
64 'type' => 'test',
66 ));
68 // The "A" servers should be shuffled randomly, so either outcome is
69 // acceptable.
70 $option_1 = array('C', 'A1', 'A2', 'B', 'D');
71 $option_2 = array('C', 'A2', 'A1', 'B', 'D');
72 $actual = mpull($mailers, 'getKey');
74 $this->assertTrue(($actual === $option_1) || ($actual === $option_2));
76 // Make sure that when we're load balancing we actually send traffic to
77 // both servers reasonably often.
78 $saw_a1 = false;
79 $saw_a2 = false;
80 $attempts = 0;
81 while (true) {
82 $mailers = $this->newMailersWithConfig(
83 array(
84 array(
85 'key' => 'A1',
86 'priority' => 300,
87 'type' => 'test',
89 array(
90 'key' => 'A2',
91 'priority' => 300,
92 'type' => 'test',
94 ));
96 $first_key = head($mailers)->getKey();
98 if ($first_key == 'A1') {
99 $saw_a1 = true;
102 if ($first_key == 'A2') {
103 $saw_a2 = true;
106 if ($saw_a1 && $saw_a2) {
107 break;
110 if ($attempts++ > 1024) {
111 throw new Exception(
112 pht(
113 'Load balancing between two mail servers did not select both '.
114 'servers after an absurd number of attempts.'));
118 $this->assertTrue($saw_a1 && $saw_a2);
121 public function testMailerConstraints() {
122 $config = array(
123 array(
124 'key' => 'X1',
125 'type' => 'test',
127 array(
128 'key' => 'X1-in',
129 'type' => 'test',
130 'outbound' => false,
132 array(
133 'key' => 'X1-out',
134 'type' => 'test',
135 'inbound' => false,
137 array(
138 'key' => 'X1-void',
139 'type' => 'test',
140 'inbound' => false,
141 'outbound' => false,
145 $mailers = $this->newMailersWithConfig(
146 $config,
147 array());
148 $this->assertEqual(4, count($mailers));
150 $mailers = $this->newMailersWithConfig(
151 $config,
152 array(
153 'inbound' => true,
155 $this->assertEqual(2, count($mailers));
157 $mailers = $this->newMailersWithConfig(
158 $config,
159 array(
160 'outbound' => true,
162 $this->assertEqual(2, count($mailers));
164 $mailers = $this->newMailersWithConfig(
165 $config,
166 array(
167 'inbound' => true,
168 'outbound' => true,
170 $this->assertEqual(1, count($mailers));
172 $mailers = $this->newMailersWithConfig(
173 $config,
174 array(
175 'types' => array('test'),
177 $this->assertEqual(4, count($mailers));
179 $mailers = $this->newMailersWithConfig(
180 $config,
181 array(
182 'types' => array('duck'),
184 $this->assertEqual(0, count($mailers));
187 private function newMailersWithConfig(
188 array $config,
189 array $constraints = array()) {
191 $env = PhabricatorEnv::beginScopedEnv();
192 $env->overrideEnvConfig('cluster.mailers', $config);
194 $mailers = PhabricatorMetaMTAMail::newMailers($constraints);
196 unset($env);
197 return $mailers;