Skip to content

Commit 783fd43

Browse files
jmikolakvwalker
authored andcommitted
Improve functional test for Find with maxAwaitTimeMS
This inserts an additional document so that we can assert that the first rewind and next calls do not block, while the last next does issue a getMore and block.
1 parent 7c1dd58 commit 783fd43

File tree

1 file changed

+29
-6
lines changed

1 file changed

+29
-6
lines changed

tests/Operation/FindFunctionalTest.php

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -148,22 +148,45 @@ public function testMaxAwaitTimeMS()
148148
// Insert documents into the capped collection.
149149
$bulkWrite = new BulkWrite(['ordered' => true]);
150150
$bulkWrite->insert(['_id' => 1]);
151+
$bulkWrite->insert(['_id' => 2]);
151152
$result = $this->manager->executeBulkWrite($this->getNamespace(), $bulkWrite);
152153

153154
$operation = new Find($databaseName, $cappedCollectionName, [], ['cursorType' => Find::TAILABLE_AWAIT, 'maxAwaitTimeMS' => $maxAwaitTimeMS]);
154155
$cursor = $operation->execute($this->getPrimaryServer());
155156
$it = new \IteratorIterator($cursor);
156157

157-
/* Make sure we await results for at least maxAwaitTimeMS, since no new
158-
* documents should be inserted to wake up the server's query thread.
159-
* Also ensure that we don't wait too long (server default is one
160-
* second). */
158+
/* The initial query includes the one and only document in its result
159+
* batch, so we should not expect a delay. */
160+
$startTime = microtime(true);
161161
$it->rewind();
162+
$duration = microtime(true) - $startTime;
163+
$this->assertLessThan($maxAwaitTimeMS * 0.001, $duration);
164+
165+
$this->assertTrue($it->valid());
166+
$this->assertSameDocument(['_id' => 1], $it->current());
167+
168+
/* Advancing again takes us to the last document of the result batch,
169+
* but still should not issue a getMore */
170+
$startTime = microtime(true);
162171
$it->next();
172+
$duration = microtime(true) - $startTime;
173+
$this->assertLessThan($maxAwaitTimeMS * 0.001, $duration);
174+
175+
$this->assertTrue($it->valid());
176+
$this->assertSameDocument(['_id' => 2], $it->current());
177+
178+
/* Now that we've reached the end of the initial result batch, advancing
179+
* again will issue a getMore. Expect to wait at least maxAwaitTimeMS,
180+
* since no new documents should be inserted to wake up the server's
181+
* query thread. Also ensure we don't wait too long (server default is
182+
* one second). */
163183
$startTime = microtime(true);
164184
$it->next();
165-
$this->assertGreaterThanOrEqual($maxAwaitTimeMS * 0.001, microtime(true) - $startTime);
166-
$this->assertLessThan(0.5, microtime(true) - $startTime);
185+
$duration = microtime(true) - $startTime;
186+
$this->assertGreaterThanOrEqual($maxAwaitTimeMS * 0.001, $duration);
187+
$this->assertLessThan(0.5, $duration);
188+
189+
$this->assertFalse($it->valid());
167190
}
168191

169192
/**

0 commit comments

Comments
 (0)