Skip to content

Commit

Permalink
Fix MongoDB concurrency check
Browse files Browse the repository at this point in the history
  • Loading branch information
sandrokeil committed Aug 26, 2018
1 parent 5d15a0f commit 50a460c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
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 50a460c

Please sign in to comment.