Skip to content

Commit 33952f7

Browse files
committed
CRUD prose tests 3 and 4
1 parent 381e31f commit 33952f7

File tree

2 files changed

+142
-0
lines changed

2 files changed

+142
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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/blob/master/source/crud/tests/README.md
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+
$bulkWrite = BulkWriteCommandBuilder::createWithCollection($client->selectCollection($this->getDatabaseName(), $this->getCollectionName()));
33+
34+
for ($i = 0; $i < $maxWriteBatchSize + 1; ++$i) {
35+
$bulkWrite->insertOne(['a' => 'b']);
36+
}
37+
38+
$subscriber = new class implements CommandSubscriber {
39+
public array $commandStartedEvents = [];
40+
41+
public function commandStarted(CommandStartedEvent $event): void
42+
{
43+
$this->commandStartedEvents[] = $event;
44+
}
45+
46+
public function commandSucceeded(CommandSucceededEvent $event): void
47+
{
48+
}
49+
50+
public function commandFailed(CommandFailedEvent $event): void
51+
{
52+
}
53+
};
54+
55+
$client->addSubscriber($subscriber);
56+
57+
$result = $client->bulkWrite($bulkWrite);
58+
59+
self::assertSame($maxWriteBatchSize + 1, $result->getInsertedCount());
60+
self::assertCount(2, $subscriber->commandStartedEvents);
61+
[$firstEvent, $secondEvent] = $subscriber->commandStartedEvents;
62+
self::assertIsArray($firstCommandOps = $firstEvent->getCommand()->ops ?? null);
63+
self::assertCount($maxWriteBatchSize, $firstCommandOps);
64+
self::assertIsArray($secondCommandOps = $secondEvent->getCommand()->ops ?? null);
65+
self::assertCount(1, $secondCommandOps);
66+
self::assertEquals($firstEvent->getOperationId(), $secondEvent->getOperationId());
67+
}
68+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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/blob/master/source/crud/tests/README.md
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+
$bulkWrite = BulkWriteCommandBuilder::createWithCollection($client->selectCollection($this->getDatabaseName(), $this->getCollectionName()));
39+
40+
for ($i = 0; $i < $numModels; ++$i) {
41+
$bulkWrite->insertOne($document);
42+
}
43+
44+
$subscriber = new class implements CommandSubscriber {
45+
public array $commandStartedEvents = [];
46+
47+
public function commandStarted(CommandStartedEvent $event): void
48+
{
49+
$this->commandStartedEvents[] = $event;
50+
}
51+
52+
public function commandSucceeded(CommandSucceededEvent $event): void
53+
{
54+
}
55+
56+
public function commandFailed(CommandFailedEvent $event): void
57+
{
58+
}
59+
};
60+
61+
$client->addSubscriber($subscriber);
62+
63+
$result = $client->bulkWrite($bulkWrite);
64+
65+
self::assertSame($numModels, $result->getInsertedCount());
66+
self::assertCount(2, $subscriber->commandStartedEvents);
67+
[$firstEvent, $secondEvent] = $subscriber->commandStartedEvents;
68+
self::assertIsArray($firstCommandOps = $firstEvent->getCommand()->ops ?? null);
69+
self::assertCount($numModels - 1, $firstCommandOps);
70+
self::assertIsArray($secondCommandOps = $secondEvent->getCommand()->ops ?? null);
71+
self::assertCount(1, $secondCommandOps);
72+
self::assertEquals($firstEvent->getOperationId(), $secondEvent->getOperationId());
73+
}
74+
}

0 commit comments

Comments
 (0)