Skip to content

PHPLIB-445: Leave key as-is if ChangeStream::next() fails to extract resume token #622

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/ChangeStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,14 +212,15 @@ private function onIteration($isNext)
return;
}

$this->resumeToken = $this->extractResumeToken($this->csIt->current());

/* Increment the key if the iteration event was a call to next() and we
* have already advanced past the first result. */
if ($isNext && $this->hasAdvanced) {
$this->key++;
}

$this->hasAdvanced = true;
$this->resumeToken = $this->extractResumeToken($this->csIt->current());
}

/**
Expand Down
8 changes: 3 additions & 5 deletions tests/Operation/WatchFunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -706,7 +706,7 @@ public function testNextAdvancesKey()
$this->assertSame(1, $changeStream->key());
}

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

/* Note: we intentionally do not start iteration with rewind() to ensure
* that we test extraction functionality within next(). */
$this->insertDocument(['x' => 1]);
$this->insertDocument(['x' => 2]);
$this->insertDocument(['x' => 3]);
Expand All @@ -735,14 +733,14 @@ public function testResumeTokenNotFoundAdvancesKey()
$this->fail('ResumeTokenException was not thrown');
} catch (ResumeTokenException $e) {}

$this->assertSame(1, $changeStream->key());
$this->assertSame(0, $changeStream->key());

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

$this->assertSame(2, $changeStream->key());
$this->assertSame(0, $changeStream->key());
}

public function testSessionPersistsAfterResume()
Expand Down