Skip to content
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

Fix MongoDB concurrency check #65

Merged
merged 1 commit into from
Aug 26, 2018
Merged
Show file tree
Hide file tree
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
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Change Log

## [v3.0.1](https://github.com/prooph/event-store-mongodb-adapter/tree/v3.0.1) (2018-08-26)
[Full Changelog](https://github.com/prooph/event-store-mongodb-adapter/compare/v3.0.0...v3.0.1)

**Fixed bugs:**

- MongoDB concurrency check [\#65](https://github.com/prooph/event-store-mongodb-adapter/pull/65) ([sandrokeil](https://github.com/sandrokeil))

## [v3.0.0](https://github.com/prooph/event-store-mongodb-adapter/tree/v3.0.0) (2018-08-16)
[Full Changelog](https://github.com/prooph/event-store-mongodb-adapter/compare/v2.4.1...v3.0.0)

Expand Down
2 changes: 1 addition & 1 deletion src/MongoDbEventStoreAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ public function appendTo(StreamName $streamName, Iterator $streamEvents)
} catch (\MongoDB\Driver\Exception\BulkWriteException $e) {
$code = isset($e->getWriteResult()->getWriteErrors()[0]) ?
$e->getWriteResult()->getWriteErrors()[0]->getCode()
: 0;
: $e->getCode();

if (\in_array($code, [11000, 11001, 12582], true)) {
throw new ConcurrencyException('At least one event with same version exists already', 0, $e);
Expand Down
32 changes: 32 additions & 0 deletions tests/MongoDbEventStoreAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Prooph\Common\Messaging\NoOpMessageConverter;
use Prooph\EventStore\Adapter\MongoDb\Exception\RuntimeAdapterException;
use Prooph\EventStore\Adapter\MongoDb\MongoDbEventStoreAdapter;
use Prooph\EventStore\Exception\ConcurrencyException;
use Prooph\EventStore\Stream\Stream;
use Prooph\EventStore\Stream\StreamName;
use ProophTest\EventStore\Mock\UserCreated;
Expand Down Expand Up @@ -541,6 +542,37 @@ public function it_writes_to_different_streams_in_one_transaction_if_they_exists
$this->assertEquals(2, $this->client->selectCollection($dbName, 'another_test_stream')->countDocuments());
}

/**
* @test
*/
public function it_throws_concurrency_exception_on_duplicate_key_error(): void
{
$dbName = TestUtil::getDatabaseName();

$this->adapter = new MongoDbEventStoreAdapter(
new FQCNMessageFactory(),
new NoOpMessageConverter(),
$this->client,
$dbName,
[
'Prooph\Model\User' => 'test_collection_name',
]
);

$testStream = $this->getTestStream();

$this->adapter->beginTransaction();

$this->adapter->create($testStream);

$this->expectException(ConcurrencyException::class);
$this->expectExceptionMessage('At least one event');

$this->adapter->appendTo($testStream->streamName(), $testStream->streamEvents());

$this->adapter->commit();
}

/**
* @test
*/
Expand Down