-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from hirethunk/chris/ver-51-braindead-concurrency
VER-51: Very basic concurrency guards
- Loading branch information
Showing
4 changed files
with
124 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
namespace Thunk\Verbs\Exceptions; | ||
|
||
use RuntimeException; | ||
|
||
class ConcurrencyException extends RuntimeException | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
use Thunk\Verbs\Event; | ||
use Thunk\Verbs\Exceptions\ConcurrencyException; | ||
use Thunk\Verbs\Lifecycle\EventStore; | ||
use Thunk\Verbs\Models\VerbEvent; | ||
use Thunk\Verbs\State; | ||
use Thunk\Verbs\Support\StateCollection; | ||
|
||
it('does not throw on sequential events', function () { | ||
$store = app(EventStore::class); | ||
|
||
$event = new ConcurrencyTestEvent(); | ||
$event->id = 1; | ||
ConcurrencyTestState::singleton()->last_event_id = 1; | ||
|
||
$store->write([$event]); | ||
|
||
$event2 = new ConcurrencyTestEvent(); | ||
$event2->id = 2; | ||
ConcurrencyTestState::singleton()->last_event_id = 2; | ||
|
||
$store->write([$event2]); | ||
|
||
expect(VerbEvent::count())->toBe(2); | ||
}); | ||
|
||
it('throws on non-sequential events', function () { | ||
$store = app(EventStore::class); | ||
|
||
$event = new ConcurrencyTestEvent(); | ||
$event->id = 2; | ||
ConcurrencyTestState::singleton()->last_event_id = 2; | ||
|
||
$store->write([$event]); | ||
|
||
$event2 = new ConcurrencyTestEvent(); | ||
$event2->id = 1; | ||
ConcurrencyTestState::singleton()->last_event_id = 1; | ||
|
||
$store->write([$event2]); | ||
})->throws(ConcurrencyException::class); | ||
|
||
class ConcurrencyTestEvent extends Event | ||
{ | ||
public function states(): StateCollection | ||
{ | ||
return StateCollection::make([ConcurrencyTestState::singleton()]); | ||
} | ||
} | ||
|
||
class ConcurrencyTestState extends State | ||
{ | ||
} |