1<?php 2 3declare(strict_types = 1); 4 5namespace Elasticsearch\Tests\ConnectionPool\Selectors; 6 7use Elasticsearch; 8use Elasticsearch\Connections\ConnectionInterface; 9 10/** 11 * Class SnifferTest 12 * 13 * @category Tests 14 * @package Elasticsearch 15 * @subpackage Tests\ConnectionPool\RoundRobinSelectorTest 16 * @author Zachary Tong <zachary.tong@elasticsearch.com> 17 * @license http://www.apache.org/licenses/LICENSE-2.0 Apache2 18 * @link http://elasticsearch.org 19 */ 20class RoundRobinSelectorTest extends \PHPUnit\Framework\TestCase 21{ 22 /** 23 * Add Ten connections, select 15 to verify round robin 24 * 25 * @covers \Elasticsearch\ConnectionPool\Selectors\RoundRobinSelector::select 26 * 27 * @return void 28 */ 29 public function testTenConnections() 30 { 31 $roundRobin = new Elasticsearch\ConnectionPool\Selectors\RoundRobinSelector(); 32 33 $mockConnections = []; 34 foreach (range(0, 9) as $index) { 35 $mockConnections[$index] = $this->getMockBuilder(ConnectionInterface::class) 36 ->disableOriginalConstructor() 37 ->getMock(); 38 } 39 40 // select ten 41 $this->assertSame($mockConnections[0], $roundRobin->select($mockConnections)); 42 $this->assertSame($mockConnections[1], $roundRobin->select($mockConnections)); 43 $this->assertSame($mockConnections[2], $roundRobin->select($mockConnections)); 44 $this->assertSame($mockConnections[3], $roundRobin->select($mockConnections)); 45 $this->assertSame($mockConnections[4], $roundRobin->select($mockConnections)); 46 $this->assertSame($mockConnections[5], $roundRobin->select($mockConnections)); 47 $this->assertSame($mockConnections[6], $roundRobin->select($mockConnections)); 48 $this->assertSame($mockConnections[7], $roundRobin->select($mockConnections)); 49 $this->assertSame($mockConnections[8], $roundRobin->select($mockConnections)); 50 $this->assertSame($mockConnections[9], $roundRobin->select($mockConnections)); 51 52 // select five - should start from the first one (index: 0) 53 $this->assertSame($mockConnections[0], $roundRobin->select($mockConnections)); 54 $this->assertSame($mockConnections[1], $roundRobin->select($mockConnections)); 55 $this->assertSame($mockConnections[2], $roundRobin->select($mockConnections)); 56 $this->assertSame($mockConnections[3], $roundRobin->select($mockConnections)); 57 $this->assertSame($mockConnections[4], $roundRobin->select($mockConnections)); 58 } 59 60 /** 61 * Add Ten connections, select five, remove three, test another 10 to check 62 * that the round-robining works after removing connections 63 * 64 * @covers \Elasticsearch\ConnectionPool\Selectors\RoundRobinSelector::select 65 * 66 * @return void 67 */ 68 public function testAddTenConnectionsTestFiveRemoveThreeTestTen() 69 { 70 $roundRobin = new Elasticsearch\ConnectionPool\Selectors\RoundRobinSelector(); 71 72 $mockConnections = []; 73 foreach (range(0, 9) as $index) { 74 $mockConnections[$index] = $this->getMockBuilder(ConnectionInterface::class) 75 ->disableOriginalConstructor() 76 ->getMock(); 77 } 78 79 // select five 80 $this->assertSame($mockConnections[0], $roundRobin->select($mockConnections)); 81 $this->assertSame($mockConnections[1], $roundRobin->select($mockConnections)); 82 $this->assertSame($mockConnections[2], $roundRobin->select($mockConnections)); 83 $this->assertSame($mockConnections[3], $roundRobin->select($mockConnections)); 84 $this->assertSame($mockConnections[4], $roundRobin->select($mockConnections)); 85 86 // remove three 87 unset($mockConnections[8]); 88 unset($mockConnections[9]); 89 unset($mockConnections[10]); 90 91 // select ten after removal 92 $this->assertSame($mockConnections[5], $roundRobin->select($mockConnections)); 93 $this->assertSame($mockConnections[6], $roundRobin->select($mockConnections)); 94 $this->assertSame($mockConnections[7], $roundRobin->select($mockConnections)); 95 $this->assertSame($mockConnections[0], $roundRobin->select($mockConnections)); 96 $this->assertSame($mockConnections[1], $roundRobin->select($mockConnections)); 97 $this->assertSame($mockConnections[2], $roundRobin->select($mockConnections)); 98 $this->assertSame($mockConnections[3], $roundRobin->select($mockConnections)); 99 $this->assertSame($mockConnections[4], $roundRobin->select($mockConnections)); 100 $this->assertSame($mockConnections[5], $roundRobin->select($mockConnections)); 101 $this->assertSame($mockConnections[6], $roundRobin->select($mockConnections)); 102 } 103} 104