Skip to content

Commit 2c87d39

Browse files
committed
Refactor killing of change stream cursor to private method
1 parent 41892d2 commit 2c87d39

File tree

1 file changed

+17
-10
lines changed

1 file changed

+17
-10
lines changed

tests/Operation/WatchFunctionalTest.php

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace MongoDB\Tests\Operation;
44

5+
use MongoDB\ChangeStream;
56
use MongoDB\Client;
67
use MongoDB\Driver\Manager;
78
use MongoDB\Driver\ReadPreference;
@@ -52,8 +53,7 @@ public function testResume()
5253

5354
$this->assertSameDocument($expectedResult, $changeStream->current());
5455

55-
$operation = new DatabaseCommand($this->getDatabaseName(), ["killCursors" => $this->getCollectionName(), "cursors" => [$changeStream->getCursorId()]]);
56-
$operation->execute($this->getPrimaryServer());
56+
$this->killChangeStreamCursor($changeStream);
5757

5858
$this->insertDocument(['_id' => 3, 'x' => 'baz']);
5959

@@ -150,8 +150,7 @@ public function testNoChangeAfterResumeBeforeInsert()
150150

151151
$this->assertSameDocument($expectedResult, $changeStream->current());
152152

153-
$operation = new DatabaseCommand($this->getDatabaseName(), ["killCursors" => $this->getCollectionName(), "cursors" => [$changeStream->getCursorId()]]);
154-
$operation->execute($this->getPrimaryServer());
153+
$this->killChangeStreamCursor($changeStream);
155154

156155
$changeStream->next();
157156
$this->assertNull($changeStream->current());
@@ -176,8 +175,7 @@ public function testResumeAfterKillThenNoOperations()
176175
$operation = new Watch($this->manager, $this->getDatabaseName(), $this->getCollectionName(), []);
177176
$changeStream = $operation->execute($this->getPrimaryServer());
178177

179-
$operation = new DatabaseCommand($this->getDatabaseName(), ["killCursors" => $this->getCollectionName(), "cursors" => [$changeStream->getCursorId()]]);
180-
$operation->execute($this->getPrimaryServer());
178+
$this->killChangeStreamCursor($changeStream);
181179

182180
$changeStream->next();
183181
$this->assertNull($changeStream->current());
@@ -188,8 +186,7 @@ public function testResumeAfterKillThenOperation()
188186
$operation = new Watch($this->manager, $this->getDatabaseName(), $this->getCollectionName(), []);
189187
$changeStream = $operation->execute($this->getPrimaryServer());
190188

191-
$operation = new DatabaseCommand($this->getDatabaseName(), ["killCursors" => $this->getCollectionName(), "cursors" => [$changeStream->getCursorId()]]);
192-
$operation->execute($this->getPrimaryServer());
189+
$this->killChangeStreamCursor($changeStream);
193190

194191
$this->insertDocument(['_id' => 1, 'x' => 'foo']);
195192

@@ -214,8 +211,7 @@ public function testKey()
214211
$changeStream->next();
215212
$this->assertNull($changeStream->key());
216213

217-
$operation = new DatabaseCommand($this->getDatabaseName(), ["killCursors" => $this->getCollectionName(), "cursors" => [$changeStream->getCursorId()]]);
218-
$operation->execute($this->getPrimaryServer());
214+
$this->killChangeStreamCursor($changeStream);
219215

220216
$changeStream->next();
221217
$this->assertNull($changeStream->key());
@@ -318,4 +314,15 @@ private function insertDocument($document)
318314
$writeResult = $insertOne->execute($this->getPrimaryServer());
319315
$this->assertEquals(1, $writeResult->getInsertedCount());
320316
}
317+
318+
private function killChangeStreamCursor(ChangeStream $changeStream)
319+
{
320+
$command = [
321+
'killCursors' => $this->getCollectionName(),
322+
'cursors' => [ $changeStream->getCursorId() ],
323+
];
324+
325+
$operation = new DatabaseCommand($this->getDatabaseName(), $command);
326+
$operation->execute($this->getPrimaryServer());
327+
}
321328
}

0 commit comments

Comments
 (0)