Skip to content

Commit 746c0f0

Browse files
committed
Use w:majority for operations in change stream tests
This will avoid spurious test failures due to timing and ensure that each acknowledged write is returned in the next change stream iteration.
1 parent 80cee6f commit 746c0f0

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

tests/Operation/WatchFunctionalTest.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use MongoDB\Driver\Manager;
88
use MongoDB\Driver\ReadPreference;
99
use MongoDB\Driver\Server;
10+
use MongoDB\Driver\WriteConcern;
1011
use MongoDB\Driver\Exception\ConnectionTimeoutException;
1112
use MongoDB\Driver\Exception\LogicException;
1213
use MongoDB\Exception\ResumeTokenException;
@@ -1026,7 +1027,12 @@ public function testSessionFreed()
10261027

10271028
private function insertDocument($document)
10281029
{
1029-
$insertOne = new InsertOne($this->getDatabaseName(), $this->getCollectionName(), $document);
1030+
$insertOne = new InsertOne(
1031+
$this->getDatabaseName(),
1032+
$this->getCollectionName(),
1033+
$document,
1034+
['writeConcern' => new WriteConcern(WriteConcern::MAJORITY)]
1035+
);
10301036
$writeResult = $insertOne->execute($this->getPrimaryServer());
10311037
$this->assertEquals(1, $writeResult->getInsertedCount());
10321038
}

tests/SpecTests/Operation.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use MongoDB\Database;
77
use MongoDB\Driver\Cursor;
88
use MongoDB\Driver\Session;
9+
use MongoDB\Driver\WriteConcern;
910
use MongoDB\Driver\Exception\BulkWriteException;
1011
use MongoDB\Driver\Exception\Exception;
1112
use MongoDB\Operation\FindOneAndReplace;
@@ -53,6 +54,11 @@ public static function fromChangeStreams(stdClass $operation)
5354
{
5455
$o = new self($operation);
5556

57+
/* Note: change streams only return majority-committed writes, so ensure
58+
* each operation applies that write concern. This will avoid spurious
59+
* test failures. */
60+
$writeConcern = new WriteConcern(WriteConcern::MAJORITY);
61+
5662
// Expect all operations to succeed
5763
$o->errorExpectation = ErrorExpectation::noError();
5864

@@ -66,13 +72,16 @@ public static function fromChangeStreams(stdClass $operation)
6672
$o->arguments = ['command' => [
6773
'renameCollection' => $operation->database . '.' . $operation->collection,
6874
'to' => $operation->database . '.' . $operation->arguments->to,
75+
// Note: Database::command() does not inherit WC, so be explicit
76+
'writeConcern' => $writeConcern,
6977
]];
7078

7179
return $o;
7280
}
7381

7482
$o->databaseName = $operation->database;
7583
$o->collectionName = $operation->collection;
84+
$o->collectionOptions = ['writeConcern' => $writeConcern];
7685
$o->object = self::OBJECT_SELECT_COLLECTION;
7786

7887
return $o;

0 commit comments

Comments
 (0)