Skip to content

Commit

Permalink
fix: Make logger test dynamic due to different logs when using neo4j …
Browse files Browse the repository at this point in the history
…vs bolt schemes
  • Loading branch information
exaby73 committed Oct 16, 2024
1 parent a33599a commit 9a75b9a
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 28 deletions.
44 changes: 35 additions & 9 deletions src/Neo4j/Neo4jConnectionPool.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
use Laudis\Neo4j\Enum\AccessMode;
use Laudis\Neo4j\Enum\RoutingRoles;
use Psr\Http\Message\UriInterface;
use Psr\Log\LogLevel;
use Psr\SimpleCache\CacheInterface;

use function random_int;
Expand Down Expand Up @@ -78,8 +79,13 @@ public function __construct(
private readonly ?Neo4jLogger $logger,
) {}

public static function create(UriInterface $uri, AuthenticateInterface $auth, DriverConfiguration $conf, AddressResolverInterface $resolver, SemaphoreInterface $semaphore): self
{
public static function create(
UriInterface $uri,
AuthenticateInterface $auth,
DriverConfiguration $conf,
AddressResolverInterface $resolver,
SemaphoreInterface $semaphore
): self {
return new self(
$semaphore,
BoltFactory::create($conf->getLogger()),
Expand Down Expand Up @@ -128,14 +134,14 @@ public function acquire(SessionConfiguration $config): Generator
$latestError = null;

if ($table == null) {
$addresses = (function () {
yield gethostbyname($this->data->getUri()->getHost());
yield from $this->resolver->getAddresses($this->data->getUri()->getHost());
})();
$addresses = $this->getAddresses($this->data->getUri()->getHost());
foreach ($addresses as $address) {
$triedAddresses[] = $address;

$pool = $this->createOrGetPool($this->data->getUri()->getHost(), $this->data->getUri()->withHost($address));
$pool = $this->createOrGetPool(
$this->data->getUri()->getHost(),
$this->data->getUri()->withHost($address)
);
try {
/** @var BoltConnection $connection */
$connection = GeneratorHelper::getReturnFromGenerator($pool->acquire($config));
Expand All @@ -147,6 +153,7 @@ public function acquire(SessionConfiguration $config): Generator
}

$this->cache->set($key, $table, $table->getTtl());
// TODO: release probably logs off the connection, it is not preferable
$pool->release($connection);

break;
Expand Down Expand Up @@ -197,6 +204,7 @@ private function routingTable(BoltConnection $connection, SessionConfiguration $
{
$bolt = $connection->protocol();

$this->getLogger()?->log(LogLevel::DEBUG, 'ROUTE', ['db' => $config->getDatabase()]);
/** @var array{rt: array{servers: list<array{addresses: list<string>, role:string}>, ttl: int}} $route */
$route = $bolt->route([], [], ['db' => $config->getDatabase()])
->getResponse()
Expand All @@ -210,7 +218,9 @@ private function routingTable(BoltConnection $connection, SessionConfiguration $

public function release(ConnectionInterface $connection): void
{
$this->createOrGetPool($connection->getServerAddress()->getHost(), $connection->getServerAddress())->release($connection);
$this->createOrGetPool($connection->getServerAddress()->getHost(), $connection->getServerAddress())->release(
$connection
);
}

private function createKey(ConnectionRequestData $data, ?SessionConfiguration $config = null): string
Expand All @@ -219,7 +229,14 @@ private function createKey(ConnectionRequestData $data, ?SessionConfiguration $c

$key = implode(
':',
array_filter([$data->getUserAgent(), $uri->getHost(), $config ? $config->getDatabase() : null, $uri->getPort() ?? '7687'])
array_filter(
[
$data->getUserAgent(),
$uri->getHost(),
$config ? $config->getDatabase() : null,
$uri->getPort() ?? '7687',
]
)
);

return str_replace([
Expand All @@ -242,4 +259,13 @@ public function close(): void
self::$pools = [];
$this->cache->clear();
}

/**
* @return Generator<string>
*/
private function getAddresses(string $host): Generator
{
yield gethostbyname($host);
yield from $this->resolver->getAddresses($host);
}
}
55 changes: 36 additions & 19 deletions tests/Integration/Neo4jLoggerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,31 +55,14 @@ public function testLogger(): void
],
],
];
$logger->expects(self::exactly(3))->method('info')->willReturnCallback(
$logger->expects(self::exactly(count($expectedInfoLogs)))->method('info')->willReturnCallback(
static function (string $message, array $context) use (&$infoLogs) {
$infoLogs[] = [$message, $context];
}
);

$debugLogs = [];
$expectedDebugLogs = [
[
'HELLO',
[
'user_agent' => 'neo4j-php-client/2',
],
],
[
'LOGON',
[
'scheme' => 'basic',
'principal' => 'neo4j',
],
],
[
'GOODBYE',
[],
],
[
'HELLO',
[
Expand All @@ -104,7 +87,41 @@ static function (string $message, array $context) use (&$infoLogs) {
[],
],
];
$logger->expects(self::exactly(7))->method('debug')->willReturnCallback(

if ($this->getUri()->getScheme() === 'neo4j') {
array_splice(
$expectedDebugLogs,
0,
0,
[
[
'HELLO',
[
'user_agent' => 'neo4j-php-client/2',
],
],
[
'LOGON',
[
'scheme' => 'basic',
'principal' => 'neo4j',
],
],
[
'ROUTE',
[
'db' => null,
],
],
[
'GOODBYE',
[],
],
],
);
}

$logger->expects(self::exactly(count($expectedDebugLogs)))->method('debug')->willReturnCallback(
static function (string $message, array $context) use (&$debugLogs) {
$debugLogs[] = [$message, $context];
}
Expand Down

0 comments on commit 9a75b9a

Please sign in to comment.