Skip to content

Commit 03eccf6

Browse files
committed
PHPC-2660 Test that killCursors is sent after a connection error during getMore
1 parent 9fa2e51 commit 03eccf6

1 file changed

Lines changed: 74 additions & 0 deletions

File tree

tests/cursor/bug2660-001.phpt

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
--TEST--
2+
PHPC-2660: killCursors is sent after a connection error during getMore
3+
--SKIPIF--
4+
<?php require __DIR__ . "/../utils/basic-skipif.inc"; ?>
5+
<?php skip_if_not_libmongoc_crypto(); ?>
6+
<?php skip_if_test_commands_disabled(); ?>
7+
<?php skip_if_not_live(); ?>
8+
<?php skip_if_not_clean(); ?>
9+
--FILE--
10+
<?php
11+
require_once __DIR__ . "/../utils/basic.inc";
12+
13+
class KillCursorsSubscriber implements MongoDB\Driver\Monitoring\CommandSubscriber
14+
{
15+
public int $killCursorsCount = 0;
16+
17+
public function commandStarted(MongoDB\Driver\Monitoring\CommandStartedEvent $event): void
18+
{
19+
if ($event->getCommandName() === 'killCursors') {
20+
$this->killCursorsCount++;
21+
echo "killCursors was sent\n";
22+
}
23+
}
24+
25+
public function commandSucceeded(MongoDB\Driver\Monitoring\CommandSucceededEvent $event): void {}
26+
27+
public function commandFailed(MongoDB\Driver\Monitoring\CommandFailedEvent $event): void {}
28+
}
29+
30+
$manager = create_test_manager();
31+
32+
// Select a specific server to target the failpoint
33+
$server = $manager->selectServer(new MongoDB\Driver\ReadPreference('primary'));
34+
35+
$bulk = new MongoDB\Driver\BulkWrite;
36+
$bulk->insert(['_id' => 1]);
37+
$bulk->insert(['_id' => 2]);
38+
$manager->executeBulkWrite(NS, $bulk);
39+
40+
// Create cursor with batchSize=1 to establish a server-side cursor
41+
$cursor = $server->executeQuery(NS, new MongoDB\Driver\Query([], ['batchSize' => 1]));
42+
$iterator = new IteratorIterator($cursor);
43+
44+
// Configure failpoint to close the connection on getMore
45+
configureTargetedFailPoint(
46+
$server,
47+
'failCommand',
48+
['times' => 1],
49+
['failCommands' => ['getMore'], 'closeConnection' => true]
50+
);
51+
52+
$subscriber = new KillCursorsSubscriber;
53+
MongoDB\Driver\Monitoring\addSubscriber($subscriber);
54+
55+
// Trigger getMore which will fail with a connection error
56+
throws(function() use ($iterator) {
57+
$iterator->next();
58+
}, MongoDB\Driver\Exception\ConnectionTimeoutException::class);
59+
60+
// Destroy the cursor - libmongoc should send killCursors after reconnecting
61+
unset($iterator, $cursor);
62+
63+
MongoDB\Driver\Monitoring\removeSubscriber($subscriber);
64+
65+
printf("killCursors was sent %d time(s)\n", $subscriber->killCursorsCount);
66+
67+
?>
68+
===DONE===
69+
<?php exit(0); ?>
70+
--EXPECT--
71+
OK: Got MongoDB\Driver\Exception\ConnectionTimeoutException
72+
killCursors was sent
73+
killCursors was sent 1 time(s)
74+
===DONE===

0 commit comments

Comments
 (0)