Commit e4a7f0a4 authored by Katherine Walker's avatar Katherine Walker

Improve maxAwaitTimeMS test for ChangeStreamFunctionalTest

parent 783fd434
...@@ -241,17 +241,45 @@ class ChangeStreamFunctionalTest extends FunctionalTestCase ...@@ -241,17 +241,45 @@ class ChangeStreamFunctionalTest extends FunctionalTestCase
public function testMaxAwaitTimeMS() public function testMaxAwaitTimeMS()
{ {
$this->collection = new Collection($this->manager, $this->getDatabaseName(), $this->getCollectionName()); $this->collection = new Collection($this->manager, $this->getDatabaseName(), $this->getCollectionName());
$maxAwaitTimeMS = 10; /* 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
* ensure we see the write. */
$maxAwaitTimeMS = 100;
$changeStreamResult = $this->collection->watch([], ['maxAwaitTimeMS' => $maxAwaitTimeMS]); $changeStreamResult = $this->collection->watch([], ['maxAwaitTimeMS' => $maxAwaitTimeMS]);
/* Make sure we await results for at least maxAwaitTimeMS, since no new /* The initial change stream is empty so we should expect a delay when
* documents should be inserted to wake up the server's command thread. * we call rewind, since it issues a getMore. Expect to wait at least
* Also ensure that we don't wait too long (server default is one * maxAwaitTimeMS, since no new documents should be inserted to wake up
* second). */ * the server's query thread. Also ensure we don't wait too long (server
* default is one second). */
$startTime = microtime(true); $startTime = microtime(true);
$changeStreamResult->rewind(); $changeStreamResult->rewind();
$this->assertGreaterThanOrEqual($maxAwaitTimeMS * 0.001, microtime(true) - $startTime); $duration = microtime(true) - $startTime;
$this->assertLessThan(0.5, microtime(true) - $startTime); $this->assertGreaterThanOrEqual($maxAwaitTimeMS * 0.001, $duration);
} $this->assertLessThan(0.5, $duration);
$this->assertFalse($changeStreamResult->valid());
/* Advancing again on a change stream will issue a getMore, so we should
* expect a delay again. */
$startTime = microtime(true);
$changeStreamResult->next();
$duration = microtime(true) - $startTime;
$this->assertGreaterThanOrEqual($maxAwaitTimeMS * 0.001, $duration);
$this->assertLessThan(0.5, $duration);
$this->assertFalse($changeStreamResult->valid());
/* After inserting a document, the change stream will not issue a
* getMore so we should not expect a delay. */
$result = $this->collection->insertOne(['_id' => 1]);
$this->assertInstanceOf('MongoDB\InsertOneResult', $result);
$this->assertSame(1, $result->getInsertedCount());
$startTime = microtime(true);
$changeStreamResult->next();
$duration = microtime(true) - $startTime;
$this->assertLessThan($maxAwaitTimeMS * 0.001, $duration);
$this->assertTrue($changeStreamResult->valid());
}
} }
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