Skip to content

Commit

Permalink
chore: add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
imdhemy committed Feb 25, 2024
1 parent ca6a2f7 commit 848dbfb
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/OpenSearch/ConnectionPool/SniffingConnectionPool.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

namespace OpenSearch\ConnectionPool;

use Exception;
use OpenSearch\Common\Exceptions\Curl\OperationTimeoutException;
use OpenSearch\Common\Exceptions\NoNodesAvailableException;
use OpenSearch\ConnectionPool\Selectors\SelectorInterface;
use OpenSearch\Connections\Connection;
Expand Down Expand Up @@ -123,7 +123,7 @@ private function sniffConnection(Connection $connection): bool
{
try {
$response = $connection->sniff();
} catch (Exception $exception) {
} catch (OperationTimeoutException $exception) {
return false;
}

Expand Down
97 changes: 97 additions & 0 deletions tests/ConnectionPool/SniffingConnectionPoolTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

namespace OpenSearch\Tests\ConnectionPool;

use OpenSearch\Common\Exceptions\Curl\OperationTimeoutException;
use OpenSearch\Common\Exceptions\NoNodesAvailableException;
use OpenSearch\ConnectionPool\Selectors\RoundRobinSelector;
use OpenSearch\ConnectionPool\SniffingConnectionPool;
Expand Down Expand Up @@ -179,6 +180,102 @@ public function itShouldFailIfAllNodesAreDown(): void
$connectionPool->nextConnection();
}

/** @test */
public function sniffShouldStopIfAllSniffRequestsFail(): void
{
$connection = $this->createMock(Connection::class);
$connection->method('isAlive')->willReturn(false);
$connection->method('sniff')->willThrowException(new OperationTimeoutException());
$selector = new RoundRobinSelector();
$connectionFactory = $this->createMock(ConnectionFactoryInterface::class);

$connectionPool = new SniffingConnectionPool(
[$connection],
$selector,
$connectionFactory,
['sniffingInterval' => 0]
);

$this->expectException(NoNodesAvailableException::class);
$connectionFactory->expects($this->never())->method('create');

$connectionPool->nextConnection();
}

/** @test */
public function sniffShouldStopIfNodesAreEmpty(): void
{
$clusterState = $this->clusterState(0);
$connection = $this->createMock(Connection::class);
$connection->method('isAlive')->willReturn(false);
$connection->method('sniff')->willReturn($clusterState);
$selector = new RoundRobinSelector();
$connectionFactory = $this->createMock(ConnectionFactoryInterface::class);

$connectionPool = new SniffingConnectionPool(
[$connection],
$selector,
$connectionFactory,
['sniffingInterval' => 0]
);

$this->expectException(NoNodesAvailableException::class);
$connectionFactory->expects($this->never())->method('create');

$connectionPool->nextConnection();
}

/** @test */
public function itShouldNotSniffBeforeScheduledSniffTime(): void
{
$connection = $this->createMock(Connection::class);
$connection->method('isAlive')->willReturn(false);
$connection->method('sniff')->willReturn($this->clusterState(2));
$selector = new RoundRobinSelector();
$connectionFactory = $this->createMock(ConnectionFactoryInterface::class);

$connectionPool = new SniffingConnectionPool(
[$connection],
$selector,
$connectionFactory,
['sniffingInterval' => 300]
);

$connectionFactory->expects($this->never())->method('create');
$this->expectException(NoNodesAvailableException::class);

$connectionPool->nextConnection();
}

/** @test */
public function scheduleCheck(): void
{
$clusterState = $this->clusterState(2);
$firstConnection = $this->createMock(Connection::class);
$firstConnection->method('isAlive')->willReturn(true);
$firstConnection->method('sniff')->willReturn($clusterState);
$secondConnection = $this->createMock(Connection::class);
$secondConnection->method('isAlive')->willReturn(true);
$selector = $this->createMock(RoundRobinSelector::class);
$selector->expects($this->exactly(2))->method('select')->willReturnOnConsecutiveCalls(
$firstConnection,
$secondConnection
);
$connectionFactory = $this->createMock(ConnectionFactoryInterface::class);
$connectionFactory->method('create')->willReturnOnConsecutiveCalls($firstConnection, $secondConnection);

$connectionPool = new SniffingConnectionPool(
[$firstConnection],
$selector,
$connectionFactory,
['sniffingInterval' => 300]
);

$connectionPool->scheduleCheck();

$this->assertSame($secondConnection, $connectionPool->nextConnection());
}

private function clusterState(int $numberOfNodes): array
{
$clusterState = ['nodes' => []];
Expand Down

0 comments on commit 848dbfb

Please sign in to comment.