Skip to content

Commit 8f08df3

Browse files
committed
white box test to test connection recycling
1 parent b7010b6 commit 8f08df3

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

src/Bolt/BoltUnmanagedTransaction.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,4 +163,9 @@ private function getBolt(): Bolt
163163
{
164164
return $this->connection->getImplementation();
165165
}
166+
167+
public function __destruct()
168+
{
169+
$this->connection->close();
170+
}
166171
}

src/Http/HttpUnmanagedTransaction.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,4 +130,9 @@ public function rollback(): void
130130

131131
HttpHelper::interpretResponse($response);
132132
}
133+
134+
public function __destruct()
135+
{
136+
$this->connection->close();
137+
}
133138
}

tests/Integration/TransactionIntegrationTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,15 @@
1313

1414
namespace Laudis\Neo4j\Tests\Integration;
1515

16+
use Laudis\Neo4j\Bolt\BoltConnectionPool;
17+
use Laudis\Neo4j\Bolt\BoltDriver;
1618
use Laudis\Neo4j\Contracts\FormatterInterface;
1719
use Laudis\Neo4j\Databags\Statement;
1820
use Laudis\Neo4j\Exception\InvalidTransactionStateException;
1921
use Laudis\Neo4j\Exception\Neo4jException;
2022
use Laudis\Neo4j\Formatter\BasicFormatter;
23+
use ReflectionClass;
24+
use function str_starts_with;
2125

2226
/**
2327
* @psalm-import-type BasicResults from \Laudis\Neo4j\Formatter\BasicFormatter
@@ -298,4 +302,38 @@ public function testRollbackInvalid(string $alias): void
298302
}
299303
self::assertTrue($exception);
300304
}
305+
306+
/**
307+
* @dataProvider connectionAliases
308+
* @noinspection PhpUnusedLocalVariableInspection
309+
* @psalm-suppress UnusedVariable
310+
*/
311+
public function testCorrectConnectionReuse(string $alias): void
312+
{
313+
$driver = $this->getClient()->getDriver($alias);
314+
if (!$driver instanceof BoltDriver) {
315+
self::markTestSkipped('Can only white box test bolt driver');
316+
}
317+
318+
$poolReflection = new ReflectionClass(BoltConnectionPool::class);
319+
$poolReflection->setStaticPropertyValue('connectionCache', []);
320+
321+
$this->getClient()->run('MATCH (x) RETURN x', [], $alias);
322+
$this->getClient()->run('MATCH (x) RETURN x', [], $alias);
323+
$this->getClient()->run('MATCH (x) RETURN x', [], $alias);
324+
$this->getClient()->run('MATCH (x) RETURN x', [], $alias);
325+
$a = $this->getClient()->beginTransaction([], $alias);
326+
$b = $this->getClient()->beginTransaction([], $alias);
327+
$this->getClient()->run('MATCH (x) RETURN x', [], $alias);
328+
329+
$poolReflection = new ReflectionClass(BoltConnectionPool::class);
330+
/** @var array $cache */
331+
$cache = $poolReflection->getStaticPropertyValue('connectionCache');
332+
333+
$key = array_key_first($cache);
334+
self::assertIsString($key);
335+
self::assertArrayHasKey($key, $cache);
336+
/** @psalm-suppress MixedArgument */
337+
self::assertCount(3, $cache[$key]);
338+
}
301339
}

0 commit comments

Comments
 (0)