Skip to content

Commit 8151219

Browse files
committed
Use approximate pivot for maxAwaitTimeMS assertions
Since maxAwaitTimeMS is not a hard limit (the server may reply sooner), this will help avoid fragile test failures.
1 parent a46774d commit 8151219

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

tests/Operation/FindFunctionalTest.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,12 @@ public function testMaxAwaitTimeMS()
154154
$this->markTestSkipped('maxAwaitTimeMS option is not supported');
155155
}
156156

157-
$maxAwaitTimeMS = 10;
157+
$maxAwaitTimeMS = 100;
158+
159+
/* Calculate an approximate pivot to use for time assertions. We will
160+
* assert that the duration of blocking responses is greater than this
161+
* value, and vice versa. */
162+
$pivot = ($maxAwaitTimeMS * 0.001) * 0.9;
158163

159164
// Create a capped collection.
160165
$databaseName = $this->getDatabaseName();
@@ -183,7 +188,7 @@ public function testMaxAwaitTimeMS()
183188
$startTime = microtime(true);
184189
$it->rewind();
185190
$duration = microtime(true) - $startTime;
186-
$this->assertLessThan($maxAwaitTimeMS * 0.001, $duration);
191+
$this->assertLessThan($pivot, $duration);
187192

188193
$this->assertTrue($it->valid());
189194
$this->assertSameDocument(['_id' => 1], $it->current());
@@ -193,7 +198,7 @@ public function testMaxAwaitTimeMS()
193198
$startTime = microtime(true);
194199
$it->next();
195200
$duration = microtime(true) - $startTime;
196-
$this->assertLessThan($maxAwaitTimeMS * 0.001, $duration);
201+
$this->assertLessThan($pivot, $duration);
197202

198203
$this->assertTrue($it->valid());
199204
$this->assertSameDocument(['_id' => 2], $it->current());
@@ -206,7 +211,7 @@ public function testMaxAwaitTimeMS()
206211
$startTime = microtime(true);
207212
$it->next();
208213
$duration = microtime(true) - $startTime;
209-
$this->assertGreaterThanOrEqual($maxAwaitTimeMS * 0.001, $duration);
214+
$this->assertGreaterThan($pivot, $duration);
210215
$this->assertLessThan(0.5, $duration);
211216

212217
$this->assertFalse($it->valid());

tests/Operation/WatchFunctionalTest.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,11 @@ public function testMaxAwaitTimeMS()
380380
* ensure we see the write. */
381381
$maxAwaitTimeMS = 100;
382382

383+
/* Calculate an approximate pivot to use for time assertions. We will
384+
* assert that the duration of blocking responses is greater than this
385+
* value, and vice versa. */
386+
$pivot = ($maxAwaitTimeMS * 0.001) * 0.9;
387+
383388
$operation = new Watch($this->manager, $this->getDatabaseName(), $this->getCollectionName(), [], ['maxAwaitTimeMS' => $maxAwaitTimeMS]);
384389
$changeStream = $operation->execute($this->getPrimaryServer());
385390

@@ -391,7 +396,7 @@ public function testMaxAwaitTimeMS()
391396
$startTime = microtime(true);
392397
$changeStream->rewind();
393398
$duration = microtime(true) - $startTime;
394-
$this->assertGreaterThanOrEqual($maxAwaitTimeMS * 0.001, $duration);
399+
$this->assertGreaterThan($pivot, $duration);
395400
$this->assertLessThan(0.5, $duration);
396401

397402
$this->assertFalse($changeStream->valid());
@@ -401,7 +406,7 @@ public function testMaxAwaitTimeMS()
401406
$startTime = microtime(true);
402407
$changeStream->next();
403408
$duration = microtime(true) - $startTime;
404-
$this->assertGreaterThanOrEqual($maxAwaitTimeMS * 0.001, $duration);
409+
$this->assertGreaterThan($pivot, $duration);
405410
$this->assertLessThan(0.5, $duration);
406411

407412
$this->assertFalse($changeStream->valid());
@@ -413,7 +418,7 @@ public function testMaxAwaitTimeMS()
413418
$startTime = microtime(true);
414419
$changeStream->next();
415420
$duration = microtime(true) - $startTime;
416-
$this->assertLessThan($maxAwaitTimeMS * 0.001, $duration);
421+
$this->assertLessThan($pivot, $duration);
417422
$this->assertTrue($changeStream->valid());
418423
}
419424

0 commit comments

Comments
 (0)