Skip to content

Commit 40e7065

Browse files
committed
Revise spec test for state of change stream cursor
The spec requests that we check that the initial change stream cursor is alive. This can be done by both asserting its cursor ID is non-zero and also calling MongoDB\Driver\Cursor::isDead().
1 parent 306e568 commit 40e7065

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

tests/Operation/WatchFunctionalTest.php

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use MongoDB\Operation\Watch;
1414
use MongoDB\Tests\CommandObserver;
1515
use stdClass;
16+
use ReflectionClass;
1617

1718
class WatchFunctionalTest extends FunctionalTestCase
1819
{
@@ -230,12 +231,31 @@ public function testNonEmptyPipeline()
230231
$this->assertSameDocument($expectedResult, $changeStream->current());
231232
}
232233

233-
public function testCursorWithEmptyBatchNotClosed()
234+
public function testInitialCursorIsNotClosed()
234235
{
235-
$operation = new Watch($this->manager, $this->getDatabaseName(), $this->getCollectionName(), [], ['maxAwaitTimeMS' => 100]);
236+
$operation = new Watch($this->manager, $this->getDatabaseName(), $this->getCollectionName(), []);
236237
$changeStream = $operation->execute($this->getPrimaryServer());
237238

238-
$this->assertNotNull($changeStream);
239+
/* The spec requests that we assert that the cursor returned from the
240+
* aggregate command is not closed on the driver side. We will verify
241+
* this by checking that the cursor ID is non-zero and that libmongoc
242+
* reports the cursor as alive. While the cursor ID is easily accessed
243+
* through ChangeStream, we'll need to use reflection to access the
244+
* internal Cursor and call isDead(). */
245+
$this->assertNotEquals('0', (string) $changeStream->getCursorId());
246+
247+
$rc = new ReflectionClass('MongoDB\ChangeStream');
248+
$rp = $rc->getProperty('csIt');
249+
$rp->setAccessible(true);
250+
251+
$iterator = $rp->getValue($changeStream);
252+
253+
$this->assertInstanceOf('IteratorIterator', $iterator);
254+
255+
$cursor = $iterator->getInnerIterator();
256+
257+
$this->assertInstanceOf('MongoDB\Driver\Cursor', $cursor);
258+
$this->assertFalse($cursor->isDead());
239259
}
240260

241261
/**

0 commit comments

Comments
 (0)