Skip to content

Commit 4e3dd50

Browse files
committed
fixed testkit test for test_Session_run
1 parent 5f3809f commit 4e3dd50

File tree

6 files changed

+65
-11
lines changed

6 files changed

+65
-11
lines changed

src/Bolt/BoltConnection.php

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ public function close(): void
319319
try {
320320
if ($this->isOpen()) {
321321
if ($this->isStreaming()) {
322-
$this->consumeResults();
322+
$this->discardUnconsumedResults();
323323
}
324324

325325
$message = $this->messageFactory->createGoodbyeMessage();
@@ -405,4 +405,30 @@ private function assertNoFailure(Response $response): void
405405
throw Neo4jException::fromBoltResponse($response);
406406
}
407407
}
408+
409+
/**
410+
* Discard unconsumed results - sends DISCARD to server for each subscribed result.
411+
*/
412+
public function discardUnconsumedResults(): void
413+
{
414+
$this->logger?->log(LogLevel::DEBUG, 'Discarding unconsumed results');
415+
416+
$this->subscribedResults = array_values(array_filter(
417+
$this->subscribedResults,
418+
static fn (WeakReference $ref): bool => $ref->get() !== null
419+
));
420+
421+
if (!empty($this->subscribedResults)) {
422+
try {
423+
$this->discard(null);
424+
$this->logger?->log(LogLevel::DEBUG, 'Sent DISCARD ALL for unconsumed results');
425+
} catch (Throwable $e) {
426+
$this->logger?->log(LogLevel::ERROR, 'Failed to discard results', [
427+
'exception' => $e->getMessage(),
428+
]);
429+
}
430+
}
431+
432+
$this->subscribedResults = [];
433+
}
408434
}

src/Bolt/Session.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939
*/
4040
final class Session implements SessionInterface
4141
{
42+
/** @var list<BoltConnection> */
43+
private array $usedConnections = [];
4244
/** @psalm-readonly */
4345
private readonly BookmarkHolder $bookmarkHolder;
4446

@@ -178,6 +180,7 @@ private function acquireConnection(TransactionConfiguration $config, SessionConf
178180
$timeout = ($timeout < 30) ? 30 : $timeout;
179181
$connection->setTimeout($timeout + 2);
180182
}
183+
$this->usedConnections[] = $connection;
181184

182185
return $connection;
183186
}
@@ -217,6 +220,14 @@ public function getLastBookmark(): Bookmark
217220
return $this->bookmarkHolder->getBookmark();
218221
}
219222

223+
public function close(): void
224+
{
225+
foreach ($this->usedConnections as $connection) {
226+
$connection->discardUnconsumedResults();
227+
}
228+
$this->usedConnections = [];
229+
}
230+
220231
private function getLogger(): ?Neo4jLogger
221232
{
222233
return $this->pool->getLogger();

src/Types/CypherList.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,19 @@ class CypherList implements CypherSequence, Iterator, ArrayAccess
4242
* @use CypherSequenceTrait<TValue>
4343
*/
4444
use CypherSequenceTrait;
45+
private ?int $qid = null;
4546

4647
/**
4748
* @param iterable<mixed, TValue>|callable():Generator<mixed, TValue> $iterable
4849
*
4950
* @psalm-mutation-free
5051
*/
51-
public function __construct(iterable|callable $iterable = [])
52+
public function __construct(iterable|callable $iterable = [], ?int $qid = null)
5253
{
5354
if (is_array($iterable)) {
5455
$iterable = new ArrayIterator($iterable);
5556
}
57+
$this->qid = $qid;
5658

5759
$this->generator = static function () use ($iterable): Generator {
5860
$i = 0;
@@ -425,4 +427,9 @@ public function each(callable $callable): self
425427

426428
return $this;
427429
}
430+
431+
public function getQid(): ?int
432+
{
433+
return $this->qid;
434+
}
428435
}

testkit-backend/src/Handlers/AbstractRunner.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public function __construct(MainRepository $repository, LoggerInterface $logger)
4747
$this->logger = $logger;
4848
}
4949

50-
public function handle($request): ResultResponse
50+
public function handle($request): ResultResponse|DriverErrorResponse
5151
{
5252
$session = $this->getRunner($request);
5353
$id = Uuid::v4();
@@ -80,12 +80,11 @@ public function handle($request): ResultResponse
8080
return new ResultResponse($id, $result->isEmpty() ? [] : $result->first()->keys());
8181
} catch (Neo4jException $exception) {
8282
$this->logger->debug($exception->__toString());
83-
$this->repository->addRecords($id, new DriverErrorResponse(
83+
84+
return new DriverErrorResponse(
8485
$this->getId($request),
8586
$exception
86-
));
87-
88-
return new ResultResponse($id, []);
87+
);
8988
} // NOTE: all other exceptions will be caught in the Backend
9089
}
9190

testkit-backend/src/Handlers/SessionClose.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ public function __construct(MainRepository $repository)
3535
*/
3636
public function handle($request): SessionResponse
3737
{
38+
$session = $this->repository->getSession($request->getSessionId());
39+
40+
if ($session !== null) {
41+
$session->close();
42+
}
3843
$this->repository->removeSession($request->getSessionId());
3944

4045
return new SessionResponse($request->getSessionId());

testkit-backend/testkit.sh

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,17 @@ EXIT_CODE=0
7575

7676
#stub
7777
#test-basic-query
78+
#python3 -m unittest tests.stub.basic_query.test_basic_query.TestBasicQuery.test_4x4_populates_node_element_id_with_id
79+
#python3 -m unittest tests.stub.basic_query.test_basic_query.TestBasicQuery.test_5x0_populates_node_element_id_with_string
80+
#python3 -m unittest tests.stub.basic_query.test_basic_query.TestBasicQuery.test_4x4_populates_rel_element_id_with_id
81+
#python3 -m unittest tests.stub.basic_query.test_basic_query.TestBasicQuery.test_4x4_populates_path_element_ids_with_long
7882

79-
python3 -m unittest tests.stub.basic_query.test_basic_query.TestBasicQuery.test_4x4_populates_node_element_id_with_id
80-
python3 -m unittest tests.stub.basic_query.test_basic_query.TestBasicQuery.test_5x0_populates_node_element_id_with_string
81-
python3 -m unittest tests.stub.basic_query.test_basic_query.TestBasicQuery.test_4x4_populates_rel_element_id_with_id
82-
python3 -m unittest tests.stub.basic_query.test_basic_query.TestBasicQuery.test_4x4_populates_path_element_ids_with_long
83+
84+
#test-session-run
85+
python3 -m unittest tests.stub.session_run.test_session_run.TestSessionRun.test_discard_on_session_close_untouched_result
86+
python3 -m unittest tests.stub.session_run.test_session_run.TestSessionRun.test_discard_on_session_close_unfinished_result
87+
python3 -m unittest tests.stub.session_run.test_session_run.TestSessionRun.test_no_discard_on_session_close_finished_result
88+
python3 -m unittest tests.stub.session_run.test_session_run.TestSessionRun.test_raises_error_on_session_run
8389

8490

8591
exit $EXIT_CODE

0 commit comments

Comments
 (0)