Completed
Push — master ( 6bc3b4...34ca55 )
by Ralf
15s queued 11s
created

SniffingConnectionPoolTest   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 419
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 259
dl 0
loc 419
rs 10
c 0
b 0
f 0
wmc 16
1
<?php
2
/**
3
 * User: zach
4
 * Date: 9/20/13
5
 * Time: 12:51 PM
6
 */
7
8
use Elasticsearch\Common\Exceptions\NoNodesAvailableException;
9
use Elasticsearch\ConnectionPool\SniffingConnectionPool;
10
use Mockery as m;
11
12
/**
13
 * Class SniffingConnectionPoolTest
14
 *
15
 * @category   Tests
16
 * @package    Elasticsearch
17
 * @subpackage Tests/SniffingConnectionPoolTest
18
 * @author     Zachary Tong <[email protected]>
19
 * @license    http://www.apache.org/licenses/LICENSE-2.0 Apache2
20
 * @link       http://elasticsearch.org
21
 */
22
class SniffingConnectionPoolTest extends \PHPUnit_Framework_TestCase
23
{
24
25
    public function tearDown() {
26
        m::close();
27
    }
28
29
    public function testAddOneHostThenGetConnection()
30
    {
31
        $mockConnection = m::mock('\Elasticsearch\Connections\GuzzleConnection')
32
                          ->shouldReceive('ping')
33
                          ->andReturn(true)
34
                          ->getMock()
35
                          ->shouldReceive('isAlive')
36
                          ->andReturn(true)
37
                          ->getMock();
38
39
        $connections = array($mockConnection);
40
41
        $selector = m::mock('\Elasticsearch\ConnectionPool\Selectors\RoundRobinSelector')
42
                    ->shouldReceive('select')
43
                    ->andReturn($connections[0])
44
                    ->getMock();
45
46
        $connectionFactory = m::mock('\Elasticsearch\Connections\ConnectionFactory');
47
48
        $connectionPoolParams = array('randomizeHosts' => false);
49
        $connectionPool = new SniffingConnectionPool($connections, $selector, $connectionFactory, $connectionPoolParams);
50
51
        $retConnection = $connectionPool->nextConnection();
52
53
        $this->assertEquals($mockConnection, $retConnection);
54
55
    }
56
57
    public function testAddOneHostAndTriggerSniff()
58
    {
59
        $clusterState['text'] = '{"ok":true,"cluster_name":"elasticsearch_zach","nodes":{"Bl2ihSr7TcuUHxhu1GA_YQ":{"name":"Vesta","transport_address":"inet[/192.168.1.119:9300]","hostname":"zach-ThinkPad-W530","version":"0.90.5","http_address":"inet[/192.168.1.119:9200]"}}}';
60
61
        $mockConnection = m::mock('\Elasticsearch\Connections\GuzzleConnection')
62
                          ->shouldReceive('ping')->andReturn(true)->getMock()
63
                          ->shouldReceive('isAlive')->andReturn(true)->getMock()
64
                          ->shouldReceive('getTransportSchema')->once()->andReturn('http')->getMock()
65
                          ->shouldReceive('sniff')->once()->andReturn($clusterState)->getMock();
66
67
        $connections = array($mockConnection);
68
        $mockNewConnection = m::mock('\Elasticsearch\Connections\GuzzleConnection')
69
                             ->shouldReceive('isAlive')->andReturn(true)->getMock();
70
71
        $selector = m::mock('\Elasticsearch\ConnectionPool\Selectors\RoundRobinSelector')
72
                    ->shouldReceive('select')->twice()
73
                    ->andReturn($mockNewConnection)
74
                    ->getMock();
75
76
        $connectionFactory = m::mock('\Elasticsearch\Connections\ConnectionFactory')
77
                    ->shouldReceive('create')->with(array('host' => '192.168.1.119', 'port' => 9200))->andReturn($mockNewConnection)->getMock();
78
79
        $connectionPoolParams = array(
80
            'randomizeHosts' => false,
81
            'sniffingInterval'  => -1
82
        );
83
        $connectionPool = new SniffingConnectionPool($connections, $selector, $connectionFactory, $connectionPoolParams);
84
85
        $retConnection = $connectionPool->nextConnection();
86
87
        $this->assertEquals($mockNewConnection, $retConnection);
88
    }
89
90
    public function testAddOneHostAndForceNext()
91
    {
92
        $clusterState['text'] = '{"ok":true,"cluster_name":"elasticsearch_zach","nodes":{"Bl2ihSr7TcuUHxhu1GA_YQ":{"name":"Vesta","transport_address":"inet[/192.168.1.119:9300]","hostname":"zach-ThinkPad-W530","version":"0.90.5","http_address":"inet[/192.168.1.119:9200]"}}}';
93
94
        $mockConnection = m::mock('\Elasticsearch\Connections\GuzzleConnection')
95
                          ->shouldReceive('ping')->andReturn(true)->getMock()
96
                          ->shouldReceive('isAlive')->andReturn(true)->getMock()
97
                          ->shouldReceive('getTransportSchema')->once()->andReturn('http')->getMock()
98
                          ->shouldReceive('sniff')->once()->andReturn($clusterState)->getMock();
99
100
        $connections = array($mockConnection);
101
        $mockNewConnection = m::mock('\Elasticsearch\Connections\GuzzleConnection')
102
                             ->shouldReceive('isAlive')->andReturn(true)->getMock();
103
104
        $selector = m::mock('\Elasticsearch\ConnectionPool\Selectors\RoundRobinSelector')
105
                    ->shouldReceive('select')->once()->andReturn($mockConnection)->getMock()
106
                    ->shouldReceive('select')->once()->andReturn($mockNewConnection)->getMock();
107
108
        $connectionFactory = m::mock('\Elasticsearch\Connections\ConnectionFactory')
109
                             ->shouldReceive('create')->with(array('host' => '192.168.1.119', 'port' => 9200))->andReturn($mockNewConnection)->getMock();
110
111
        $connectionPoolParams = array(
112
            'randomizeHosts' => false
113
        );
114
        $connectionPool = new SniffingConnectionPool($connections, $selector, $connectionFactory, $connectionPoolParams);
115
116
        $retConnection = $connectionPool->nextConnection(true);
117
118
        $this->assertEquals($mockNewConnection, $retConnection);
119
    }
120
121
122
    public function testAddTenNodesThenGetConnection()
123
    {
124
        $connections = array();
125
126
        foreach (range(1,10) as $index) {
127
            $mockConnection = m::mock('\Elasticsearch\Connections\GuzzleConnection')
128
                              ->shouldReceive('ping')
129
                              ->andReturn(true)
130
                              ->getMock()
131
                              ->shouldReceive('isAlive')
132
                              ->andReturn(true)
133
                              ->getMock();
134
135
            $connections[] = $mockConnection;
136
        }
137
138
139
        $selector = m::mock('\Elasticsearch\ConnectionPool\Selectors\RoundRobinSelector')
140
                    ->shouldReceive('select')
141
                    ->andReturn($connections[0])
142
                    ->getMock();
143
144
        $connectionFactory = m::mock('\Elasticsearch\Connections\ConnectionFactory');
145
146
        $connectionPoolParams = array('randomizeHosts' => false);
147
        $connectionPool = new SniffingConnectionPool($connections, $selector, $connectionFactory, $connectionPoolParams);
148
149
        $retConnection = $connectionPool->nextConnection();
150
151
        $this->assertEquals($connections[0], $retConnection);
152
153
    }
154
155
    public function testAddTenNodesTimeoutAllButLast()
156
    {
157
        $connections = array();
158
159
        foreach (range(1,9) as $index) {
160
            $mockConnection = m::mock('\Elasticsearch\Connections\GuzzleConnection')
161
                              ->shouldReceive('ping')
162
                              ->andReturn(false)
163
                              ->getMock()
164
                              ->shouldReceive('isAlive')
165
                              ->andReturn(false)
166
                              ->getMock();
167
168
            $connections[] = $mockConnection;
169
        }
170
171
        $mockConnection = m::mock('\Elasticsearch\Connections\GuzzleConnection')
172
                          ->shouldReceive('ping')
173
                          ->andReturn(true)
174
                          ->getMock()
175
                          ->shouldReceive('isAlive')
176
                          ->andReturn(true)
177
                          ->getMock();
178
179
        $connections[] = $mockConnection;
180
181
182
        $selector = m::mock('\Elasticsearch\ConnectionPool\Selectors\RoundRobinSelector')
183
                    ->shouldReceive('select')
184
                    ->andReturnValues($connections)
185
                    ->getMock();
186
187
        $connectionFactory = m::mock('\Elasticsearch\Connections\ConnectionFactory');
188
189
        $connectionPoolParams = array('randomizeHosts' => false);
190
        $connectionPool = new SniffingConnectionPool($connections, $selector, $connectionFactory, $connectionPoolParams);
191
192
        $retConnection = $connectionPool->nextConnection();
193
194
        $this->assertEquals($connections[9], $retConnection);
195
196
    }
197
198
199
    /**
200
     * @expectedException Elasticsearch\Common\Exceptions\NoNodesAvailableException
201
     */
202
    public function testAddTenNodesAllTimeout()
203
    {
204
        $connections = array();
205
206
        foreach (range(1,10) as $index) {
207
            $mockConnection = m::mock('\Elasticsearch\Connections\GuzzleConnection')
208
                              ->shouldReceive('ping')
209
                              ->andReturn(false)
210
                              ->getMock()
211
                              ->shouldReceive('isAlive')
212
                              ->andReturn(false)
213
                              ->getMock();
214
215
            $connections[] = $mockConnection;
216
        }
217
218
        $selector = m::mock('\Elasticsearch\ConnectionPool\Selectors\RoundRobinSelector')
219
                    ->shouldReceive('select')
220
                    ->andReturnValues($connections)
221
                    ->getMock();
222
223
        $connectionFactory = m::mock('\Elasticsearch\Connections\ConnectionFactory');
224
225
        $connectionPoolParams = array('randomizeHosts' => false);
226
        $connectionPool = new SniffingConnectionPool($connections, $selector, $connectionFactory, $connectionPoolParams);
227
228
        $retConnection = $connectionPool->nextConnection();
229
230
    }
231
232
    public function testAddOneHostSniffTwo()
233
    {
234
        $clusterState['text'] = '{"ok":true,"cluster_name":"elasticsearch_zach","nodes":{"node1":{"name":"Vesta","transport_address":"inet[/192.168.1.119:9300]","hostname":"zach-ThinkPad-W530","version":"0.90.5","http_address":"inet[/192.168.1.119:9200]"}, "node2":{"name":"Vesta","transport_address":"inet[/192.168.1.119:9301]","hostname":"zach-ThinkPad-W530","version":"0.90.5","http_address":"inet[/192.168.1.119:9201]"}}}';
235
236
        $mockConnection = m::mock('\Elasticsearch\Connections\GuzzleConnection')
237
                          ->shouldReceive('ping')->andReturn(true)->getMock()
238
                          ->shouldReceive('isAlive')->andReturn(true)->getMock()
239
                          ->shouldReceive('getTransportSchema')->twice()->andReturn('http')->getMock()
240
                          ->shouldReceive('sniff')->twice()->andReturn($clusterState)->getMock();
241
242
        $connections = array($mockConnection);
243
244
        $newConnections = array();
245
        $newConnections[] = m::mock('\Elasticsearch\Connections\GuzzleConnection')
246
                             ->shouldReceive('isAlive')->andReturn(true)->getMock();
247
248
        $newConnections[] = m::mock('\Elasticsearch\Connections\GuzzleConnection')
249
                             ->shouldReceive('isAlive')->andReturn(true)->getMock();
250
251
        $selector = m::mock('\Elasticsearch\ConnectionPool\Selectors\RoundRobinSelector')
252
                    ->shouldReceive('select')
253
                    ->andReturnValues(array(        //selects provided node first, then the new cluster list
254
                            $mockConnection,
255
                            $newConnections[0],
256
                            $newConnections[1]
257
                    ))
258
                    ->getMock();
259
260
        $connectionFactory = m::mock('\Elasticsearch\Connections\ConnectionFactory')
261
                             ->shouldReceive('create')->with(array('host' => '192.168.1.119', 'port' => 9200))->andReturn($newConnections[0])->getMock()
262
                             ->shouldReceive('create')->with(array('host' => '192.168.1.119', 'port' => 9201))->andReturn($newConnections[1])->getMock();
263
264
        $connectionPoolParams = array(
265
            'randomizeHosts' => false,
266
            'sniffingInterval'  => -1
267
        );
268
        $connectionPool = new SniffingConnectionPool($connections, $selector, $connectionFactory, $connectionPoolParams);
269
270
        $retConnection = $connectionPool->nextConnection();
271
        $this->assertEquals($newConnections[0], $retConnection);
272
273
        $retConnection = $connectionPool->nextConnection();
274
        $this->assertEquals($newConnections[1], $retConnection);
275
    }
276
277
    /**
278
     * @expectedException Elasticsearch\Common\Exceptions\NoNodesAvailableException
279
     */
280
    public function testAddSeed_SniffTwo_TimeoutTwo()
281
    {
282
        $clusterState['text'] = '{"ok":true,"cluster_name":"elasticsearch_zach","nodes":{"node1":{"name":"Vesta","transport_address":"inet[/192.168.1.119:9300]","hostname":"zach-ThinkPad-W530","version":"0.90.5","http_address":"inet[/192.168.1.119:9200]"}, "node2":{"name":"Vesta","transport_address":"inet[/192.168.1.119:9301]","hostname":"zach-ThinkPad-W530","version":"0.90.5","http_address":"inet[/192.168.1.119:9201]"}}}';
283
284
        $mockConnection = m::mock('\Elasticsearch\Connections\GuzzleConnection')
285
                          ->shouldReceive('ping')->andReturn(true)->getMock()
286
                          ->shouldReceive('isAlive')->andReturn(true)->getMock()
287
                          ->shouldReceive('getTransportSchema')->once()->andReturn('http')->getMock()
288
                          ->shouldReceive('sniff')->once()->andReturn($clusterState)->getMock();
289
290
        $connections = array($mockConnection);
291
292
        $newConnections = array();
293
        $newConnections[] = m::mock('\Elasticsearch\Connections\GuzzleConnection')
294
                            ->shouldReceive('isAlive')->andReturn(false)->getMock()
295
                            ->shouldReceive('ping')->andReturn(false)->getMock();
296
297
        $newConnections[] = m::mock('\Elasticsearch\Connections\GuzzleConnection')
298
                            ->shouldReceive('isAlive')->andReturn(false)->getMock()
299
                            ->shouldReceive('ping')->andReturn(false)->getMock();
300
301
        $selector = m::mock('\Elasticsearch\ConnectionPool\Selectors\RoundRobinSelector')
302
                    ->shouldReceive('select')
303
                    ->andReturnValues(array(        //selects provided node first, then the new cluster list
304
                    $mockConnection,
305
                    $newConnections[0],
306
                    $newConnections[1]
307
                ))
308
                    ->getMock();
309
310
        $connectionFactory = m::mock('\Elasticsearch\Connections\ConnectionFactory')
311
                             ->shouldReceive('create')->with(array('host' => '192.168.1.119', 'port' => 9200))->andReturn($newConnections[0])->getMock()
312
                             ->shouldReceive('create')->with(array('host' => '192.168.1.119', 'port' => 9201))->andReturn($newConnections[1])->getMock();
313
314
        $connectionPoolParams = array(
315
            'randomizeHosts' => false,
316
            'sniffingInterval'  => -1
317
        );
318
        $connectionPool = new SniffingConnectionPool($connections, $selector, $connectionFactory, $connectionPoolParams);
319
320
        $retConnection = $connectionPool->nextConnection();
321
        $this->assertEquals($mockConnection, $retConnection);
322
323
    }
324
325
326
    public function testTen_TimeoutNine_SniffTenth_AddTwoAlive()
327
    {
328
        $clusterState['text'] = '{"ok":true,"cluster_name":"elasticsearch_zach","nodes":{"node1":{"name":"Vesta","transport_address":"inet[/192.168.1.119:9300]","hostname":"zach-ThinkPad-W530","version":"0.90.5","http_address":"inet[/192.168.1.119:9200]"}, "node2":{"name":"Vesta","transport_address":"inet[/192.168.1.119:9301]","hostname":"zach-ThinkPad-W530","version":"0.90.5","http_address":"inet[/192.168.1.119:9201]"}}}';
329
330
        $connections = array();
331
332
        foreach (range(1,10) as $index) {
333
            $mockConnection = m::mock('\Elasticsearch\Connections\GuzzleConnection')
334
                              ->shouldReceive('ping')->andReturn(false)->getMock()
335
                              ->shouldReceive('isAlive')->andReturn(true)->getMock()
336
                              ->shouldReceive('sniff')->andThrow('Elasticsearch\Common\Exceptions\Curl\OperationTimeoutException')->getMock();
337
338
            $connections[] = $mockConnection;
339
        }
340
341
        $mockConnection = m::mock('\Elasticsearch\Connections\GuzzleConnection')
342
                          ->shouldReceive('ping')->andReturn(true)->getMock()
343
                          ->shouldReceive('isAlive')->andReturn(true)->getMock()
344
                          ->shouldReceive('sniff')->andReturn($clusterState)->getMock()
345
                          ->shouldReceive('getTransportSchema')->twice()->andReturn('http')->getMock();
346
347
        $connections[] = $mockConnection;
348
349
        $newConnections = $connections;
350
        $newConnections[] = m::mock('\Elasticsearch\Connections\GuzzleConnection')
351
                            ->shouldReceive('isAlive')->andReturn(true)->getMock()
352
                            ->shouldReceive('ping')->andReturn(true)->getMock();
353
354
        $newConnections[] = m::mock('\Elasticsearch\Connections\GuzzleConnection')
355
                            ->shouldReceive('isAlive')->andReturn(true)->getMock()
356
                            ->shouldReceive('ping')->andReturn(true)->getMock();
357
358
        $selector = m::mock('\Elasticsearch\ConnectionPool\Selectors\RoundRobinSelector')
359
                    ->shouldReceive('select')
360
                    ->andReturnValues($newConnections)
361
                    ->getMock();
362
363
        $connectionFactory = m::mock('\Elasticsearch\Connections\ConnectionFactory')
364
                             ->shouldReceive('create')->with(array('host' => '192.168.1.119', 'port' => 9200))->andReturn($newConnections[10])->getMock()
365
                             ->shouldReceive('create')->with(array('host' => '192.168.1.119', 'port' => 9201))->andReturn($newConnections[11])->getMock();
366
367
        $connectionPoolParams = array(
368
            'randomizeHosts' => false,
369
            'sniffingInterval'  => -1
370
        );
371
        $connectionPool = new SniffingConnectionPool($connections, $selector, $connectionFactory, $connectionPoolParams);
372
373
        $retConnection = $connectionPool->nextConnection();
374
        $this->assertEquals($newConnections[11], $retConnection);
375
376
        $retConnection = $connectionPool->nextConnection();
377
        $this->assertEquals($newConnections[12], $retConnection);
378
379
    }
380
381
    /**
382
     * @expectedException Elasticsearch\Common\Exceptions\NoNodesAvailableException
383
     */
384
    public function testTen_TimeoutNine_SniffTenth_AddTwoDead_TimeoutEveryone()
385
    {
386
        $clusterState['text'] = '{"ok":true,"cluster_name":"elasticsearch_zach","nodes":{"node1":{"name":"Vesta","transport_address":"inet[/192.168.1.119:9300]","hostname":"zach-ThinkPad-W530","version":"0.90.5","http_address":"inet[/192.168.1.119:9200]"}, "node2":{"name":"Vesta","transport_address":"inet[/192.168.1.119:9301]","hostname":"zach-ThinkPad-W530","version":"0.90.5","http_address":"inet[/192.168.1.119:9201]"}}}';
387
388
        $connections = array();
389
390
        foreach (range(1,10) as $index) {
391
            $mockConnection = m::mock('\Elasticsearch\Connections\GuzzleConnection')
392
                              ->shouldReceive('ping')->andReturn(false)->getMock()
393
                              ->shouldReceive('isAlive')->andReturn(true)->getMock()
394
                              ->shouldReceive('sniff')->andThrow('Elasticsearch\Common\Exceptions\Curl\OperationTimeoutException')->getMock();
395
396
            $connections[] = $mockConnection;
397
        }
398
399
        $mockConnection = m::mock('\Elasticsearch\Connections\GuzzleConnection')
400
                          ->shouldReceive('ping')->andReturn(true)->getMock()
401
                          ->shouldReceive('isAlive')->andReturn(true)->getMock()
402
                          ->shouldReceive('sniff')->andReturn($clusterState)->getMock()
403
                          ->shouldReceive('getTransportSchema')->once()->andReturn('http')->getMock()
404
                          ->shouldReceive('sniff')->andThrow('Elasticsearch\Common\Exceptions\Curl\OperationTimeoutException')->getMock();
405
406
        $connections[] = $mockConnection;
407
408
        $newConnections = $connections;
409
        $newConnections[] = m::mock('\Elasticsearch\Connections\GuzzleConnection')
410
                            ->shouldReceive('isAlive')->andReturn(false)->getMock()
411
                            ->shouldReceive('ping')->andReturn(false)->getMock()
412
                            ->shouldReceive('sniff')->andThrow('Elasticsearch\Common\Exceptions\Curl\OperationTimeoutException')->getMock();
413
414
        $newConnections[] = m::mock('\Elasticsearch\Connections\GuzzleConnection')
415
                            ->shouldReceive('isAlive')->andReturn(false)->getMock()
416
                            ->shouldReceive('ping')->andReturn(false)->getMock()
417
                            ->shouldReceive('sniff')->andThrow('Elasticsearch\Common\Exceptions\Curl\OperationTimeoutException')->getMock();
418
419
        $selector = m::mock('\Elasticsearch\ConnectionPool\Selectors\RoundRobinSelector')
420
                    ->shouldReceive('select')
421
                    ->andReturnValues($newConnections)
422
                    ->getMock();
423
424
        $RRConnections = $newConnections;
425
        //array_push($connections);
426
        $connectionFactory = m::mock('\Elasticsearch\Connections\ConnectionFactory')
427
                             ->shouldReceive('create')->with(array('host' => '192.168.1.119', 'port' => 9200))->andReturn($newConnections[10])->getMock()
428
                             ->shouldReceive('create')->with(array('host' => '192.168.1.119', 'port' => 9201))->andReturn($newConnections[11])->getMock();
429
430
        $connectionPoolParams = array(
431
            'randomizeHosts' => false,
432
            'sniffingInterval'  => -1
433
        );
434
        $connectionPool = new SniffingConnectionPool($connections, $selector, $connectionFactory, $connectionPoolParams);
435
436
        $retConnection = $connectionPool->nextConnection();
437
        $this->assertEquals($newConnections[11], $retConnection);
438
439
        $retConnection = $connectionPool->nextConnection();
440
        $this->assertEquals($newConnections[12], $retConnection);
441
442
    }
443
}