diff --git a/src/Elasticsearch/Transport.php b/src/Elasticsearch/Transport.php index f436b0bf3..e27d9fdbd 100644 --- a/src/Elasticsearch/Transport.php +++ b/src/Elasticsearch/Transport.php @@ -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(); } diff --git a/tests/Elasticsearch/Tests/TransportTest.php b/tests/Elasticsearch/Tests/TransportTest.php new file mode 100644 index 000000000..84923c167 --- /dev/null +++ b/tests/Elasticsearch/Tests/TransportTest.php @@ -0,0 +1,84 @@ +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); + } +}