Skip to content

PHPLIB-457: Add missing change stream prose test 14 #675

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
Sep 4, 2019
Merged
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
62 changes: 62 additions & 0 deletions tests/Operation/WatchFunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use MongoDB\Driver\Exception\LogicException;
use MongoDB\Driver\Exception\ServerException;
use MongoDB\Driver\Manager;
use MongoDB\Driver\Monitoring\CommandSucceededEvent;
use MongoDB\Driver\ReadPreference;
use MongoDB\Driver\WriteConcern;
use MongoDB\Exception\ResumeTokenException;
Expand Down Expand Up @@ -1404,6 +1405,67 @@ public function testGetResumeTokenReturnsOriginalResumeTokenOnEmptyBatch()
$this->assertSame($resumeToken, $changeStream->getResumeToken());
}

/**
* For a ChangeStream under these conditions:
* - The batch is not empty.
* - The batch hasn’t been iterated at all.
* - Only the initial aggregate command has been executed.
* Expected result:
* - getResumeToken must return startAfter from the initial aggregate if the option was specified.
* - getResumeToken must return resumeAfter from the initial aggregate if the option was specified.
* - If neither the startAfter nor resumeAfter options were specified, the getResumeToken result must be empty.
*/
public function testResumeTokenBehaviour()
{
if (version_compare($this->getServerVersion(), '4.1.1', '<')) {
$this->markTestSkipped('Testing resumeAfter and startAfter can only be tested on servers >= 4.1.1');
}

$operation = new Watch($this->manager, $this->getDatabaseName(), $this->getCollectionName(), [], $this->defaultOptions);

$lastOpTime = null;

$changeStream = null;
(new CommandObserver())->observe(function () use ($operation, &$changeStream) {
$changeStream = $operation->execute($this->getPrimaryServer());
}, function ($event) use (&$lastOpTime) {
$this->assertInstanceOf(CommandSucceededEvent::class, $event['succeeded']);
$reply = $event['succeeded']->getReply();

$this->assertObjectHasAttribute('operationTime', $reply);
$lastOpTime = $reply->operationTime;
});

$this->insertDocument(['x' => 1]);

$changeStream->next();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should use advanceCursorUntilValid() from #659, since this test will also run on sharded clusters.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was going to merge this before #659 since I'm still working on figuring out the cause for the double free. I'd like to go ahead with the merge then change this while rebasing #659.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SGTM.

$this->assertTrue($changeStream->valid());
$resumeToken = $changeStream->getResumeToken();

$this->insertDocument(['x' => 2]);

// Test startAfter option
$options = ['startAfter' => $resumeToken] + $this->defaultOptions;
$operation = new Watch($this->manager, $this->getDatabaseName(), $this->getCollectionName(), [], $options);
$changeStream = $operation->execute($this->getPrimaryServer());

$this->assertEquals($resumeToken, $changeStream->getResumeToken());

// Test resumeAfter option
$options = ['resumeAfter' => $resumeToken] + $this->defaultOptions;
$operation = new Watch($this->manager, $this->getDatabaseName(), $this->getCollectionName(), [], $options);
$changeStream = $operation->execute($this->getPrimaryServer());

$this->assertEquals($resumeToken, $changeStream->getResumeToken());

// Test without option
$options = ['startAtOperationTime' => $lastOpTime] + $this->defaultOptions;
$operation = new Watch($this->manager, $this->getDatabaseName(), $this->getCollectionName(), [], $options);
$changeStream = $operation->execute($this->getPrimaryServer());

$this->assertNull($changeStream->getResumeToken());
}

/**
* Prose test: "$changeStream stage for ChangeStream started with startAfter
* against a server >=4.1.1 that has not received any results yet MUST
Expand Down