Skip to content

Commit b4d04ca

Browse files
committed
CRUD prose tests 3 and 4
1 parent 4d858d9 commit b4d04ca

File tree

2 files changed

+148
-0
lines changed

2 files changed

+148
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
3+
namespace MongoDB\Tests\SpecTests\Crud;
4+
5+
use MongoDB\BulkWriteCommandBuilder;
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\Tests\SpecTests\FunctionalTestCase;
11+
12+
/**
13+
* Prose test 3: MongoClient.bulkWrite batch splits a writeModels input with greater than maxWriteBatchSize operations
14+
*
15+
* @see https://github.com/mongodb/specifications/tree/master/source/crud/tests#3-mongoclientbulkwrite-batch-splits-a-writemodels-input-with-greater-than-maxwritebatchsize-operations
16+
*/
17+
class Prose3_BulkWriteSplitsOnMaxWriteBatchSizeTest extends FunctionalTestCase
18+
{
19+
public function testSplitOnMaxWriteBatchSize(): void
20+
{
21+
if ($this->isServerless()) {
22+
$this->markTestSkipped('bulkWrite command is not supported');
23+
}
24+
25+
$this->skipIfServerVersion('<', '8.0', 'bulkWrite command is not supported');
26+
27+
$client = self::createTestClient();
28+
29+
$maxWriteBatchSize = $this->getPrimaryServer()->getInfo()['maxWriteBatchSize'] ?? null;
30+
self::assertIsInt($maxWriteBatchSize);
31+
32+
$collection = $client->selectCollection($this->getDatabaseName(), $this->getCollectionName());
33+
$bulkWrite = BulkWriteCommandBuilder::createWithCollection($collection);
34+
35+
for ($i = 0; $i < $maxWriteBatchSize + 1; ++$i) {
36+
$bulkWrite->insertOne(['a' => 'b']);
37+
}
38+
39+
$subscriber = new class implements CommandSubscriber {
40+
public array $commandStartedEvents = [];
41+
42+
public function commandStarted(CommandStartedEvent $event): void
43+
{
44+
if ($event->getCommandName() === 'bulkWrite') {
45+
$this->commandStartedEvents[] = $event;
46+
}
47+
}
48+
49+
public function commandSucceeded(CommandSucceededEvent $event): void
50+
{
51+
}
52+
53+
public function commandFailed(CommandFailedEvent $event): void
54+
{
55+
}
56+
};
57+
58+
$client->addSubscriber($subscriber);
59+
60+
$result = $client->bulkWrite($bulkWrite);
61+
62+
self::assertSame($maxWriteBatchSize + 1, $result->getInsertedCount());
63+
self::assertCount(2, $subscriber->commandStartedEvents);
64+
[$firstEvent, $secondEvent] = $subscriber->commandStartedEvents;
65+
self::assertIsArray($firstCommandOps = $firstEvent->getCommand()->ops ?? null);
66+
self::assertCount($maxWriteBatchSize, $firstCommandOps);
67+
self::assertIsArray($secondCommandOps = $secondEvent->getCommand()->ops ?? null);
68+
self::assertCount(1, $secondCommandOps);
69+
self::assertEquals($firstEvent->getOperationId(), $secondEvent->getOperationId());
70+
}
71+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
3+
namespace MongoDB\Tests\SpecTests\Crud;
4+
5+
use MongoDB\BulkWriteCommandBuilder;
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\Tests\SpecTests\FunctionalTestCase;
11+
12+
use function str_repeat;
13+
14+
/**
15+
* Prose test 4: MongoClient.bulkWrite batch splits when an ops payload exceeds maxMessageSizeBytes
16+
*
17+
* @see https://github.com/mongodb/specifications/tree/master/source/crud/tests#4-mongoclientbulkwrite-batch-splits-when-an-ops-payload-exceeds-maxmessagesizebytes
18+
*/
19+
class Prose4_BulkWriteSplitsOnMaxMessageSizeBytesTest extends FunctionalTestCase
20+
{
21+
public function testSplitOnMaxWriteBatchSize(): void
22+
{
23+
if ($this->isServerless()) {
24+
$this->markTestSkipped('bulkWrite command is not supported');
25+
}
26+
27+
$this->skipIfServerVersion('<', '8.0', 'bulkWrite command is not supported');
28+
29+
$client = self::createTestClient();
30+
31+
$hello = $this->getPrimaryServer()->getInfo();
32+
self::assertIsInt($maxBsonObjectSize = $hello['maxBsonObjectSize'] ?? null);
33+
self::assertIsInt($maxMessageSizeBytes = $hello['maxMessageSizeBytes'] ?? null);
34+
35+
$numModels = (int) ($maxMessageSizeBytes / $maxBsonObjectSize + 1);
36+
$document = ['a' => str_repeat('b', $maxBsonObjectSize - 500)];
37+
38+
$collection = $client->selectCollection($this->getDatabaseName(), $this->getCollectionName());
39+
$bulkWrite = BulkWriteCommandBuilder::createWithCollection($collection);
40+
41+
for ($i = 0; $i < $numModels; ++$i) {
42+
$bulkWrite->insertOne($document);
43+
}
44+
45+
$subscriber = new class implements CommandSubscriber {
46+
public array $commandStartedEvents = [];
47+
48+
public function commandStarted(CommandStartedEvent $event): void
49+
{
50+
if ($event->getCommandName() === 'bulkWrite') {
51+
$this->commandStartedEvents[] = $event;
52+
}
53+
}
54+
55+
public function commandSucceeded(CommandSucceededEvent $event): void
56+
{
57+
}
58+
59+
public function commandFailed(CommandFailedEvent $event): void
60+
{
61+
}
62+
};
63+
64+
$client->addSubscriber($subscriber);
65+
66+
$result = $client->bulkWrite($bulkWrite);
67+
68+
self::assertSame($numModels, $result->getInsertedCount());
69+
self::assertCount(2, $subscriber->commandStartedEvents);
70+
[$firstEvent, $secondEvent] = $subscriber->commandStartedEvents;
71+
self::assertIsArray($firstCommandOps = $firstEvent->getCommand()->ops ?? null);
72+
self::assertCount($numModels - 1, $firstCommandOps);
73+
self::assertIsArray($secondCommandOps = $secondEvent->getCommand()->ops ?? null);
74+
self::assertCount(1, $secondCommandOps);
75+
self::assertEquals($firstEvent->getOperationId(), $secondEvent->getOperationId());
76+
}
77+
}

0 commit comments

Comments
 (0)