Skip to content

Commit 81492d6

Browse files
committed
tested if the drivers unnecesarily opened connections
1 parent 5c08909 commit 81492d6

File tree

3 files changed

+47
-2
lines changed

3 files changed

+47
-2
lines changed

src/Common/DriverSetupManager.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
namespace Laudis\Neo4j\Common;
1313

14-
use Laudis\Neo4j\Databags\SessionConfiguration;
1514
use function array_key_exists;
1615
use function array_key_first;
1716
use function array_reduce;
@@ -22,6 +21,7 @@
2221
use Laudis\Neo4j\Contracts\FormatterInterface;
2322
use Laudis\Neo4j\Databags\DriverConfiguration;
2423
use Laudis\Neo4j\Databags\DriverSetup;
24+
use Laudis\Neo4j\Databags\SessionConfiguration;
2525
use Laudis\Neo4j\DriverFactory;
2626
use const PHP_INT_MIN;
2727
use RuntimeException;

src/Http/HttpDriver.php

+1
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ public function createSession(?SessionConfiguration $config = null): SessionInte
128128
public function verifyConnectivity(?SessionConfiguration $config = null): bool
129129
{
130130
$config ??= SessionConfiguration::default();
131+
131132
return $this->getHttpConnectionPool($this->tsxUrl($config))
132133
->canConnect($this->uri, $this->auth);
133134
}

tests/Integration/ClientIntegrationTest.php

+45-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
use InvalidArgumentException;
1818
use Laudis\Neo4j\Authentication\Authenticate;
1919
use Laudis\Neo4j\Basic\Driver;
20+
use Laudis\Neo4j\Bolt\BoltDriver;
21+
use Laudis\Neo4j\Bolt\ConnectionPool;
2022
use Laudis\Neo4j\ClientBuilder;
2123
use Laudis\Neo4j\Common\Uri;
2224
use Laudis\Neo4j\Contracts\FormatterInterface;
@@ -27,10 +29,11 @@
2729
use Laudis\Neo4j\Formatter\OGMFormatter;
2830
use Laudis\Neo4j\Types\CypherList;
2931
use Laudis\Neo4j\Types\CypherMap;
32+
use ReflectionClass;
3033
use function str_starts_with;
3134

3235
/**
33-
* @psalm-import-type OGMTypes from \Laudis\Neo4j\Formatter\OGMFormatter
36+
* @psalm-import-type OGMTypes from OGMFormatter
3437
*
3538
* @extends EnvironmentAwareIntegrationTest<CypherList<CypherMap<OGMTypes>>>
3639
*/
@@ -355,4 +358,45 @@ public function fetchSize(string $connection, int $fetchSize): void
355358

356359
$this->assertCount($nodesAmount, $uniqueUserIds);
357360
}
361+
362+
public function testRedundantAcquire(): void
363+
{
364+
$connections = self::buildConnections();
365+
366+
$builder = ClientBuilder::create();
367+
foreach ($connections as $i => $connection) {
368+
$uri = Uri::create($connection);
369+
$alias = $uri->getScheme().'_'.$i;
370+
$builder = $builder->withDriver($alias, $connection);
371+
}
372+
373+
$client = $builder->withFormatter(ClientIntegrationTest::formatter())
374+
->withDefaultSessionConfiguration(SessionConfiguration::default()->withDatabase('neo4j'))
375+
->build();
376+
377+
foreach ($connections as $i => $connection) {
378+
$uri = Uri::create($connection);
379+
$alias = $uri->getScheme().'_'.$i;
380+
$client->run('MATCH (x) RETURN x', [], $alias);
381+
382+
$driver = $client->getDriver($alias);
383+
384+
// We make sure there is no redundant acquire by testing the amount of open connections.
385+
// These may never exceed 1 in this simple case.
386+
if ($driver instanceof BoltDriver) {
387+
$reflection = new ReflectionClass($driver);
388+
389+
$poolProp = $reflection->getProperty('pool');
390+
/** @var ConnectionPool $pool */
391+
$pool = $poolProp->getValue($driver);
392+
393+
$reflection = new ReflectionClass($pool);
394+
$connectionProp = $reflection->getProperty('activeConnections');
395+
/** @var array $activeConnections */
396+
$activeConnections = $connectionProp->getValue($pool);
397+
398+
$this->assertCount(1, $activeConnections);
399+
}
400+
}
401+
}
358402
}

0 commit comments

Comments
 (0)