Skip to content

Commit

Permalink
Fix for StickyRoundRobinSelector starts with second host (#1253)
Browse files Browse the repository at this point in the history
* Improved the StickyRoundRobinSelector test

* Fixed StickyRoundRobinSelector starts with second host
  • Loading branch information
ezimuel committed Sep 23, 2022
1 parent e1ed999 commit 5def762
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/Elasticsearch/Connections/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class Connection implements ConnectionInterface
/**
* @var bool
*/
protected $isAlive = false;
protected $isAlive = true;

/**
* @var float
Expand Down
37 changes: 37 additions & 0 deletions tests/Elasticsearch/Tests/ClientBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,16 @@

namespace Elasticsearch\Tests;

use CurlHandle;
use Elasticsearch\Client;
use Elasticsearch\ClientBuilder;
use Elasticsearch\Common\Exceptions\ElasticsearchException;
use Elasticsearch\Common\Exceptions\RuntimeException;
use Elasticsearch\ConnectionPool\Selectors\StickyRoundRobinSelector;
use Elasticsearch\ConnectionPool\StaticNoPingConnectionPool;
use Elasticsearch\Tests\ClientBuilder\DummyLogger;
use GuzzleHttp\Ring\Client\CurlHandler;
use GuzzleHttp\Ring\Client\MockHandler;
use PHPUnit\Framework\TestCase;

class ClientBuilderTest extends TestCase
Expand Down Expand Up @@ -376,4 +381,36 @@ public function testFromConfigWithIncludePortInHostHeader()
$this->assertEquals($url, $request['request']['headers']['Host'][0]);
}
}

/**
* @see https://github.com/elastic/elasticsearch-php/issues/1242
*/
public function testRandomizedHostsDisableWithStickRoundRobinSelectorSelectFirstNode()
{
$hosts = ['one', 'two', 'three'];
$data = '{"foo":"bar}';

$handler = new MockHandler([
'status' => 200,
'transfer_stats' => [
'total_time' => 100
],
'body' => fopen('data:text/plain,'.urlencode($data), 'rb'),
'effective_url' => 'one'
]);

$client = ClientBuilder::create()
->setHosts($hosts)
->setHandler($handler)
->setSelector(StickyRoundRobinSelector::class)
->setConnectionPool(StaticNoPingConnectionPool::class, ['randomizeHosts' => false])
->build();

try {
$result = $client->info();
} catch (ElasticsearchException $e) {
$request = $client->transport->getLastConnection()->getLastRequestInfo();
$this->assertEquals('one', $request['request']['headers']['Host'][0]);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,18 @@
*/
class StickyRoundRobinSelectorTest extends \PHPUnit\Framework\TestCase
{
public function setUp(): void
{
$this->roundRobin = new Elasticsearch\ConnectionPool\Selectors\StickyRoundRobinSelector();
}

public function tearDown(): void
{
m::close();
}

public function testTenConnections()
{
$roundRobin = new Elasticsearch\ConnectionPool\Selectors\StickyRoundRobinSelector();

$mockConnections = [];
$mockConnections[] = m::mock(ConnectionInterface::class)
->shouldReceive('isAlive')->times(16)->andReturn(true)->getMock();
Expand All @@ -47,16 +50,14 @@ public function testTenConnections()
}

foreach (range(0, 15) as $index) {
$retConnection = $roundRobin->select($mockConnections);
$retConnection = $this->roundRobin->select($mockConnections);

$this->assertSame($mockConnections[0], $retConnection);
}
}

public function testTenConnectionsFirstDies()
{
$roundRobin = new Elasticsearch\ConnectionPool\Selectors\StickyRoundRobinSelector();

$mockConnections = [];
$mockConnections[] = m::mock(ConnectionInterface::class)
->shouldReceive('isAlive')->once()->andReturn(false)->getMock();
Expand All @@ -69,7 +70,7 @@ public function testTenConnectionsFirstDies()
}

foreach (range(0, 15) as $index) {
$retConnection = $roundRobin->select($mockConnections);
$retConnection = $this->roundRobin->select($mockConnections);

$this->assertSame($mockConnections[1], $retConnection);
}
Expand Down

0 comments on commit 5def762

Please sign in to comment.