|
2 | 2 |
|
3 | 3 | namespace MongoDB\Tests\SpecTests;
|
4 | 4 |
|
| 5 | +use MongoDB\Driver\Exception\BulkWriteException; |
| 6 | +use MongoDB\Driver\Monitoring\CommandFailedEvent; |
| 7 | +use MongoDB\Driver\Monitoring\CommandStartedEvent; |
| 8 | +use MongoDB\Driver\Monitoring\CommandSubscriber; |
| 9 | +use MongoDB\Driver\Monitoring\CommandSucceededEvent; |
5 | 10 | use stdClass;
|
6 | 11 |
|
7 | 12 | use function basename;
|
8 | 13 | use function file_get_contents;
|
9 | 14 | use function glob;
|
| 15 | +use function version_compare; |
10 | 16 |
|
11 | 17 | /**
|
12 | 18 | * Retryable writes spec tests.
|
|
16 | 22 | */
|
17 | 23 | class RetryableWritesSpecTest extends FunctionalTestCase
|
18 | 24 | {
|
| 25 | + public const NOT_PRIMARY = 10107; |
| 26 | + public const SHUTDOWN_IN_PROGRESS = 91; |
| 27 | + |
19 | 28 | /**
|
20 | 29 | * Execute an individual test case from the specification.
|
21 | 30 | *
|
@@ -71,4 +80,86 @@ public function provideTests()
|
71 | 80 |
|
72 | 81 | return $testArgs;
|
73 | 82 | }
|
| 83 | + |
| 84 | + /** |
| 85 | + * Prose test 1: when encountering a NoWritesPerformed error after an error with a RetryableWriteError label |
| 86 | + */ |
| 87 | + public function testNoWritesPerformedErrorReturnsOriginalError(): void |
| 88 | + { |
| 89 | + if (! $this->isReplicaSet()) { |
| 90 | + $this->markTestSkipped('Test only applies to replica sets'); |
| 91 | + } |
| 92 | + |
| 93 | + if (version_compare($this->getServerVersion(), '4.4.0', '<')) { |
| 94 | + $this->markTestSkipped('NoWritesPerformed error label is only supported on MongoDB 4.4+'); |
| 95 | + } |
| 96 | + |
| 97 | + $client = self::createTestClient(null, ['retryWrites' => true]); |
| 98 | + |
| 99 | + // Step 2: Configure a fail point with error code 91 |
| 100 | + $this->configureFailPoint([ |
| 101 | + 'configureFailPoint' => 'failCommand', |
| 102 | + 'mode' => ['times' => 1], |
| 103 | + 'data' => [ |
| 104 | + 'writeConcernError' => [ |
| 105 | + 'code' => self::SHUTDOWN_IN_PROGRESS, |
| 106 | + 'errorLabels' => ['RetryableWriteError'], |
| 107 | + ], |
| 108 | + 'failCommands' => ['insert'], |
| 109 | + ], |
| 110 | + ]); |
| 111 | + |
| 112 | + $subscriber = new class ($this) implements CommandSubscriber { |
| 113 | + private $testCase; |
| 114 | + |
| 115 | + public function __construct(FunctionalTestCase $testCase) |
| 116 | + { |
| 117 | + $this->testCase = $testCase; |
| 118 | + } |
| 119 | + |
| 120 | + public function commandStarted(CommandStartedEvent $event): void |
| 121 | + { |
| 122 | + } |
| 123 | + |
| 124 | + public function commandSucceeded(CommandSucceededEvent $event): void |
| 125 | + { |
| 126 | + if ($event->getCommandName() === 'insert') { |
| 127 | + // Step 3: Configure a fail point with code 10107 |
| 128 | + $this->testCase->configureFailPoint([ |
| 129 | + 'configureFailPoint' => 'failCommand', |
| 130 | + 'mode' => ['times' => 1], |
| 131 | + 'data' => [ |
| 132 | + 'errorCode' => RetryableWritesSpecTest::NOT_PRIMARY, |
| 133 | + 'errorLabels' => ['RetryableWriteError', 'NoWritesPerformed'], |
| 134 | + 'failCommands' => ['insert'], |
| 135 | + ], |
| 136 | + ]); |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + public function commandFailed(CommandFailedEvent $event): void |
| 141 | + { |
| 142 | + } |
| 143 | + }; |
| 144 | + |
| 145 | + $client->getManager()->addSubscriber($subscriber); |
| 146 | + |
| 147 | + // Step 4: Run insertOne |
| 148 | + try { |
| 149 | + $client->selectCollection('db', 'retryable_writes')->insertOne(['write' => 1]); |
| 150 | + } catch (BulkWriteException $e) { |
| 151 | + $writeConcernError = $e->getWriteResult()->getWriteConcernError(); |
| 152 | + $this->assertNotNull($writeConcernError); |
| 153 | + |
| 154 | + // Assert that the write concern error is from the first failpoint |
| 155 | + $this->assertSame(self::SHUTDOWN_IN_PROGRESS, $writeConcernError->getCode()); |
| 156 | + } |
| 157 | + |
| 158 | + // Step 5: Disable the fail point |
| 159 | + $client->getManager()->removeSubscriber($subscriber); |
| 160 | + $this->configureFailPoint([ |
| 161 | + 'configureFailPoint' => 'failCommand', |
| 162 | + 'mode' => 'off', |
| 163 | + ]); |
| 164 | + } |
74 | 165 | }
|
0 commit comments