Skip to content

Commit 7324a5d

Browse files
authored
Merge pull request #65 from sandrokeil/hotfix/concurrency-exception
Fix MongoDB concurrency check
2 parents 5d15a0f + f8c0da5 commit 7324a5d

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# Change Log
22

3+
## [v3.0.1](https://github.com/prooph/event-store-mongodb-adapter/tree/v3.0.1) (2018-08-26)
4+
[Full Changelog](https://github.com/prooph/event-store-mongodb-adapter/compare/v3.0.0...v3.0.1)
5+
6+
**Fixed bugs:**
7+
8+
- MongoDB concurrency check [\#65](https://github.com/prooph/event-store-mongodb-adapter/pull/65) ([sandrokeil](https://github.com/sandrokeil))
9+
310
## [v3.0.0](https://github.com/prooph/event-store-mongodb-adapter/tree/v3.0.0) (2018-08-16)
411
[Full Changelog](https://github.com/prooph/event-store-mongodb-adapter/compare/v2.4.1...v3.0.0)
512

src/MongoDbEventStoreAdapter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ public function appendTo(StreamName $streamName, Iterator $streamEvents)
157157
} catch (\MongoDB\Driver\Exception\BulkWriteException $e) {
158158
$code = isset($e->getWriteResult()->getWriteErrors()[0]) ?
159159
$e->getWriteResult()->getWriteErrors()[0]->getCode()
160-
: 0;
160+
: $e->getCode();
161161

162162
if (\in_array($code, [11000, 11001, 12582], true)) {
163163
throw new ConcurrencyException('At least one event with same version exists already', 0, $e);

tests/MongoDbEventStoreAdapterTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Prooph\Common\Messaging\NoOpMessageConverter;
1818
use Prooph\EventStore\Adapter\MongoDb\Exception\RuntimeAdapterException;
1919
use Prooph\EventStore\Adapter\MongoDb\MongoDbEventStoreAdapter;
20+
use Prooph\EventStore\Exception\ConcurrencyException;
2021
use Prooph\EventStore\Stream\Stream;
2122
use Prooph\EventStore\Stream\StreamName;
2223
use ProophTest\EventStore\Mock\UserCreated;
@@ -541,6 +542,37 @@ public function it_writes_to_different_streams_in_one_transaction_if_they_exists
541542
$this->assertEquals(2, $this->client->selectCollection($dbName, 'another_test_stream')->countDocuments());
542543
}
543544

545+
/**
546+
* @test
547+
*/
548+
public function it_throws_concurrency_exception_on_duplicate_key_error(): void
549+
{
550+
$dbName = TestUtil::getDatabaseName();
551+
552+
$this->adapter = new MongoDbEventStoreAdapter(
553+
new FQCNMessageFactory(),
554+
new NoOpMessageConverter(),
555+
$this->client,
556+
$dbName,
557+
[
558+
'Prooph\Model\User' => 'test_collection_name',
559+
]
560+
);
561+
562+
$testStream = $this->getTestStream();
563+
564+
$this->adapter->beginTransaction();
565+
566+
$this->adapter->create($testStream);
567+
568+
$this->expectException(ConcurrencyException::class);
569+
$this->expectExceptionMessage('At least one event');
570+
571+
$this->adapter->appendTo($testStream->streamName(), $testStream->streamEvents());
572+
573+
$this->adapter->commit();
574+
}
575+
544576
/**
545577
* @test
546578
*/

0 commit comments

Comments
 (0)