1<?php
2
3declare(strict_types = 1);
4
5namespace Elasticsearch\Tests\ConnectionPool\Selectors;
6
7use Elasticsearch;
8use Elasticsearch\Connections\ConnectionInterface;
9use Mockery as m;
10
11/**
12 * Class StickyRoundRobinSelectorTest
13 *
14 * @category   Tests
15 * @package    Elasticsearch
16 * @subpackage Tests\ConnectionPool\StickyRoundRobinSelectorTest
17 * @author     Zachary Tong <zachary.tong@elasticsearch.com>
18 * @license    http://www.apache.org/licenses/LICENSE-2.0 Apache2
19 * @link       http://elasticsearch.org
20 */
21class StickyRoundRobinSelectorTest extends \PHPUnit\Framework\TestCase
22{
23    public function tearDown()
24    {
25        m::close();
26    }
27
28    public function testTenConnections()
29    {
30        $roundRobin = new Elasticsearch\ConnectionPool\Selectors\StickyRoundRobinSelector();
31
32        $mockConnections = [];
33        $mockConnections[] = m::mock(ConnectionInterface::class)
34            ->shouldReceive('isAlive')->times(16)->andReturn(true)->getMock();
35
36        foreach (range(0, 9) as $index) {
37            $mockConnections[] = m::mock(ConnectionInterface::class);
38        }
39
40        foreach (range(0, 15) as $index) {
41            $retConnection = $roundRobin->select($mockConnections);
42
43            $this->assertSame($mockConnections[0], $retConnection);
44        }
45    }
46
47    public function testTenConnectionsFirstDies()
48    {
49        $roundRobin = new Elasticsearch\ConnectionPool\Selectors\StickyRoundRobinSelector();
50
51        $mockConnections = [];
52        $mockConnections[] = m::mock(ConnectionInterface::class)
53            ->shouldReceive('isAlive')->once()->andReturn(false)->getMock();
54
55        $mockConnections[] = m::mock(ConnectionInterface::class)
56            ->shouldReceive('isAlive')->times(15)->andReturn(true)->getMock();
57
58        foreach (range(0, 8) as $index) {
59            $mockConnections[] = m::mock(ConnectionInterface::class);
60        }
61
62        foreach (range(0, 15) as $index) {
63            $retConnection = $roundRobin->select($mockConnections);
64
65            $this->assertSame($mockConnections[1], $retConnection);
66        }
67    }
68}
69