Skip to content

PHPLIB-935: Propagate Original Error for Write Errors Labeled NoWritesPerformed #1034

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 16, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 91 additions & 0 deletions tests/SpecTests/RetryableWritesSpecTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,17 @@

namespace MongoDB\Tests\SpecTests;

use MongoDB\Driver\Exception\BulkWriteException;
use MongoDB\Driver\Monitoring\CommandFailedEvent;
use MongoDB\Driver\Monitoring\CommandStartedEvent;
use MongoDB\Driver\Monitoring\CommandSubscriber;
use MongoDB\Driver\Monitoring\CommandSucceededEvent;
use stdClass;

use function basename;
use function file_get_contents;
use function glob;
use function version_compare;

/**
* Retryable writes spec tests.
Expand All @@ -16,6 +22,9 @@
*/
class RetryableWritesSpecTest extends FunctionalTestCase
{
public const NOT_PRIMARY = 10107;
public const SHUTDOWN_IN_PROGRESS = 91;

/**
* Execute an individual test case from the specification.
*
Expand Down Expand Up @@ -71,4 +80,86 @@ public function provideTests()

return $testArgs;
}

/**
* Prose test 1: when encountering a NoWritesPerformed error after an error with a RetryableWriteError label
*/
public function testNoWritesPerformedErrorReturnsOriginalError(): void
{
if (! $this->isReplicaSet()) {
$this->markTestSkipped('Test only applies to replica sets');
}

if (version_compare($this->getServerVersion(), '4.4.0', '<')) {
$this->markTestSkipped('NoWritesPerformed error label is only supported on MongoDB 4.4+');
}

$client = self::createTestClient(null, ['retryWrites' => true]);

// Step 2: Configure a fail point with error code 91
$this->configureFailPoint([
'configureFailPoint' => 'failCommand',
'mode' => ['times' => 1],
'data' => [
'writeConcernError' => [
'code' => self::SHUTDOWN_IN_PROGRESS,
'errorLabels' => ['RetryableWriteError'],
],
'failCommands' => ['insert'],
],
]);

$subscriber = new class ($this) implements CommandSubscriber {
private $testCase;

public function __construct(FunctionalTestCase $testCase)
{
$this->testCase = $testCase;
}

public function commandStarted(CommandStartedEvent $event): void
{
}

public function commandSucceeded(CommandSucceededEvent $event): void
{
if ($event->getCommandName() === 'insert') {
// Step 3: Configure a fail point with code 10107
$this->testCase->configureFailPoint([
'configureFailPoint' => 'failCommand',
'mode' => ['times' => 1],
'data' => [
'errorCode' => RetryableWritesSpecTest::NOT_PRIMARY,
'errorLabels' => ['RetryableWriteError', 'NoWritesPerformed'],
'failCommands' => ['insert'],
],
]);
}
}

public function commandFailed(CommandFailedEvent $event): void
{
}
};

$client->getManager()->addSubscriber($subscriber);

// Step 4: Run insertOne
try {
$client->selectCollection('db', 'retryable_writes')->insertOne(['write' => 1]);
} catch (BulkWriteException $e) {
$writeConcernError = $e->getWriteResult()->getWriteConcernError();
$this->assertNotNull($writeConcernError);

// Assert that the write concern error is from the first failpoint
$this->assertSame(self::SHUTDOWN_IN_PROGRESS, $writeConcernError->getCode());
}

// Step 5: Disable the fail point
$client->getManager()->removeSubscriber($subscriber);
$this->configureFailPoint([
'configureFailPoint' => 'failCommand',
'mode' => 'off',
]);
}
}