1 | <?php |
---|
2 | /** |
---|
3 | * Copyright 2011 Bas de Nooijer. All rights reserved. |
---|
4 | * |
---|
5 | * Redistribution and use in source and binary forms, with or without |
---|
6 | * modification, are permitted provided that the following conditions are met: |
---|
7 | * |
---|
8 | * 1. Redistributions of source code must retain the above copyright notice, |
---|
9 | * this list of conditions and the following disclaimer. |
---|
10 | * |
---|
11 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
---|
12 | * this listof conditions and the following disclaimer in the documentation |
---|
13 | * and/or other materials provided with the distribution. |
---|
14 | * |
---|
15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS" |
---|
16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
---|
19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
---|
20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
---|
21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
---|
22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
---|
23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
---|
24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
---|
25 | * POSSIBILITY OF SUCH DAMAGE. |
---|
26 | * |
---|
27 | * The views and conclusions contained in the software and documentation are |
---|
28 | * those of the authors and should not be interpreted as representing official |
---|
29 | * policies, either expressed or implied, of the copyright holder. |
---|
30 | */ |
---|
31 | |
---|
32 | class Solarium_Plugin_LoadbalancerTest extends PHPUnit_Framework_TestCase |
---|
33 | { |
---|
34 | /** |
---|
35 | * @var Solarium_Plugin_Loadbalancer |
---|
36 | */ |
---|
37 | protected $_plugin; |
---|
38 | |
---|
39 | /** |
---|
40 | * @var Solarium_Client |
---|
41 | */ |
---|
42 | protected $_client; |
---|
43 | |
---|
44 | protected $_serverOptions = array('host' => 'nonexistinghostname'); |
---|
45 | |
---|
46 | public function setUp() |
---|
47 | { |
---|
48 | $this->_plugin = new Solarium_Plugin_Loadbalancer(); |
---|
49 | |
---|
50 | $this->_client = new Solarium_Client(); |
---|
51 | $adapter = $this->getMock('Solarium_Client_Adapter_Http'); |
---|
52 | $adapter->expects($this->any()) |
---|
53 | ->method('execute') |
---|
54 | ->will($this->returnValue('dummyresult')); |
---|
55 | $this->_client->setAdapter($adapter); |
---|
56 | $this->_plugin->init($this->_client, array()); |
---|
57 | } |
---|
58 | |
---|
59 | public function testConfigMode() |
---|
60 | { |
---|
61 | $options = array( |
---|
62 | 'server' => array( |
---|
63 | 'server1' => array( |
---|
64 | 'options' => array( |
---|
65 | 'host' => 'host1' |
---|
66 | ), |
---|
67 | 'weight' => 10, |
---|
68 | ), |
---|
69 | 'server2' => array( |
---|
70 | 'options' => array( |
---|
71 | 'host' => 'host2' |
---|
72 | ), |
---|
73 | 'weight' => 5, |
---|
74 | ), |
---|
75 | ), |
---|
76 | 'blockedquerytype' => array(Solarium_Client::QUERYTYPE_UPDATE, Solarium_Client::QUERYTYPE_MORELIKETHIS) |
---|
77 | ); |
---|
78 | |
---|
79 | $this->_plugin->setOptions($options); |
---|
80 | |
---|
81 | $this->assertEquals( |
---|
82 | array( |
---|
83 | 'server1' => array( |
---|
84 | 'options' => array('host' => 'host1'), |
---|
85 | 'weight' => 10, |
---|
86 | ), |
---|
87 | 'server2' => array( |
---|
88 | 'options' => array('host' => 'host2'), |
---|
89 | 'weight' => 5, |
---|
90 | ) |
---|
91 | ), |
---|
92 | $this->_plugin->getServers() |
---|
93 | ); |
---|
94 | |
---|
95 | $this->assertEquals( |
---|
96 | array(Solarium_Client::QUERYTYPE_UPDATE, Solarium_Client::QUERYTYPE_MORELIKETHIS), |
---|
97 | $this->_plugin->getBlockedQueryTypes() |
---|
98 | ); |
---|
99 | } |
---|
100 | |
---|
101 | public function testSetAndGetFailoverEnabled() |
---|
102 | { |
---|
103 | $this->_plugin->setFailoverEnabled(true); |
---|
104 | $this->assertEquals(true, $this->_plugin->getFailoverEnabled()); |
---|
105 | } |
---|
106 | |
---|
107 | public function testSetAndGetFailoverMaxRetries() |
---|
108 | { |
---|
109 | $this->_plugin->setFailoverMaxRetries(16); |
---|
110 | $this->assertEquals(16, $this->_plugin->getFailoverMaxRetries()); |
---|
111 | } |
---|
112 | |
---|
113 | public function testAddServer() |
---|
114 | { |
---|
115 | $this->_plugin->addServer('s1', $this->_serverOptions, 10); |
---|
116 | |
---|
117 | $this->assertEquals( |
---|
118 | array('s1' => |
---|
119 | array( |
---|
120 | 'options' => $this->_serverOptions, |
---|
121 | 'weight' => 10, |
---|
122 | ) |
---|
123 | ), |
---|
124 | $this->_plugin->getServers() |
---|
125 | ); |
---|
126 | } |
---|
127 | |
---|
128 | public function testAddServerWithDuplicateKey() |
---|
129 | { |
---|
130 | $this->_plugin->addServer('s1', $this->_serverOptions, 10); |
---|
131 | |
---|
132 | $this->setExpectedException('Solarium_Exception'); |
---|
133 | $this->_plugin->addServer('s1', $this->_serverOptions, 20); |
---|
134 | } |
---|
135 | |
---|
136 | public function testGetServer() |
---|
137 | { |
---|
138 | $this->_plugin->addServer('s1', $this->_serverOptions, 10); |
---|
139 | |
---|
140 | $this->assertEquals( |
---|
141 | array('options' => $this->_serverOptions, 'weight' => 10), |
---|
142 | $this->_plugin->getServer('s1') |
---|
143 | ); |
---|
144 | } |
---|
145 | |
---|
146 | public function testGetInvalidServer() |
---|
147 | { |
---|
148 | $this->_plugin->addServer('s1', $this->_serverOptions, 10); |
---|
149 | |
---|
150 | $this->setExpectedException('Solarium_Exception'); |
---|
151 | $this->_plugin->getServer('s2'); |
---|
152 | } |
---|
153 | |
---|
154 | public function testClearServers() |
---|
155 | { |
---|
156 | $this->_plugin->addServer('s1', $this->_serverOptions, 10); |
---|
157 | $this->_plugin->clearServers(); |
---|
158 | $this->assertEquals(array(), $this->_plugin->getServers()); |
---|
159 | } |
---|
160 | |
---|
161 | public function testAddServers() |
---|
162 | { |
---|
163 | $servers = array( |
---|
164 | 's1' => array('options' => $this->_serverOptions, 'weight' => 10), |
---|
165 | 's2' => array('options' => $this->_serverOptions, 'weight' => 20), |
---|
166 | ); |
---|
167 | |
---|
168 | $this->_plugin->addServers($servers); |
---|
169 | $this->assertEquals($servers, $this->_plugin->getServers()); |
---|
170 | } |
---|
171 | |
---|
172 | public function testRemoveServer() |
---|
173 | { |
---|
174 | $servers = array( |
---|
175 | 's1' => array('options' => $this->_serverOptions, 'weight' => 10), |
---|
176 | 's2' => array('options' => $this->_serverOptions, 'weight' => 20), |
---|
177 | ); |
---|
178 | |
---|
179 | $this->_plugin->addServers($servers); |
---|
180 | $this->_plugin->removeServer('s1'); |
---|
181 | |
---|
182 | $this->assertEquals( |
---|
183 | array('s2' => array('options' => $this->_serverOptions, 'weight' => 20)), |
---|
184 | $this->_plugin->getServers() |
---|
185 | ); |
---|
186 | } |
---|
187 | |
---|
188 | public function testSetServers() |
---|
189 | { |
---|
190 | $servers1 = array( |
---|
191 | 's1' => array('options' => $this->_serverOptions, 'weight' => 10), |
---|
192 | 's2' => array('options' => $this->_serverOptions, 'weight' => 20), |
---|
193 | ); |
---|
194 | |
---|
195 | $servers2 = array( |
---|
196 | 's3' => array('options' => $this->_serverOptions, 'weight' => 50), |
---|
197 | 's4' => array('options' => $this->_serverOptions, 'weight' => 30), |
---|
198 | ); |
---|
199 | |
---|
200 | $this->_plugin->addServers($servers1); |
---|
201 | $this->_plugin->setServers($servers2); |
---|
202 | |
---|
203 | $this->assertEquals( |
---|
204 | $servers2, |
---|
205 | $this->_plugin->getServers() |
---|
206 | ); |
---|
207 | } |
---|
208 | |
---|
209 | public function testSetAndGetForcedServerForNextQuery() |
---|
210 | { |
---|
211 | $servers1 = array( |
---|
212 | 's1' => array('options' => $this->_serverOptions, 'weight' => 10), |
---|
213 | 's2' => array('options' => $this->_serverOptions, 'weight' => 20), |
---|
214 | ); |
---|
215 | $this->_plugin->addServers($servers1); |
---|
216 | |
---|
217 | $this->_plugin->setForcedServerForNextQuery('s2'); |
---|
218 | $this->assertEquals('s2', $this->_plugin->getForcedServerForNextQuery()); |
---|
219 | } |
---|
220 | |
---|
221 | public function testSetForcedServerForNextQueryWithInvalidKey() |
---|
222 | { |
---|
223 | $servers1 = array( |
---|
224 | 's1' => array('options' => $this->_serverOptions, 'weight' => 10), |
---|
225 | 's2' => array('options' => $this->_serverOptions, 'weight' => 20), |
---|
226 | ); |
---|
227 | $this->_plugin->addServers($servers1); |
---|
228 | |
---|
229 | $this->setExpectedException('Solarium_Exception'); |
---|
230 | $this->_plugin->setForcedServerForNextQuery('s3'); |
---|
231 | } |
---|
232 | |
---|
233 | public function testAddBlockedQueryType() |
---|
234 | { |
---|
235 | $this->_plugin->addBlockedQueryType('type1'); |
---|
236 | $this->_plugin->addBlockedQueryType('type2'); |
---|
237 | |
---|
238 | $this->assertEquals( |
---|
239 | array(Solarium_Client::QUERYTYPE_UPDATE, 'type1', 'type2'), |
---|
240 | $this->_plugin->getBlockedQueryTypes() |
---|
241 | ); |
---|
242 | } |
---|
243 | |
---|
244 | public function testClearBlockedQueryTypes() |
---|
245 | { |
---|
246 | $this->_plugin->addBlockedQueryType('type1'); |
---|
247 | $this->_plugin->addBlockedQueryType('type2'); |
---|
248 | $this->_plugin->clearBlockedQueryTypes(); |
---|
249 | $this->assertEquals(array(), $this->_plugin->getBlockedQueryTypes()); |
---|
250 | } |
---|
251 | |
---|
252 | public function testAddBlockedQueryTypes() |
---|
253 | { |
---|
254 | $blockedQueryTypes = array('type1', 'type2', 'type3'); |
---|
255 | |
---|
256 | $this->_plugin->clearBlockedQueryTypes(); |
---|
257 | $this->_plugin->addBlockedQueryTypes($blockedQueryTypes); |
---|
258 | $this->assertEquals($blockedQueryTypes, $this->_plugin->getBlockedQueryTypes()); |
---|
259 | } |
---|
260 | |
---|
261 | public function testRemoveBlockedQueryType() |
---|
262 | { |
---|
263 | $blockedQueryTypes = array('type1', 'type2', 'type3'); |
---|
264 | |
---|
265 | $this->_plugin->clearBlockedQueryTypes(); |
---|
266 | $this->_plugin->addBlockedQueryTypes($blockedQueryTypes); |
---|
267 | $this->_plugin->removeBlockedQueryType('type2'); |
---|
268 | |
---|
269 | $this->assertEquals( |
---|
270 | array('type1', 'type3'), |
---|
271 | $this->_plugin->getBlockedQueryTypes() |
---|
272 | ); |
---|
273 | } |
---|
274 | |
---|
275 | public function testSetBlockedQueryTypes() |
---|
276 | { |
---|
277 | $blockedQueryTypes = array('type1', 'type2', 'type3'); |
---|
278 | |
---|
279 | $this->_plugin->setBlockedQueryTypes($blockedQueryTypes); |
---|
280 | |
---|
281 | $this->assertEquals( |
---|
282 | $blockedQueryTypes, |
---|
283 | $this->_plugin->getBlockedQueryTypes() |
---|
284 | ); |
---|
285 | } |
---|
286 | |
---|
287 | public function testPreExecuteRequestWithForcedServer() |
---|
288 | { |
---|
289 | $servers = array( |
---|
290 | 's1' => array('options' => $this->_serverOptions, 'weight' => 100), |
---|
291 | 's2' => array('options' => $this->_serverOptions, 'weight' => 1), |
---|
292 | ); |
---|
293 | $query = new Solarium_Query_Select(); |
---|
294 | $request = new Solarium_Client_Request(); |
---|
295 | |
---|
296 | $this->_plugin->setServers($servers); |
---|
297 | $this->_plugin->setForcedServerForNextQuery('s2'); |
---|
298 | $this->_plugin->preCreateRequest($query); |
---|
299 | $this->_plugin->preExecuteRequest($request); |
---|
300 | |
---|
301 | $this->assertEquals( |
---|
302 | 's2', |
---|
303 | $this->_plugin->getLastServerKey() |
---|
304 | ); |
---|
305 | } |
---|
306 | |
---|
307 | public function testAdapterPresetRestore() |
---|
308 | { |
---|
309 | $originalHost = $this->_client->getAdapter()->getHost(); |
---|
310 | $servers = array( |
---|
311 | 's1' => array('options' => $this->_serverOptions, 'weight' => 100), |
---|
312 | 's2' => array('options' => $this->_serverOptions, 'weight' => 1), |
---|
313 | ); |
---|
314 | $request = new Solarium_Client_Request(); |
---|
315 | |
---|
316 | $this->_plugin->setServers($servers); |
---|
317 | $this->_plugin->setForcedServerForNextQuery('s2'); |
---|
318 | |
---|
319 | $query = new Solarium_Query_Select(); |
---|
320 | $this->_plugin->preCreateRequest($query); |
---|
321 | $this->_plugin->preExecuteRequest($request); |
---|
322 | |
---|
323 | $this->assertEquals( |
---|
324 | 's2', |
---|
325 | $this->_plugin->getLastServerKey() |
---|
326 | ); |
---|
327 | |
---|
328 | $query = new Solarium_Query_Update(); // this is a blocked querytype that should trigger a restore |
---|
329 | $this->_plugin->preCreateRequest($query); |
---|
330 | $this->_plugin->preExecuteRequest($request); |
---|
331 | |
---|
332 | $this->assertEquals( |
---|
333 | $originalHost, |
---|
334 | $this->_client->getAdapter()->getHost() |
---|
335 | ); |
---|
336 | } |
---|
337 | |
---|
338 | public function testBlockedQueryTypeNotLoadbalanced() |
---|
339 | { |
---|
340 | $originalHost = $this->_client->getAdapter()->getHost(); |
---|
341 | $servers = array( |
---|
342 | 's1' => array('options' => $this->_serverOptions, 'weight' => 100), |
---|
343 | 's2' => array('options' => $this->_serverOptions, 'weight' => 1), |
---|
344 | ); |
---|
345 | $this->_plugin->setServers($servers); |
---|
346 | $request = new Solarium_Client_Request(); |
---|
347 | |
---|
348 | $query = new Solarium_Query_Update(); // this is a blocked querytype that should not be loadbalanced |
---|
349 | $this->_plugin->preCreateRequest($query); |
---|
350 | $this->_plugin->preExecuteRequest($request); |
---|
351 | |
---|
352 | $this->assertEquals( |
---|
353 | $originalHost, |
---|
354 | $this->_client->getAdapter()->getHost() |
---|
355 | ); |
---|
356 | |
---|
357 | $this->assertEquals( |
---|
358 | null, |
---|
359 | $this->_plugin->getLastServerKey() |
---|
360 | ); |
---|
361 | } |
---|
362 | |
---|
363 | public function testLoadbalancerRandomizing() |
---|
364 | { |
---|
365 | $servers = array( |
---|
366 | 's1' => array('options' => $this->_serverOptions, 'weight' => 1), |
---|
367 | 's2' => array('options' => $this->_serverOptions, 'weight' => 1), |
---|
368 | ); |
---|
369 | $this->_plugin->setServers($servers); |
---|
370 | $request = new Solarium_Client_Request(); |
---|
371 | |
---|
372 | $query = new Solarium_Query_Select(); // |
---|
373 | $this->_plugin->preCreateRequest($query); |
---|
374 | $this->_plugin->preExecuteRequest($request); |
---|
375 | |
---|
376 | $this->assertTrue( |
---|
377 | in_array($this->_plugin->getLastServerKey(), array('s1','s2')) |
---|
378 | ); |
---|
379 | } |
---|
380 | |
---|
381 | public function testFailover() |
---|
382 | { |
---|
383 | $this->_plugin = new TestLoadbalancer(); // special loadbalancer that returns servers in fixed order |
---|
384 | $this->_client = new Solarium_Client(); |
---|
385 | $this->_client->setAdapter(new TestAdapterForFailover()); // set special mock that fails once |
---|
386 | $this->_plugin->init($this->_client, array()); |
---|
387 | |
---|
388 | $request = new Solarium_Client_Request(); |
---|
389 | $servers = array( |
---|
390 | 's1' => array('options' => $this->_serverOptions, 'weight' => 1), |
---|
391 | 's2' => array('options' => $this->_serverOptions, 'weight' => 1), |
---|
392 | ); |
---|
393 | $this->_plugin->setServers($servers); |
---|
394 | $this->_plugin->setFailoverEnabled(true); |
---|
395 | |
---|
396 | $query = new Solarium_Query_Select(); |
---|
397 | $this->_plugin->preCreateRequest($query); |
---|
398 | $this->_plugin->preExecuteRequest($request); |
---|
399 | |
---|
400 | $this->assertEquals( |
---|
401 | 's2', |
---|
402 | $this->_plugin->getLastServerKey() |
---|
403 | ); |
---|
404 | } |
---|
405 | |
---|
406 | public function testFailoverMaxRetries() |
---|
407 | { |
---|
408 | $this->_plugin = new TestLoadbalancer(); // special loadbalancer that returns servers in fixed order |
---|
409 | $this->_client = new Solarium_Client(); |
---|
410 | $adapter = new TestAdapterForFailover(); |
---|
411 | $adapter->setFailCount(10); |
---|
412 | $this->_client->setAdapter($adapter); // set special mock that fails for all servers |
---|
413 | $this->_plugin->init($this->_client, array()); |
---|
414 | |
---|
415 | $request = new Solarium_Client_Request(); |
---|
416 | $servers = array( |
---|
417 | 's1' => array('options' => $this->_serverOptions, 'weight' => 1), |
---|
418 | 's2' => array('options' => $this->_serverOptions, 'weight' => 1), |
---|
419 | ); |
---|
420 | $this->_plugin->setServers($servers); |
---|
421 | $this->_plugin->setFailoverEnabled(true); |
---|
422 | |
---|
423 | $query = new Solarium_Query_Select(); |
---|
424 | $this->_plugin->preCreateRequest($query); |
---|
425 | |
---|
426 | $this->setExpectedException('Solarium_Exception', 'Maximum number of loadbalancer retries reached'); |
---|
427 | |
---|
428 | $this->_plugin->preExecuteRequest($request); |
---|
429 | } |
---|
430 | |
---|
431 | |
---|
432 | |
---|
433 | } |
---|
434 | |
---|
435 | class TestLoadbalancer extends Solarium_Plugin_Loadbalancer{ |
---|
436 | |
---|
437 | protected $_counter = 0; |
---|
438 | |
---|
439 | /** |
---|
440 | * Get options array for a randomized server |
---|
441 | * |
---|
442 | * @return array |
---|
443 | */ |
---|
444 | protected function _getRandomServerOptions() |
---|
445 | { |
---|
446 | $this->_counter++; |
---|
447 | $serverKey = 's'.$this->_counter; |
---|
448 | |
---|
449 | $this->_serverExcludes[] = $serverKey; |
---|
450 | $this->_lastServerKey = $serverKey; |
---|
451 | return $this->_servers[$serverKey]['options']; |
---|
452 | } |
---|
453 | |
---|
454 | } |
---|
455 | |
---|
456 | class TestAdapterForFailover extends Solarium_Client_Adapter_Http{ |
---|
457 | |
---|
458 | protected $_counter = 0; |
---|
459 | |
---|
460 | protected $_failCount = 1; |
---|
461 | |
---|
462 | public function setFailCount($count) |
---|
463 | { |
---|
464 | $this->_failCount = $count; |
---|
465 | } |
---|
466 | |
---|
467 | public function execute($request) |
---|
468 | { |
---|
469 | $this->_counter++; |
---|
470 | if($this->_counter <= $this->_failCount) { |
---|
471 | throw new Solarium_Client_HttpException('failover exception'); |
---|
472 | } |
---|
473 | |
---|
474 | return 'dummyvalue'; |
---|
475 | } |
---|
476 | |
---|
477 | } |
---|