Skip to content

Commit c73060e

Browse files
authored
Randomize the collection name to avoid collisions when search indexes are created or dropped asynchronously (#2859)
1 parent fa8affc commit c73060e

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

tests/Doctrine/ODM/MongoDB/Tests/Functional/SchemaManagerWaitForSearchIndexesTest.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,27 @@
99
use Doctrine\ODM\MongoDB\Tests\BaseTestCase;
1010
use Documents\CmsArticle;
1111
use MongoDB\Driver\BulkWrite;
12+
use MongoDB\Driver\WriteConcern;
1213
use PHPUnit\Framework\Attributes\Group;
1314
use PHPUnit\Framework\Attributes\TestWith;
1415

16+
use function bin2hex;
1517
use function hrtime;
18+
use function random_bytes;
1619

1720
#[Group('atlas')]
1821
class SchemaManagerWaitForSearchIndexesTest extends BaseTestCase
1922
{
23+
protected function setUp(): void
24+
{
25+
parent::setUp();
26+
27+
// Randomize the collection name to avoid collisions when search indexes
28+
// are created or dropped asynchronously
29+
$this->dm->getClassMetadata(CmsArticle::class)
30+
->setCollection('articles_' . bin2hex(random_bytes(4)));
31+
}
32+
2033
#[TestWith([0])]
2134
#[TestWith([50_000])]
2235
public function testWait(int $nbDocuments): void
@@ -32,7 +45,11 @@ public function testWait(int $nbDocuments): void
3245
$bulk->insert(['topic' => 'topic ' . $i, 'title' => 'title ' . $i, 'text' => 'text ' . $i]);
3346
}
3447

35-
$collection->getManager()->executeBulkWrite($collection->getNamespace(), $bulk);
48+
$collection->getManager()->executeBulkWrite(
49+
$collection->getNamespace(),
50+
$bulk,
51+
['writeConcern' => new WriteConcern(WriteConcern::MAJORITY)],
52+
);
3653
}
3754

3855
// The index must be created after data insertion, so the index status is not immediately "READY"

0 commit comments

Comments
 (0)