|
| 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