Commit d42c3b4a authored by Jeremy Mikola's avatar Jeremy Mikola

Merge pull request #511

parents 899b43ce 454313a3
...@@ -17,6 +17,8 @@ use ReflectionClass; ...@@ -17,6 +17,8 @@ use ReflectionClass;
class WatchFunctionalTest extends FunctionalTestCase class WatchFunctionalTest extends FunctionalTestCase
{ {
private $defaultOptions = ['maxAwaitTimeMS' => 500];
public function setUp() public function setUp()
{ {
parent::setUp(); parent::setUp();
...@@ -34,7 +36,7 @@ class WatchFunctionalTest extends FunctionalTestCase ...@@ -34,7 +36,7 @@ class WatchFunctionalTest extends FunctionalTestCase
{ {
$this->insertDocument(['_id' => 1, 'x' => 'foo']); $this->insertDocument(['_id' => 1, 'x' => 'foo']);
$operation = new Watch($this->manager, $this->getDatabaseName(), $this->getCollectionName(), [], ['maxAwaitTimeMS' => 100]); $operation = new Watch($this->manager, $this->getDatabaseName(), $this->getCollectionName(), [], $this->defaultOptions);
$changeStream = $operation->execute($this->getPrimaryServer()); $changeStream = $operation->execute($this->getPrimaryServer());
$changeStream->rewind(); $changeStream->rewind();
...@@ -81,7 +83,7 @@ class WatchFunctionalTest extends FunctionalTestCase ...@@ -81,7 +83,7 @@ class WatchFunctionalTest extends FunctionalTestCase
$manager = new Manager($this->getUri(), ['socketTimeoutMS' => 50]); $manager = new Manager($this->getUri(), ['socketTimeoutMS' => 50]);
$primaryServer = $manager->selectServer(new ReadPreference(ReadPreference::RP_PRIMARY)); $primaryServer = $manager->selectServer(new ReadPreference(ReadPreference::RP_PRIMARY));
$operation = new Watch($manager, $this->getDatabaseName(), $this->getCollectionName(), [], ['maxAwaitTimeMS' => 100]); $operation = new Watch($manager, $this->getDatabaseName(), $this->getCollectionName(), [], $this->defaultOptions);
$changeStream = $operation->execute($primaryServer); $changeStream = $operation->execute($primaryServer);
/* Note: we intentionally do not start iteration with rewind() to ensure /* Note: we intentionally do not start iteration with rewind() to ensure
...@@ -134,7 +136,7 @@ class WatchFunctionalTest extends FunctionalTestCase ...@@ -134,7 +136,7 @@ class WatchFunctionalTest extends FunctionalTestCase
$manager = new Manager($this->getUri(), ['socketTimeoutMS' => 50]); $manager = new Manager($this->getUri(), ['socketTimeoutMS' => 50]);
$primaryServer = $manager->selectServer(new ReadPreference(ReadPreference::RP_PRIMARY)); $primaryServer = $manager->selectServer(new ReadPreference(ReadPreference::RP_PRIMARY));
$operation = new Watch($manager, $this->getDatabaseName(), $this->getCollectionName(), [], ['maxAwaitTimeMS' => 100]); $operation = new Watch($manager, $this->getDatabaseName(), $this->getCollectionName(), [], $this->defaultOptions);
$changeStream = $operation->execute($primaryServer); $changeStream = $operation->execute($primaryServer);
$commands = []; $commands = [];
...@@ -180,7 +182,7 @@ class WatchFunctionalTest extends FunctionalTestCase ...@@ -180,7 +182,7 @@ class WatchFunctionalTest extends FunctionalTestCase
{ {
$this->insertDocument(['_id' => 1, 'x' => 'foo']); $this->insertDocument(['_id' => 1, 'x' => 'foo']);
$operation = new Watch($this->manager, $this->getDatabaseName(), $this->getCollectionName(), [], ['maxAwaitTimeMS' => 100]); $operation = new Watch($this->manager, $this->getDatabaseName(), $this->getCollectionName(), [], $this->defaultOptions);
$changeStream = $operation->execute($this->getPrimaryServer()); $changeStream = $operation->execute($this->getPrimaryServer());
$changeStream->rewind(); $changeStream->rewind();
...@@ -225,7 +227,7 @@ class WatchFunctionalTest extends FunctionalTestCase ...@@ -225,7 +227,7 @@ class WatchFunctionalTest extends FunctionalTestCase
public function testKey() public function testKey()
{ {
$operation = new Watch($this->manager, $this->getDatabaseName(), $this->getCollectionName(), [], ['maxAwaitTimeMS' => 100]); $operation = new Watch($this->manager, $this->getDatabaseName(), $this->getCollectionName(), [], $this->defaultOptions);
$changeStream = $operation->execute($this->getPrimaryServer()); $changeStream = $operation->execute($this->getPrimaryServer());
$this->assertFalse($changeStream->valid()); $this->assertFalse($changeStream->valid());
...@@ -262,7 +264,7 @@ class WatchFunctionalTest extends FunctionalTestCase ...@@ -262,7 +264,7 @@ class WatchFunctionalTest extends FunctionalTestCase
{ {
$pipeline = [['$project' => ['foo' => [0]]]]; $pipeline = [['$project' => ['foo' => [0]]]];
$operation = new Watch($this->manager, $this->getDatabaseName(), $this->getCollectionName(), $pipeline, ['maxAwaitTimeMS' => 100]); $operation = new Watch($this->manager, $this->getDatabaseName(), $this->getCollectionName(), $pipeline, $this->defaultOptions);
$changeStream = $operation->execute($this->getPrimaryServer()); $changeStream = $operation->execute($this->getPrimaryServer());
$this->insertDocument(['_id' => 1]); $this->insertDocument(['_id' => 1]);
...@@ -313,7 +315,7 @@ class WatchFunctionalTest extends FunctionalTestCase ...@@ -313,7 +315,7 @@ class WatchFunctionalTest extends FunctionalTestCase
{ {
$pipeline = [['$project' => ['_id' => 0 ]]]; $pipeline = [['$project' => ['_id' => 0 ]]];
$operation = new Watch($this->manager, $this->getDatabaseName(), $this->getCollectionName(), $pipeline, ['maxAwaitTimeMS' => 100]); $operation = new Watch($this->manager, $this->getDatabaseName(), $this->getCollectionName(), $pipeline, $this->defaultOptions);
$changeStream = $operation->execute($this->getPrimaryServer()); $changeStream = $operation->execute($this->getPrimaryServer());
/* Note: we intentionally do not start iteration with rewind() to ensure /* Note: we intentionally do not start iteration with rewind() to ensure
...@@ -331,7 +333,7 @@ class WatchFunctionalTest extends FunctionalTestCase ...@@ -331,7 +333,7 @@ class WatchFunctionalTest extends FunctionalTestCase
{ {
$pipeline = [['$project' => ['_id' => 0 ]]]; $pipeline = [['$project' => ['_id' => 0 ]]];
$operation = new Watch($this->manager, $this->getDatabaseName(), $this->getCollectionName(), $pipeline, ['maxAwaitTimeMS' => 100]); $operation = new Watch($this->manager, $this->getDatabaseName(), $this->getCollectionName(), $pipeline, $this->defaultOptions);
$changeStream = $operation->execute($this->getPrimaryServer()); $changeStream = $operation->execute($this->getPrimaryServer());
$this->insertDocument(['x' => 1]); $this->insertDocument(['x' => 1]);
...@@ -347,7 +349,7 @@ class WatchFunctionalTest extends FunctionalTestCase ...@@ -347,7 +349,7 @@ class WatchFunctionalTest extends FunctionalTestCase
{ {
$pipeline = [['$project' => ['_id' => ['$literal' => 'foo']]]]; $pipeline = [['$project' => ['_id' => ['$literal' => 'foo']]]];
$operation = new Watch($this->manager, $this->getDatabaseName(), $this->getCollectionName(), $pipeline, ['maxAwaitTimeMS' => 100]); $operation = new Watch($this->manager, $this->getDatabaseName(), $this->getCollectionName(), $pipeline, $this->defaultOptions);
$changeStream = $operation->execute($this->getPrimaryServer()); $changeStream = $operation->execute($this->getPrimaryServer());
/* Note: we intentionally do not start iteration with rewind() to ensure /* Note: we intentionally do not start iteration with rewind() to ensure
...@@ -365,7 +367,7 @@ class WatchFunctionalTest extends FunctionalTestCase ...@@ -365,7 +367,7 @@ class WatchFunctionalTest extends FunctionalTestCase
{ {
$pipeline = [['$project' => ['_id' => ['$literal' => 'foo']]]]; $pipeline = [['$project' => ['_id' => ['$literal' => 'foo']]]];
$operation = new Watch($this->manager, $this->getDatabaseName(), $this->getCollectionName(), $pipeline, ['maxAwaitTimeMS' => 100]); $operation = new Watch($this->manager, $this->getDatabaseName(), $this->getCollectionName(), $pipeline, $this->defaultOptions);
$changeStream = $operation->execute($this->getPrimaryServer()); $changeStream = $operation->execute($this->getPrimaryServer());
$this->insertDocument(['x' => 1]); $this->insertDocument(['x' => 1]);
...@@ -378,13 +380,18 @@ class WatchFunctionalTest extends FunctionalTestCase ...@@ -378,13 +380,18 @@ class WatchFunctionalTest extends FunctionalTestCase
/* On average, an acknowledged write takes about 20 ms to appear in a /* On average, an acknowledged write takes about 20 ms to appear in a
* change stream on the server so we'll use a higher maxAwaitTimeMS to * change stream on the server so we'll use a higher maxAwaitTimeMS to
* ensure we see the write. */ * ensure we see the write. */
$maxAwaitTimeMS = 100; $maxAwaitTimeMS = 500;
/* Calculate an approximate pivot to use for time assertions. We will /* Calculate an approximate pivot to use for time assertions. We will
* assert that the duration of blocking responses is greater than this * assert that the duration of blocking responses is greater than this
* value, and vice versa. */ * value, and vice versa. */
$pivot = ($maxAwaitTimeMS * 0.001) * 0.9; $pivot = ($maxAwaitTimeMS * 0.001) * 0.9;
/* Calculate an approximate upper bound to use for time assertions. We
* will assert that the duration of blocking responses is less than this
* value. */
$upperBound = ($maxAwaitTimeMS * 0.001) * 1.5;
$operation = new Watch($this->manager, $this->getDatabaseName(), $this->getCollectionName(), [], ['maxAwaitTimeMS' => $maxAwaitTimeMS]); $operation = new Watch($this->manager, $this->getDatabaseName(), $this->getCollectionName(), [], ['maxAwaitTimeMS' => $maxAwaitTimeMS]);
$changeStream = $operation->execute($this->getPrimaryServer()); $changeStream = $operation->execute($this->getPrimaryServer());
...@@ -397,7 +404,7 @@ class WatchFunctionalTest extends FunctionalTestCase ...@@ -397,7 +404,7 @@ class WatchFunctionalTest extends FunctionalTestCase
$changeStream->rewind(); $changeStream->rewind();
$duration = microtime(true) - $startTime; $duration = microtime(true) - $startTime;
$this->assertGreaterThan($pivot, $duration); $this->assertGreaterThan($pivot, $duration);
$this->assertLessThan(0.5, $duration); $this->assertLessThan($upperBound, $duration);
$this->assertFalse($changeStream->valid()); $this->assertFalse($changeStream->valid());
...@@ -407,7 +414,7 @@ class WatchFunctionalTest extends FunctionalTestCase ...@@ -407,7 +414,7 @@ class WatchFunctionalTest extends FunctionalTestCase
$changeStream->next(); $changeStream->next();
$duration = microtime(true) - $startTime; $duration = microtime(true) - $startTime;
$this->assertGreaterThan($pivot, $duration); $this->assertGreaterThan($pivot, $duration);
$this->assertLessThan(0.5, $duration); $this->assertLessThan($upperBound, $duration);
$this->assertFalse($changeStream->valid()); $this->assertFalse($changeStream->valid());
...@@ -424,7 +431,7 @@ class WatchFunctionalTest extends FunctionalTestCase ...@@ -424,7 +431,7 @@ class WatchFunctionalTest extends FunctionalTestCase
public function testRewindResumesAfterCursorNotFound() public function testRewindResumesAfterCursorNotFound()
{ {
$operation = new Watch($this->manager, $this->getDatabaseName(), $this->getCollectionName(), [], ['maxAwaitTimeMS' => 100]); $operation = new Watch($this->manager, $this->getDatabaseName(), $this->getCollectionName(), [], $this->defaultOptions);
$changeStream = $operation->execute($this->getPrimaryServer()); $changeStream = $operation->execute($this->getPrimaryServer());
$this->killChangeStreamCursor($changeStream); $this->killChangeStreamCursor($changeStream);
...@@ -436,7 +443,7 @@ class WatchFunctionalTest extends FunctionalTestCase ...@@ -436,7 +443,7 @@ class WatchFunctionalTest extends FunctionalTestCase
public function testRewindExtractsResumeTokenAndNextResumes() public function testRewindExtractsResumeTokenAndNextResumes()
{ {
$operation = new Watch($this->manager, $this->getDatabaseName(), $this->getCollectionName(), [], ['maxAwaitTimeMS' => 100]); $operation = new Watch($this->manager, $this->getDatabaseName(), $this->getCollectionName(), [], $this->defaultOptions);
$changeStream = $operation->execute($this->getPrimaryServer()); $changeStream = $operation->execute($this->getPrimaryServer());
$this->insertDocument(['_id' => 1, 'x' => 'foo']); $this->insertDocument(['_id' => 1, 'x' => 'foo']);
...@@ -473,7 +480,7 @@ class WatchFunctionalTest extends FunctionalTestCase ...@@ -473,7 +480,7 @@ class WatchFunctionalTest extends FunctionalTestCase
*/ */
public function testTypeMapOption(array $typeMap, $expectedChangeDocument) public function testTypeMapOption(array $typeMap, $expectedChangeDocument)
{ {
$operation = new Watch($this->manager, $this->getDatabaseName(), $this->getCollectionName(), [], ['maxAwaitTimeMS' => 100, 'typeMap' => $typeMap]); $operation = new Watch($this->manager, $this->getDatabaseName(), $this->getCollectionName(), [], ['typeMap' => $typeMap] + $this->defaultOptions);
$changeStream = $operation->execute($this->getPrimaryServer()); $changeStream = $operation->execute($this->getPrimaryServer());
$changeStream->rewind(); $changeStream->rewind();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment