Skip to content

Commit

Permalink
Merge pull request #65 from sandrokeil/hotfix/concurrency-exception
Browse files Browse the repository at this point in the history
Fix MongoDB concurrency check
  • Loading branch information
sandrokeil authored Aug 26, 2018
2 parents 5d15a0f + f8c0da5 commit 7324a5d
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
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

0 comments on commit 7324a5d

Please sign in to comment.