Skip to content

Commit 1909275

Browse files
committed
Prose test 15
1 parent 664b955 commit 1909275

File tree

1 file changed

+87
-0
lines changed

1 file changed

+87
-0
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
3+
namespace MongoDB\Tests\SpecTests\Crud;
4+
5+
use MongoDB\ClientBulkWrite;
6+
use MongoDB\Driver\Monitoring\CommandFailedEvent;
7+
use MongoDB\Driver\Monitoring\CommandStartedEvent;
8+
use MongoDB\Driver\Monitoring\CommandSubscriber;
9+
use MongoDB\Driver\Monitoring\CommandSucceededEvent;
10+
use MongoDB\Driver\WriteConcern;
11+
use MongoDB\Tests\SpecTests\FunctionalTestCase;
12+
13+
use function str_repeat;
14+
15+
/**
16+
* Prose test 15: MongoClient.bulkWrite with unacknowledged write concern uses w:0 for all batches
17+
*
18+
* @see https://github.com/mongodb/specifications/tree/master/source/crud/tests#15-mongoclientbulkwrite-with-unacknowledged-write-concern-uses-w0-for-all-batches
19+
*/
20+
class Prose15_BulkWriteUnacknowledgedWriteConcernTest extends FunctionalTestCase
21+
{
22+
public function testUnacknowledgedWriteConcern(): void
23+
{
24+
if ($this->isServerless()) {
25+
$this->markTestSkipped('bulkWrite command is not supported');
26+
}
27+
28+
$this->skipIfServerVersion('<', '8.0', 'bulkWrite command is not supported');
29+
30+
$client = self::createTestClient();
31+
32+
$hello = $this->getPrimaryServer()->getInfo();
33+
self::assertIsInt($maxBsonObjectSize = $hello['maxBsonObjectSize'] ?? null);
34+
self::assertIsInt($maxMessageSizeBytes = $hello['maxMessageSizeBytes'] ?? null);
35+
36+
// Explicitly create the collection to work around SERVER-95537
37+
$this->createCollection($this->getDatabaseName(), $this->getCollectionName());
38+
39+
$collection = $client->selectCollection($this->getDatabaseName(), $this->getCollectionName());
40+
$bulkWrite = ClientBulkWrite::createWithCollection($collection, ['ordered' => false]);
41+
42+
$numModels = (int) ($maxMessageSizeBytes / $maxBsonObjectSize) + 1;
43+
44+
for ($i = 0; $i < $numModels; ++$i) {
45+
$bulkWrite->insertOne(['a' => str_repeat('b', $maxBsonObjectSize - 500)]);
46+
}
47+
48+
$subscriber = new class implements CommandSubscriber {
49+
public array $commandStartedEvents = [];
50+
51+
public function commandStarted(CommandStartedEvent $event): void
52+
{
53+
if ($event->getCommandName() === 'bulkWrite') {
54+
$this->commandStartedEvents[] = $event;
55+
}
56+
}
57+
58+
public function commandSucceeded(CommandSucceededEvent $event): void
59+
{
60+
}
61+
62+
public function commandFailed(CommandFailedEvent $event): void
63+
{
64+
}
65+
};
66+
67+
$client->addSubscriber($subscriber);
68+
69+
$result = $client->bulkWrite($bulkWrite, ['writeConcern' => new WriteConcern(0)]);
70+
71+
self::assertFalse($result->isAcknowledged());
72+
self::assertCount(2, $subscriber->commandStartedEvents);
73+
[$firstEvent, $secondEvent] = $subscriber->commandStartedEvents;
74+
75+
$firstCommand = $firstEvent->getCommand();
76+
self::assertIsArray($firstCommand->ops ?? null);
77+
self::assertCount($numModels - 1, $firstCommand->ops);
78+
self::assertSame(0, $firstCommand->writeConcern->w ?? null);
79+
80+
$secondCommand = $secondEvent->getCommand();
81+
self::assertIsArray($secondCommand->ops ?? null);
82+
self::assertCount(1, $secondCommand->ops);
83+
self::assertSame(0, $secondCommand->writeConcern->w ?? null);
84+
85+
self::assertSame($numModels, $collection->countDocuments());
86+
}
87+
}

0 commit comments

Comments
 (0)