Skip to content

Commit

Permalink
Transport.php fix backported into 6.8.x (#1109)
Browse files Browse the repository at this point in the history
* Update Transport.php

* Fix 6.8.0 release adding missing class alises (#1114)

* Fix for #1112 issue. Adding endpoint aliases + IndicesNamespace::getAliases()

* Reverted array alias to class in src/autoload.php + added unit tests for missing class aliases in 6.8

* Fixed indentation in src/ autoload.php

* Added TransportTest

Co-authored-by: Enrico Zimuel <[email protected]>
  • Loading branch information
potemkin-git and ezimuel authored Mar 22, 2021
1 parent 285ed33 commit 7f2525f
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/Elasticsearch/Transport.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,9 @@ function ($response) {
},
//onFailure
function ($response) {
$code = $response->getCode();
// Ignore 400 level errors, as that means the server responded just fine
if (!(isset($response['code']) && $response['code'] >=400 && $response['code'] < 500)) {
if ($code < 400 || $code >= 500) {
// Otherwise schedule a check
$this->connectionPool->scheduleCheck();
}
Expand Down
84 changes: 84 additions & 0 deletions tests/Elasticsearch/Tests/TransportTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php
/**
* Elasticsearch PHP client
*
* @link https://github.com/elastic/elasticsearch-php/
* @copyright Copyright (c) Elasticsearch B.V (https://www.elastic.co)
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
* @license https://www.gnu.org/licenses/lgpl-2.1.html GNU Lesser General Public License, Version 2.1
*
* Licensed to Elasticsearch B.V under one or more agreements.
* Elasticsearch B.V licenses this file to you under the Apache 2.0 License or
* the GNU Lesser General Public License, Version 2.1, at your option.
* See the LICENSE file in the project root for more information.
*/


declare(strict_types = 1);

namespace Elasticsearch\Tests;

use Elasticsearch\Common\Exceptions\ServerErrorResponseException;
use Elasticsearch\ConnectionPool\AbstractConnectionPool;
use Elasticsearch\Connections\Connection;
use Elasticsearch\Serializers\SerializerInterface;
use Elasticsearch\Transport;
use GuzzleHttp\Ring\Future\FutureArray;
use GuzzleHttp\Ring\Future\FutureArrayInterface;
use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;
use React\Promise\Deferred;

class TransportTest extends TestCase
{
public function setUp(): void
{
$this->logger = $this->createMock(LoggerInterface::class);
$this->trace = $this->createMock(LoggerInterface::class);
$this->serializer = $this->createMock(SerializerInterface::class);
$this->connectionPool = $this->createMock(AbstractConnectionPool::class);
$this->connection = $this->createMock(Connection::class);
}

public function testPerformRequestWithServerErrorResponseException404Result()
{
$deferred = new Deferred();
$deferred->reject(new ServerErrorResponseException('foo', 404));
$future = new FutureArray($deferred->promise());

$this->connection->method('performRequest')
->willReturn($future);

$this->connectionPool->method('nextConnection')
->willReturn($this->connection);

$this->connectionPool->expects($this->never())
->method('scheduleCheck');

$transport = new Transport(1, $this->connectionPool, $this->logger);

$result = $transport->performRequest('GET', '/');
$this->assertInstanceOf(FutureArrayInterface::class, $result);
}

public function testPerformRequestWithServerErrorResponseException500Result()
{
$deferred = new Deferred();
$deferred->reject(new ServerErrorResponseException('foo', 500));
$future = new FutureArray($deferred->promise());

$this->connection->method('performRequest')
->willReturn($future);

$this->connectionPool->method('nextConnection')
->willReturn($this->connection);

$this->connectionPool->expects($this->once())
->method('scheduleCheck');

$transport = new Transport(1, $this->connectionPool, $this->logger);

$result = $transport->performRequest('GET', '/');
$this->assertInstanceOf(FutureArrayInterface::class, $result);
}
}

0 comments on commit 7f2525f

Please sign in to comment.