Skip to content

Commit fcbf896

Browse files
committed
PHPLIB-445: Leave key as-is if ChangeStream::next() fails to extract resume token
1 parent 770e2af commit fcbf896

File tree

2 files changed

+4
-6
lines changed

2 files changed

+4
-6
lines changed

src/ChangeStream.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,11 @@ public function next()
104104
try {
105105
$this->csIt->next();
106106
if ($this->valid()) {
107+
$this->resumeToken = $this->extractResumeToken($this->csIt->current());
107108
if ($this->hasAdvanced) {
108109
$this->key++;
109110
}
110111
$this->hasAdvanced = true;
111-
$this->resumeToken = $this->extractResumeToken($this->csIt->current());
112112
}
113113
/* If the cursorId is 0, the server has invalidated the cursor so we
114114
* will never perform another getMore. This means that we cannot

tests/Operation/WatchFunctionalTest.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -642,7 +642,7 @@ public function testNextAdvancesKey()
642642
$this->assertSame(1, $changeStream->key());
643643
}
644644

645-
public function testResumeTokenNotFoundAdvancesKey()
645+
public function testResumeTokenNotFoundDoesNotAdvanceKey()
646646
{
647647
if (version_compare($this->getServerVersion(), '4.1.8', '>=')) {
648648
$this->markTestSkipped('Server rejects change streams that modify resume token (SERVER-37786)');
@@ -653,8 +653,6 @@ public function testResumeTokenNotFoundAdvancesKey()
653653
$operation = new Watch($this->manager, $this->getDatabaseName(), $this->getCollectionName(), $pipeline, $this->defaultOptions);
654654
$changeStream = $operation->execute($this->getPrimaryServer());
655655

656-
/* Note: we intentionally do not start iteration with rewind() to ensure
657-
* that we test extraction functionality within next(). */
658656
$this->insertDocument(['x' => 1]);
659657
$this->insertDocument(['x' => 2]);
660658
$this->insertDocument(['x' => 3]);
@@ -671,14 +669,14 @@ public function testResumeTokenNotFoundAdvancesKey()
671669
$this->fail('ResumeTokenException was not thrown');
672670
} catch (ResumeTokenException $e) {}
673671

674-
$this->assertSame(1, $changeStream->key());
672+
$this->assertSame(0, $changeStream->key());
675673

676674
try {
677675
$changeStream->next();
678676
$this->fail('ResumeTokenException was not thrown');
679677
} catch (ResumeTokenException $e) {}
680678

681-
$this->assertSame(2, $changeStream->key());
679+
$this->assertSame(0, $changeStream->key());
682680
}
683681

684682
public function testSessionPersistsAfterResume()

0 commit comments

Comments
 (0)