From f1ef55d9d1e62727425bd76257a1a88dfe8fbe53 Mon Sep 17 00:00:00 2001 From: David Badura Date: Sat, 5 Aug 2023 17:12:55 +0200 Subject: [PATCH 1/7] remove deprecated projector listener --- .../Projector/SyncProjectorListener.php | 23 ---- .../Projector/SyncProjectorListenerTest.php | 101 ------------------ 2 files changed, 124 deletions(-) delete mode 100644 src/Projection/Projector/SyncProjectorListener.php delete mode 100644 tests/Unit/Projection/Projector/SyncProjectorListenerTest.php diff --git a/src/Projection/Projector/SyncProjectorListener.php b/src/Projection/Projector/SyncProjectorListener.php deleted file mode 100644 index db8f056c5..000000000 --- a/src/Projection/Projector/SyncProjectorListener.php +++ /dev/null @@ -1,23 +0,0 @@ -projectorResolver)) - ->handleMessage($message, ...$this->projectorRepository->projectors()); - } -} diff --git a/tests/Unit/Projection/Projector/SyncProjectorListenerTest.php b/tests/Unit/Projection/Projector/SyncProjectorListenerTest.php deleted file mode 100644 index 113f0e9a0..000000000 --- a/tests/Unit/Projection/Projector/SyncProjectorListenerTest.php +++ /dev/null @@ -1,101 +0,0 @@ -message = $message; - } - }; - - $message = new Message( - new ProfileCreated( - ProfileId::fromString('1'), - Email::fromString('foo@bar.com'), - ), - ); - - $projectorRepository = $this->prophesize(ProjectorRepository::class); - $projectorRepository->projectors()->willReturn([$projector])->shouldBeCalledOnce(); - - $resolver = $this->prophesize(ProjectorResolver::class); - $resolver->resolveHandleMethod($projector, $message)->willReturn($projector->handleProfileCreated(...))->shouldBeCalledOnce(); - - $projectionListener = new SyncProjectorListener( - $projectorRepository->reveal(), - $resolver->reveal(), - ); - - $projectionListener($message); - - self::assertSame($message, $projector->message); - } - - public function testNoMethod(): void - { - $projector = new class implements Projector { - public Message|null $message = null; - - public function targetProjection(): ProjectionId - { - return new ProjectionId('dummy', 1); - } - - public function handleProfileCreated(Message $message): void - { - $this->message = $message; - } - }; - - $message = new Message( - new ProfileCreated( - ProfileId::fromString('1'), - Email::fromString('foo@bar.com'), - ), - ); - - $projectorRepository = $this->prophesize(ProjectorRepository::class); - $projectorRepository->projectors()->willReturn([$projector])->shouldBeCalledOnce(); - - $resolver = $this->prophesize(ProjectorResolver::class); - $resolver->resolveHandleMethod($projector, $message)->willReturn(null)->shouldBeCalledOnce(); - - $projectionListener = new SyncProjectorListener( - $projectorRepository->reveal(), - $resolver->reveal(), - ); - - $projectionListener($message); - - self::assertNull($projector->message); - } -} From 4c4f22ba8c9f1ee66eb63223d2865bf2a3d70a48 Mon Sep 17 00:00:00 2001 From: David Badura Date: Sat, 5 Aug 2023 17:37:27 +0200 Subject: [PATCH 2/7] fix tests & add always boot option --- ...r.php => ProjectionistEventBusWrapper.php} | 9 +- tests/Benchmark/WriteEventsBench.php | 36 +++++-- .../IntegrationTest.php | 33 +++++-- .../BasicIntegrationTest.php | 98 ++++++++++++++----- tests/Integration/Outbox/OutboxTest.php | 39 ++++++-- .../Projectionist/ProjectionistTest.php | 4 +- .../RunProjectionistEventBusWrapperTest.php | 6 +- 7 files changed, 164 insertions(+), 61 deletions(-) rename src/Projection/Projectionist/{RunProjectionistEventBusWrapper.php => ProjectionistEventBusWrapper.php} (73%) diff --git a/src/Projection/Projectionist/RunProjectionistEventBusWrapper.php b/src/Projection/Projectionist/ProjectionistEventBusWrapper.php similarity index 73% rename from src/Projection/Projectionist/RunProjectionistEventBusWrapper.php rename to src/Projection/Projectionist/ProjectionistEventBusWrapper.php index f718b971d..d88fb22fa 100644 --- a/src/Projection/Projectionist/RunProjectionistEventBusWrapper.php +++ b/src/Projection/Projectionist/ProjectionistEventBusWrapper.php @@ -8,12 +8,13 @@ use Patchlevel\EventSourcing\EventBus\Message; use Symfony\Component\Lock\LockFactory; -final class RunProjectionistEventBusWrapper implements EventBus +final class ProjectionistEventBusWrapper implements EventBus { public function __construct( private readonly EventBus $parentEventBus, private readonly Projectionist $projectionist, private readonly LockFactory $lockFactory, + private readonly bool $alwaysBoot = false, ) { } @@ -21,13 +22,17 @@ public function dispatch(Message ...$messages): void { $this->parentEventBus->dispatch(...$messages); - $lock = $this->lockFactory->createLock('projectionist-run'); + $lock = $this->lockFactory->createLock('projectionist'); if (!$lock->acquire(true)) { return; } try { + if ($this->alwaysBoot) { + $this->projectionist->boot(); + } + $this->projectionist->run(); } finally { $lock->release(); diff --git a/tests/Benchmark/WriteEventsBench.php b/tests/Benchmark/WriteEventsBench.php index aec9e5434..59a0b9acb 100644 --- a/tests/Benchmark/WriteEventsBench.php +++ b/tests/Benchmark/WriteEventsBench.php @@ -9,8 +9,10 @@ use Patchlevel\EventSourcing\EventBus\DefaultEventBus; use Patchlevel\EventSourcing\EventBus\EventBus; use Patchlevel\EventSourcing\Metadata\AggregateRoot\AttributeAggregateRootRegistryFactory; +use Patchlevel\EventSourcing\Projection\Projection\Store\InMemoryStore; +use Patchlevel\EventSourcing\Projection\Projectionist\DefaultProjectionist; +use Patchlevel\EventSourcing\Projection\Projectionist\ProjectionistEventBusWrapper; use Patchlevel\EventSourcing\Projection\Projector\InMemoryProjectorRepository; -use Patchlevel\EventSourcing\Projection\Projector\SyncProjectorListener; use Patchlevel\EventSourcing\Repository\DefaultRepository; use Patchlevel\EventSourcing\Repository\Repository; use Patchlevel\EventSourcing\Schema\DoctrineSchemaDirector; @@ -22,6 +24,8 @@ use Patchlevel\EventSourcing\Tests\Benchmark\BasicImplementation\ProfileId; use Patchlevel\EventSourcing\Tests\Benchmark\BasicImplementation\Projection\ProfileProjector; use PhpBench\Attributes as Bench; +use Symfony\Component\Lock\LockFactory; +use Symfony\Component\Lock\Store\InMemoryStore as LockInMemoryStore; use function file_exists; use function unlink; @@ -47,20 +51,34 @@ public function setUp(): void 'path' => self::DB_PATH, ]); + $this->store = new DoctrineDbalStore( + $connection, + DefaultEventSerializer::createFromPaths([__DIR__ . '/BasicImplementation/Events']), + (new AttributeAggregateRootRegistryFactory())->create([__DIR__ . '/BasicImplementation/Aggregate']), + 'eventstore', + ); + $profileProjection = new ProfileProjector($connection); $projectionRepository = new InMemoryProjectorRepository( [$profileProjection], ); - $this->bus = new DefaultEventBus(); - $this->bus->addListener(new SyncProjectorListener($projectionRepository)); - $this->bus->addListener(new SendEmailProcessor()); + $projectionist = new DefaultProjectionist( + $this->store, + new InMemoryStore(), + $projectionRepository, + ); - $this->store = new DoctrineDbalStore( - $connection, - DefaultEventSerializer::createFromPaths([__DIR__ . '/BasicImplementation/Events']), - (new AttributeAggregateRootRegistryFactory())->create([__DIR__ . '/BasicImplementation/Aggregate']), - 'eventstore', + $innerEventStream = new DefaultEventBus(); + $innerEventStream->addListener(new SendEmailProcessor()); + + $this->bus = new ProjectionistEventBusWrapper( + $innerEventStream, + $projectionist, + new LockFactory( + new LockInMemoryStore(), + ), + true, ); $this->repository = new DefaultRepository($this->store, $this->bus, Profile::metadata()); diff --git a/tests/Integration/BankAccountSplitStream/IntegrationTest.php b/tests/Integration/BankAccountSplitStream/IntegrationTest.php index dd4c23662..4db63fba6 100644 --- a/tests/Integration/BankAccountSplitStream/IntegrationTest.php +++ b/tests/Integration/BankAccountSplitStream/IntegrationTest.php @@ -11,8 +11,10 @@ use Patchlevel\EventSourcing\Metadata\AggregateRoot\AggregateRootRegistry; use Patchlevel\EventSourcing\Metadata\AggregateRoot\AttributeAggregateRootRegistryFactory; use Patchlevel\EventSourcing\Metadata\Event\AttributeEventMetadataFactory; +use Patchlevel\EventSourcing\Projection\Projection\Store\InMemoryStore; +use Patchlevel\EventSourcing\Projection\Projectionist\DefaultProjectionist; +use Patchlevel\EventSourcing\Projection\Projectionist\ProjectionistEventBusWrapper; use Patchlevel\EventSourcing\Projection\Projector\InMemoryProjectorRepository; -use Patchlevel\EventSourcing\Projection\Projector\SyncProjectorListener; use Patchlevel\EventSourcing\Repository\DefaultRepositoryManager; use Patchlevel\EventSourcing\Schema\DoctrineSchemaDirector; use Patchlevel\EventSourcing\Serializer\DefaultEventSerializer; @@ -24,6 +26,8 @@ use Patchlevel\EventSourcing\Tests\Integration\BankAccountSplitStream\Projection\BankAccountProjection; use Patchlevel\EventSourcing\Tests\Integration\DbalManager; use PHPUnit\Framework\TestCase; +use Symfony\Component\Lock\LockFactory; +use Symfony\Component\Lock\Store\InMemoryStore as LockInMemoryStore; use function count; @@ -42,14 +46,8 @@ public function tearDown(): void $this->connection->close(); } - public function testSingleTableSuccessful(): void + public function testSuccessful(): void { - $bankAccountProjection = new BankAccountProjection($this->connection); - $projectionRepository = new InMemoryProjectorRepository([$bankAccountProjection]); - - $eventStream = new DefaultEventBus(); - $eventStream->addListener(new SyncProjectorListener($projectionRepository)); - $store = new DoctrineDbalStore( $this->connection, DefaultEventSerializer::createFromPaths([__DIR__ . '/Events']), @@ -57,6 +55,24 @@ public function testSingleTableSuccessful(): void 'eventstore', ); + $bankAccountProjection = new BankAccountProjection($this->connection); + $projectionRepository = new InMemoryProjectorRepository([$bankAccountProjection]); + + $projectionist = new DefaultProjectionist( + $store, + new InMemoryStore(), + $projectionRepository, + ); + + $eventStream = new ProjectionistEventBusWrapper( + new DefaultEventBus(), + $projectionist, + new LockFactory( + new LockInMemoryStore(), + ), + true, + ); + $manager = new DefaultRepositoryManager( new AggregateRootRegistry(['bank_account' => BankAccount::class]), $store, @@ -74,7 +90,6 @@ public function testSingleTableSuccessful(): void ); $schemaDirector->create(); - $bankAccountProjection->create(); $bankAccount = BankAccount::create(AccountId::fromString('1'), 'John'); $bankAccount->addBalance(100); diff --git a/tests/Integration/BasicImplementation/BasicIntegrationTest.php b/tests/Integration/BasicImplementation/BasicIntegrationTest.php index 45ff247a3..5b5587403 100644 --- a/tests/Integration/BasicImplementation/BasicIntegrationTest.php +++ b/tests/Integration/BasicImplementation/BasicIntegrationTest.php @@ -9,8 +9,10 @@ use Patchlevel\EventSourcing\EventBus\SymfonyEventBus; use Patchlevel\EventSourcing\Metadata\AggregateRoot\AggregateRootRegistry; use Patchlevel\EventSourcing\Metadata\AggregateRoot\AttributeAggregateRootRegistryFactory; +use Patchlevel\EventSourcing\Projection\Projection\Store\InMemoryStore; +use Patchlevel\EventSourcing\Projection\Projectionist\DefaultProjectionist; +use Patchlevel\EventSourcing\Projection\Projectionist\ProjectionistEventBusWrapper; use Patchlevel\EventSourcing\Projection\Projector\InMemoryProjectorRepository; -use Patchlevel\EventSourcing\Projection\Projector\SyncProjectorListener; use Patchlevel\EventSourcing\Repository\DefaultRepositoryManager; use Patchlevel\EventSourcing\Schema\DoctrineSchemaDirector; use Patchlevel\EventSourcing\Serializer\DefaultEventSerializer; @@ -23,6 +25,8 @@ use Patchlevel\EventSourcing\Tests\Integration\BasicImplementation\Projection\ProfileProjection; use Patchlevel\EventSourcing\Tests\Integration\DbalManager; use PHPUnit\Framework\TestCase; +use Symfony\Component\Lock\LockFactory; +use Symfony\Component\Lock\Store\InMemoryStore as LockInMemoryStore; /** @coversNothing */ final class BasicIntegrationTest extends TestCase @@ -42,20 +46,34 @@ public function tearDown(): void public function testSuccessful(): void { + $store = new DoctrineDbalStore( + $this->connection, + DefaultEventSerializer::createFromPaths([__DIR__ . '/Events']), + (new AttributeAggregateRootRegistryFactory())->create([__DIR__ . '/Aggregate']), + 'eventstore', + ); + $profileProjection = new ProfileProjection($this->connection); $projectorRepository = new InMemoryProjectorRepository( [$profileProjection], ); - $eventStream = new DefaultEventBus(); - $eventStream->addListener(new SyncProjectorListener($projectorRepository)); - $eventStream->addListener(new SendEmailProcessor()); + $projectionist = new DefaultProjectionist( + $store, + new InMemoryStore(), + $projectorRepository, + ); - $store = new DoctrineDbalStore( - $this->connection, - DefaultEventSerializer::createFromPaths([__DIR__ . '/Events']), - (new AttributeAggregateRootRegistryFactory())->create([__DIR__ . '/Aggregate']), - 'eventstore', + $innerEventStream = new DefaultEventBus(); + $innerEventStream->addListener(new SendEmailProcessor()); + + $eventStream = new ProjectionistEventBusWrapper( + $innerEventStream, + $projectionist, + new LockFactory( + new LockInMemoryStore(), + ), + true, ); $manager = new DefaultRepositoryManager( @@ -73,7 +91,6 @@ public function testSuccessful(): void ); $schemaDirector->create(); - $profileProjection->create(); $profile = Profile::create(ProfileId::fromString('1'), 'John'); $repository->save($profile); @@ -102,21 +119,35 @@ public function testSuccessful(): void public function testWithSymfonySuccessful(): void { + $store = new DoctrineDbalStore( + $this->connection, + DefaultEventSerializer::createFromPaths([__DIR__ . '/Events']), + (new AttributeAggregateRootRegistryFactory())->create([__DIR__ . '/Aggregate']), + 'eventstore', + ); + $profileProjection = new ProfileProjection($this->connection); $projectorRepository = new InMemoryProjectorRepository( [$profileProjection], ); - $eventStream = SymfonyEventBus::create([ - new SyncProjectorListener($projectorRepository), + $projectionist = new DefaultProjectionist( + $store, + new InMemoryStore(), + $projectorRepository, + ); + + $innerEventStream = SymfonyEventBus::create([ new SendEmailProcessor(), ]); - $store = new DoctrineDbalStore( - $this->connection, - DefaultEventSerializer::createFromPaths([__DIR__ . '/Events']), - (new AttributeAggregateRootRegistryFactory())->create([__DIR__ . '/Aggregate']), - 'eventstore', + $eventStream = new ProjectionistEventBusWrapper( + $innerEventStream, + $projectionist, + new LockFactory( + new LockInMemoryStore(), + ), + true, ); $manager = new DefaultRepositoryManager( @@ -124,6 +155,7 @@ public function testWithSymfonySuccessful(): void $store, $eventStream, ); + $repository = $manager->get(Profile::class); $schemaDirector = new DoctrineSchemaDirector( @@ -132,7 +164,6 @@ public function testWithSymfonySuccessful(): void ); $schemaDirector->create(); - $profileProjection->create(); $profile = Profile::create(ProfileId::fromString('1'), 'John'); $repository->save($profile); @@ -164,20 +195,34 @@ public function testWithSymfonySuccessful(): void public function testSnapshot(): void { + $store = new DoctrineDbalStore( + $this->connection, + DefaultEventSerializer::createFromPaths([__DIR__ . '/Events']), + (new AttributeAggregateRootRegistryFactory())->create([__DIR__ . '/Aggregate']), + 'eventstore', + ); + $profileProjection = new ProfileProjection($this->connection); $projectorRepository = new InMemoryProjectorRepository( [$profileProjection], ); - $eventStream = new DefaultEventBus(); - $eventStream->addListener(new SyncProjectorListener($projectorRepository)); - $eventStream->addListener(new SendEmailProcessor()); + $projectionist = new DefaultProjectionist( + $store, + new InMemoryStore(), + $projectorRepository, + ); - $store = new DoctrineDbalStore( - $this->connection, - DefaultEventSerializer::createFromPaths([__DIR__ . '/Events']), - (new AttributeAggregateRootRegistryFactory())->create([__DIR__ . '/Aggregate']), - 'eventstore', + $innerEventStream = new DefaultEventBus(); + $innerEventStream->addListener(new SendEmailProcessor()); + + $eventStream = new ProjectionistEventBusWrapper( + $innerEventStream, + $projectionist, + new LockFactory( + new LockInMemoryStore(), + ), + true, ); $manager = new DefaultRepositoryManager( @@ -195,7 +240,6 @@ public function testSnapshot(): void ); $schemaDirector->create(); - $profileProjection->create(); $profile = Profile::create(ProfileId::fromString('1'), 'John'); $repository->save($profile); diff --git a/tests/Integration/Outbox/OutboxTest.php b/tests/Integration/Outbox/OutboxTest.php index 1b9a1c1b8..768088084 100644 --- a/tests/Integration/Outbox/OutboxTest.php +++ b/tests/Integration/Outbox/OutboxTest.php @@ -10,8 +10,10 @@ use Patchlevel\EventSourcing\Outbox\DoctrineOutboxStore; use Patchlevel\EventSourcing\Outbox\OutboxEventBus; use Patchlevel\EventSourcing\Outbox\StoreOutboxConsumer; +use Patchlevel\EventSourcing\Projection\Projection\Store\InMemoryStore; +use Patchlevel\EventSourcing\Projection\Projectionist\DefaultProjectionist; +use Patchlevel\EventSourcing\Projection\Projectionist\ProjectionistEventBusWrapper; use Patchlevel\EventSourcing\Projection\Projector\InMemoryProjectorRepository; -use Patchlevel\EventSourcing\Projection\Projector\SyncProjectorListener; use Patchlevel\EventSourcing\Repository\DefaultRepository; use Patchlevel\EventSourcing\Schema\ChainSchemaConfigurator; use Patchlevel\EventSourcing\Schema\DoctrineSchemaDirector; @@ -23,6 +25,8 @@ use Patchlevel\EventSourcing\Tests\Integration\Outbox\Processor\SendEmailProcessor; use Patchlevel\EventSourcing\Tests\Integration\Outbox\Projection\ProfileProjection; use PHPUnit\Framework\TestCase; +use Symfony\Component\Lock\LockFactory; +use Symfony\Component\Lock\Store\InMemoryStore as LockInMemoryStore; /** @coversNothing */ final class OutboxTest extends TestCase @@ -42,11 +46,6 @@ public function tearDown(): void public function testSuccessful(): void { - $profileProjection = new ProfileProjection($this->connection); - $projectionRepository = new InMemoryProjectorRepository( - [$profileProjection], - ); - $serializer = DefaultEventSerializer::createFromPaths([__DIR__ . '/Events']); $registry = (new AttributeAggregateRootRegistryFactory())->create([__DIR__ . '/Aggregate']); @@ -65,11 +64,34 @@ public function testSuccessful(): void ); $realEventBus = new DefaultEventBus(); - $realEventBus->addListener(new SyncProjectorListener($projectionRepository)); $realEventBus->addListener(new SendEmailProcessor()); $outboxEventBus = new OutboxEventBus($outboxStore); - $repository = new DefaultRepository($store, $outboxEventBus, Profile::metadata()); + + $profileProjection = new ProfileProjection($this->connection); + $projectorRepository = new InMemoryProjectorRepository( + [$profileProjection], + ); + + $projectionist = new DefaultProjectionist( + $store, + new InMemoryStore(), + $projectorRepository, + ); + + $realEventBus = new DefaultEventBus(); + $realEventBus->addListener(new SendEmailProcessor()); + + $eventStream = new ProjectionistEventBusWrapper( + $outboxEventBus, + $projectionist, + new LockFactory( + new LockInMemoryStore(), + ), + true, + ); + + $repository = new DefaultRepository($store, $eventStream, Profile::metadata()); $schemaDirector = new DoctrineSchemaDirector( $this->connection, @@ -80,7 +102,6 @@ public function testSuccessful(): void ); $schemaDirector->create(); - $profileProjection->create(); $profile = Profile::create(ProfileId::fromString('1'), 'John'); $repository->save($profile); diff --git a/tests/Integration/Projectionist/ProjectionistTest.php b/tests/Integration/Projectionist/ProjectionistTest.php index 77bc3fd20..d74ba7787 100644 --- a/tests/Integration/Projectionist/ProjectionistTest.php +++ b/tests/Integration/Projectionist/ProjectionistTest.php @@ -11,7 +11,7 @@ use Patchlevel\EventSourcing\Metadata\AggregateRoot\AttributeAggregateRootRegistryFactory; use Patchlevel\EventSourcing\Projection\Projection\Store\DoctrineStore; use Patchlevel\EventSourcing\Projection\Projectionist\DefaultProjectionist; -use Patchlevel\EventSourcing\Projection\Projectionist\RunProjectionistEventBusWrapper; +use Patchlevel\EventSourcing\Projection\Projectionist\ProjectionistEventBusWrapper; use Patchlevel\EventSourcing\Projection\Projector\InMemoryProjectorRepository; use Patchlevel\EventSourcing\Repository\DefaultRepositoryManager; use Patchlevel\EventSourcing\Schema\ChainSchemaConfigurator; @@ -118,7 +118,7 @@ public function testSync(): void $manager = new DefaultRepositoryManager( $aggregateRegistry, $store, - new RunProjectionistEventBusWrapper( + new ProjectionistEventBusWrapper( new DefaultEventBus(), $projectionist, new LockFactory($lockStore), diff --git a/tests/Unit/Projection/Projectionist/RunProjectionistEventBusWrapperTest.php b/tests/Unit/Projection/Projectionist/RunProjectionistEventBusWrapperTest.php index 28c19a68b..2e2879056 100644 --- a/tests/Unit/Projection/Projectionist/RunProjectionistEventBusWrapperTest.php +++ b/tests/Unit/Projection/Projectionist/RunProjectionistEventBusWrapperTest.php @@ -7,13 +7,13 @@ use Patchlevel\EventSourcing\EventBus\EventBus; use Patchlevel\EventSourcing\EventBus\Message; use Patchlevel\EventSourcing\Projection\Projectionist\Projectionist; -use Patchlevel\EventSourcing\Projection\Projectionist\RunProjectionistEventBusWrapper; +use Patchlevel\EventSourcing\Projection\Projectionist\ProjectionistEventBusWrapper; use Patchlevel\EventSourcing\Tests\Unit\Fixture\ProfileId; use Patchlevel\EventSourcing\Tests\Unit\Fixture\ProfileVisited; use PHPUnit\Framework\TestCase; use Prophecy\PhpUnit\ProphecyTrait; -/** @covers \Patchlevel\EventSourcing\Projection\Projectionist\RunProjectionistEventBusWrapper */ +/** @covers \Patchlevel\EventSourcing\Projection\Projectionist\ProjectionistEventBusWrapper */ final class RunProjectionistEventBusWrapperTest extends TestCase { use ProphecyTrait; @@ -35,7 +35,7 @@ public function testDispatch(): void $projectionist->run()->shouldBeCalledOnce(); $projectionist->reveal(); - $eventBus = new RunProjectionistEventBusWrapper( + $eventBus = new ProjectionistEventBusWrapper( $parentEventBus->reveal(), $projectionist->reveal(), ); From 95cedd2f44b75f4d0f490d48b76bb035216168ca Mon Sep 17 00:00:00 2001 From: David Badura Date: Sat, 5 Aug 2023 21:47:30 +0200 Subject: [PATCH 3/7] debug --- src/Store/DoctrineDbalStore.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Store/DoctrineDbalStore.php b/src/Store/DoctrineDbalStore.php index fb2a57871..9aa3ba3e1 100644 --- a/src/Store/DoctrineDbalStore.php +++ b/src/Store/DoctrineDbalStore.php @@ -146,7 +146,8 @@ function (Connection $connection) use ($messages): void { */ public function transactional(Closure $function): void { - $this->connection->transactional($function); + $function(); + //$this->connection->transactional($function); } /** @param class-string $aggregate */ From be8071edac63e810ced76c4fe875c8937b9126cd Mon Sep 17 00:00:00 2001 From: David Badura Date: Sun, 6 Aug 2023 17:34:45 +0200 Subject: [PATCH 4/7] remove always boot option because broken transactions --- .../Projectionist/ProjectionistEventBusWrapper.php | 7 +------ src/Store/DoctrineDbalStore.php | 3 +-- tests/Benchmark/WriteEventsBench.php | 3 +-- .../Integration/BankAccountSplitStream/IntegrationTest.php | 2 +- .../BasicImplementation/BasicIntegrationTest.php | 6 +++--- tests/Integration/Outbox/OutboxTest.php | 2 +- 6 files changed, 8 insertions(+), 15 deletions(-) diff --git a/src/Projection/Projectionist/ProjectionistEventBusWrapper.php b/src/Projection/Projectionist/ProjectionistEventBusWrapper.php index d88fb22fa..e52dff011 100644 --- a/src/Projection/Projectionist/ProjectionistEventBusWrapper.php +++ b/src/Projection/Projectionist/ProjectionistEventBusWrapper.php @@ -14,7 +14,6 @@ public function __construct( private readonly EventBus $parentEventBus, private readonly Projectionist $projectionist, private readonly LockFactory $lockFactory, - private readonly bool $alwaysBoot = false, ) { } @@ -22,17 +21,13 @@ public function dispatch(Message ...$messages): void { $this->parentEventBus->dispatch(...$messages); - $lock = $this->lockFactory->createLock('projectionist'); + $lock = $this->lockFactory->createLock('projectionist-run'); if (!$lock->acquire(true)) { return; } try { - if ($this->alwaysBoot) { - $this->projectionist->boot(); - } - $this->projectionist->run(); } finally { $lock->release(); diff --git a/src/Store/DoctrineDbalStore.php b/src/Store/DoctrineDbalStore.php index 9aa3ba3e1..fb2a57871 100644 --- a/src/Store/DoctrineDbalStore.php +++ b/src/Store/DoctrineDbalStore.php @@ -146,8 +146,7 @@ function (Connection $connection) use ($messages): void { */ public function transactional(Closure $function): void { - $function(); - //$this->connection->transactional($function); + $this->connection->transactional($function); } /** @param class-string $aggregate */ diff --git a/tests/Benchmark/WriteEventsBench.php b/tests/Benchmark/WriteEventsBench.php index 59a0b9acb..7c91d6007 100644 --- a/tests/Benchmark/WriteEventsBench.php +++ b/tests/Benchmark/WriteEventsBench.php @@ -78,7 +78,6 @@ public function setUp(): void new LockFactory( new LockInMemoryStore(), ), - true, ); $this->repository = new DefaultRepository($this->store, $this->bus, Profile::metadata()); @@ -89,7 +88,7 @@ public function setUp(): void ); $schemaDirector->create(); - $profileProjection->create(); + $projectionist->boot(); $this->profile = Profile::create(ProfileId::fromString('1'), 'Peter'); $this->repository->save($this->profile); diff --git a/tests/Integration/BankAccountSplitStream/IntegrationTest.php b/tests/Integration/BankAccountSplitStream/IntegrationTest.php index 4db63fba6..ffb340885 100644 --- a/tests/Integration/BankAccountSplitStream/IntegrationTest.php +++ b/tests/Integration/BankAccountSplitStream/IntegrationTest.php @@ -70,7 +70,6 @@ public function testSuccessful(): void new LockFactory( new LockInMemoryStore(), ), - true, ); $manager = new DefaultRepositoryManager( @@ -90,6 +89,7 @@ public function testSuccessful(): void ); $schemaDirector->create(); + $projectionist->boot(); $bankAccount = BankAccount::create(AccountId::fromString('1'), 'John'); $bankAccount->addBalance(100); diff --git a/tests/Integration/BasicImplementation/BasicIntegrationTest.php b/tests/Integration/BasicImplementation/BasicIntegrationTest.php index 5b5587403..a41470534 100644 --- a/tests/Integration/BasicImplementation/BasicIntegrationTest.php +++ b/tests/Integration/BasicImplementation/BasicIntegrationTest.php @@ -73,7 +73,6 @@ public function testSuccessful(): void new LockFactory( new LockInMemoryStore(), ), - true, ); $manager = new DefaultRepositoryManager( @@ -91,6 +90,7 @@ public function testSuccessful(): void ); $schemaDirector->create(); + $projectionist->boot(); $profile = Profile::create(ProfileId::fromString('1'), 'John'); $repository->save($profile); @@ -147,7 +147,6 @@ public function testWithSymfonySuccessful(): void new LockFactory( new LockInMemoryStore(), ), - true, ); $manager = new DefaultRepositoryManager( @@ -164,6 +163,7 @@ public function testWithSymfonySuccessful(): void ); $schemaDirector->create(); + $projectionist->boot(); $profile = Profile::create(ProfileId::fromString('1'), 'John'); $repository->save($profile); @@ -222,7 +222,6 @@ public function testSnapshot(): void new LockFactory( new LockInMemoryStore(), ), - true, ); $manager = new DefaultRepositoryManager( @@ -240,6 +239,7 @@ public function testSnapshot(): void ); $schemaDirector->create(); + $projectionist->boot(); $profile = Profile::create(ProfileId::fromString('1'), 'John'); $repository->save($profile); diff --git a/tests/Integration/Outbox/OutboxTest.php b/tests/Integration/Outbox/OutboxTest.php index 768088084..52e1d3f38 100644 --- a/tests/Integration/Outbox/OutboxTest.php +++ b/tests/Integration/Outbox/OutboxTest.php @@ -88,7 +88,6 @@ public function testSuccessful(): void new LockFactory( new LockInMemoryStore(), ), - true, ); $repository = new DefaultRepository($store, $eventStream, Profile::metadata()); @@ -102,6 +101,7 @@ public function testSuccessful(): void ); $schemaDirector->create(); + $projectionist->boot(); $profile = Profile::create(ProfileId::fromString('1'), 'John'); $repository->save($profile); From c5cc3cd3217eeaca83ddc329e412caa9f5f91840 Mon Sep 17 00:00:00 2001 From: David Badura Date: Mon, 7 Aug 2023 07:47:38 +0200 Subject: [PATCH 5/7] rename projectionist event bus wrapper --- ...usWrapper.php => SyncProjectionistEventBusWrapper.php} | 2 +- tests/Benchmark/WriteEventsBench.php | 4 ++-- .../BankAccountSplitStream/IntegrationTest.php | 4 ++-- .../BasicImplementation/BasicIntegrationTest.php | 8 ++++---- tests/Integration/Outbox/OutboxTest.php | 4 ++-- tests/Integration/Projectionist/ProjectionistTest.php | 4 ++-- ...rTest.php => SyncProjectionistEventBusWrapperTest.php} | 8 ++++---- 7 files changed, 17 insertions(+), 17 deletions(-) rename src/Projection/Projectionist/{ProjectionistEventBusWrapper.php => SyncProjectionistEventBusWrapper.php} (92%) rename tests/Unit/Projection/Projectionist/{RunProjectionistEventBusWrapperTest.php => SyncProjectionistEventBusWrapperTest.php} (80%) diff --git a/src/Projection/Projectionist/ProjectionistEventBusWrapper.php b/src/Projection/Projectionist/SyncProjectionistEventBusWrapper.php similarity index 92% rename from src/Projection/Projectionist/ProjectionistEventBusWrapper.php rename to src/Projection/Projectionist/SyncProjectionistEventBusWrapper.php index e52dff011..bea2ef476 100644 --- a/src/Projection/Projectionist/ProjectionistEventBusWrapper.php +++ b/src/Projection/Projectionist/SyncProjectionistEventBusWrapper.php @@ -8,7 +8,7 @@ use Patchlevel\EventSourcing\EventBus\Message; use Symfony\Component\Lock\LockFactory; -final class ProjectionistEventBusWrapper implements EventBus +final class SyncProjectionistEventBusWrapper implements EventBus { public function __construct( private readonly EventBus $parentEventBus, diff --git a/tests/Benchmark/WriteEventsBench.php b/tests/Benchmark/WriteEventsBench.php index 7c91d6007..33fde26db 100644 --- a/tests/Benchmark/WriteEventsBench.php +++ b/tests/Benchmark/WriteEventsBench.php @@ -11,7 +11,7 @@ use Patchlevel\EventSourcing\Metadata\AggregateRoot\AttributeAggregateRootRegistryFactory; use Patchlevel\EventSourcing\Projection\Projection\Store\InMemoryStore; use Patchlevel\EventSourcing\Projection\Projectionist\DefaultProjectionist; -use Patchlevel\EventSourcing\Projection\Projectionist\ProjectionistEventBusWrapper; +use Patchlevel\EventSourcing\Projection\Projectionist\SyncProjectionistEventBusWrapper; use Patchlevel\EventSourcing\Projection\Projector\InMemoryProjectorRepository; use Patchlevel\EventSourcing\Repository\DefaultRepository; use Patchlevel\EventSourcing\Repository\Repository; @@ -72,7 +72,7 @@ public function setUp(): void $innerEventStream = new DefaultEventBus(); $innerEventStream->addListener(new SendEmailProcessor()); - $this->bus = new ProjectionistEventBusWrapper( + $this->bus = new SyncProjectionistEventBusWrapper( $innerEventStream, $projectionist, new LockFactory( diff --git a/tests/Integration/BankAccountSplitStream/IntegrationTest.php b/tests/Integration/BankAccountSplitStream/IntegrationTest.php index ffb340885..734230f43 100644 --- a/tests/Integration/BankAccountSplitStream/IntegrationTest.php +++ b/tests/Integration/BankAccountSplitStream/IntegrationTest.php @@ -13,7 +13,7 @@ use Patchlevel\EventSourcing\Metadata\Event\AttributeEventMetadataFactory; use Patchlevel\EventSourcing\Projection\Projection\Store\InMemoryStore; use Patchlevel\EventSourcing\Projection\Projectionist\DefaultProjectionist; -use Patchlevel\EventSourcing\Projection\Projectionist\ProjectionistEventBusWrapper; +use Patchlevel\EventSourcing\Projection\Projectionist\SyncProjectionistEventBusWrapper; use Patchlevel\EventSourcing\Projection\Projector\InMemoryProjectorRepository; use Patchlevel\EventSourcing\Repository\DefaultRepositoryManager; use Patchlevel\EventSourcing\Schema\DoctrineSchemaDirector; @@ -64,7 +64,7 @@ public function testSuccessful(): void $projectionRepository, ); - $eventStream = new ProjectionistEventBusWrapper( + $eventStream = new SyncProjectionistEventBusWrapper( new DefaultEventBus(), $projectionist, new LockFactory( diff --git a/tests/Integration/BasicImplementation/BasicIntegrationTest.php b/tests/Integration/BasicImplementation/BasicIntegrationTest.php index a41470534..61c8f8e8d 100644 --- a/tests/Integration/BasicImplementation/BasicIntegrationTest.php +++ b/tests/Integration/BasicImplementation/BasicIntegrationTest.php @@ -11,7 +11,7 @@ use Patchlevel\EventSourcing\Metadata\AggregateRoot\AttributeAggregateRootRegistryFactory; use Patchlevel\EventSourcing\Projection\Projection\Store\InMemoryStore; use Patchlevel\EventSourcing\Projection\Projectionist\DefaultProjectionist; -use Patchlevel\EventSourcing\Projection\Projectionist\ProjectionistEventBusWrapper; +use Patchlevel\EventSourcing\Projection\Projectionist\SyncProjectionistEventBusWrapper; use Patchlevel\EventSourcing\Projection\Projector\InMemoryProjectorRepository; use Patchlevel\EventSourcing\Repository\DefaultRepositoryManager; use Patchlevel\EventSourcing\Schema\DoctrineSchemaDirector; @@ -67,7 +67,7 @@ public function testSuccessful(): void $innerEventStream = new DefaultEventBus(); $innerEventStream->addListener(new SendEmailProcessor()); - $eventStream = new ProjectionistEventBusWrapper( + $eventStream = new SyncProjectionistEventBusWrapper( $innerEventStream, $projectionist, new LockFactory( @@ -141,7 +141,7 @@ public function testWithSymfonySuccessful(): void new SendEmailProcessor(), ]); - $eventStream = new ProjectionistEventBusWrapper( + $eventStream = new SyncProjectionistEventBusWrapper( $innerEventStream, $projectionist, new LockFactory( @@ -216,7 +216,7 @@ public function testSnapshot(): void $innerEventStream = new DefaultEventBus(); $innerEventStream->addListener(new SendEmailProcessor()); - $eventStream = new ProjectionistEventBusWrapper( + $eventStream = new SyncProjectionistEventBusWrapper( $innerEventStream, $projectionist, new LockFactory( diff --git a/tests/Integration/Outbox/OutboxTest.php b/tests/Integration/Outbox/OutboxTest.php index 52e1d3f38..44ac1d23e 100644 --- a/tests/Integration/Outbox/OutboxTest.php +++ b/tests/Integration/Outbox/OutboxTest.php @@ -12,7 +12,7 @@ use Patchlevel\EventSourcing\Outbox\StoreOutboxConsumer; use Patchlevel\EventSourcing\Projection\Projection\Store\InMemoryStore; use Patchlevel\EventSourcing\Projection\Projectionist\DefaultProjectionist; -use Patchlevel\EventSourcing\Projection\Projectionist\ProjectionistEventBusWrapper; +use Patchlevel\EventSourcing\Projection\Projectionist\SyncProjectionistEventBusWrapper; use Patchlevel\EventSourcing\Projection\Projector\InMemoryProjectorRepository; use Patchlevel\EventSourcing\Repository\DefaultRepository; use Patchlevel\EventSourcing\Schema\ChainSchemaConfigurator; @@ -82,7 +82,7 @@ public function testSuccessful(): void $realEventBus = new DefaultEventBus(); $realEventBus->addListener(new SendEmailProcessor()); - $eventStream = new ProjectionistEventBusWrapper( + $eventStream = new SyncProjectionistEventBusWrapper( $outboxEventBus, $projectionist, new LockFactory( diff --git a/tests/Integration/Projectionist/ProjectionistTest.php b/tests/Integration/Projectionist/ProjectionistTest.php index d74ba7787..578efd4f3 100644 --- a/tests/Integration/Projectionist/ProjectionistTest.php +++ b/tests/Integration/Projectionist/ProjectionistTest.php @@ -11,7 +11,7 @@ use Patchlevel\EventSourcing\Metadata\AggregateRoot\AttributeAggregateRootRegistryFactory; use Patchlevel\EventSourcing\Projection\Projection\Store\DoctrineStore; use Patchlevel\EventSourcing\Projection\Projectionist\DefaultProjectionist; -use Patchlevel\EventSourcing\Projection\Projectionist\ProjectionistEventBusWrapper; +use Patchlevel\EventSourcing\Projection\Projectionist\SyncProjectionistEventBusWrapper; use Patchlevel\EventSourcing\Projection\Projector\InMemoryProjectorRepository; use Patchlevel\EventSourcing\Repository\DefaultRepositoryManager; use Patchlevel\EventSourcing\Schema\ChainSchemaConfigurator; @@ -118,7 +118,7 @@ public function testSync(): void $manager = new DefaultRepositoryManager( $aggregateRegistry, $store, - new ProjectionistEventBusWrapper( + new SyncProjectionistEventBusWrapper( new DefaultEventBus(), $projectionist, new LockFactory($lockStore), diff --git a/tests/Unit/Projection/Projectionist/RunProjectionistEventBusWrapperTest.php b/tests/Unit/Projection/Projectionist/SyncProjectionistEventBusWrapperTest.php similarity index 80% rename from tests/Unit/Projection/Projectionist/RunProjectionistEventBusWrapperTest.php rename to tests/Unit/Projection/Projectionist/SyncProjectionistEventBusWrapperTest.php index 2e2879056..99d3224ad 100644 --- a/tests/Unit/Projection/Projectionist/RunProjectionistEventBusWrapperTest.php +++ b/tests/Unit/Projection/Projectionist/SyncProjectionistEventBusWrapperTest.php @@ -7,14 +7,14 @@ use Patchlevel\EventSourcing\EventBus\EventBus; use Patchlevel\EventSourcing\EventBus\Message; use Patchlevel\EventSourcing\Projection\Projectionist\Projectionist; -use Patchlevel\EventSourcing\Projection\Projectionist\ProjectionistEventBusWrapper; +use Patchlevel\EventSourcing\Projection\Projectionist\SyncProjectionistEventBusWrapper; use Patchlevel\EventSourcing\Tests\Unit\Fixture\ProfileId; use Patchlevel\EventSourcing\Tests\Unit\Fixture\ProfileVisited; use PHPUnit\Framework\TestCase; use Prophecy\PhpUnit\ProphecyTrait; -/** @covers \Patchlevel\EventSourcing\Projection\Projectionist\ProjectionistEventBusWrapper */ -final class RunProjectionistEventBusWrapperTest extends TestCase +/** @covers \Patchlevel\EventSourcing\Projection\Projectionist\SyncProjectionistEventBusWrapper */ +final class SyncProjectionistEventBusWrapperTest extends TestCase { use ProphecyTrait; @@ -35,7 +35,7 @@ public function testDispatch(): void $projectionist->run()->shouldBeCalledOnce(); $projectionist->reveal(); - $eventBus = new ProjectionistEventBusWrapper( + $eventBus = new SyncProjectionistEventBusWrapper( $parentEventBus->reveal(), $projectionist->reveal(), ); From 453d0c1aa8bf837e76ec134dc947c537b82bb884 Mon Sep 17 00:00:00 2001 From: David Badura Date: Mon, 7 Aug 2023 09:32:38 +0200 Subject: [PATCH 6/7] add static constructor --- .../SyncProjectionistEventBusWrapper.php | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/src/Projection/Projectionist/SyncProjectionistEventBusWrapper.php b/src/Projection/Projectionist/SyncProjectionistEventBusWrapper.php index bea2ef476..ee1909699 100644 --- a/src/Projection/Projectionist/SyncProjectionistEventBusWrapper.php +++ b/src/Projection/Projectionist/SyncProjectionistEventBusWrapper.php @@ -7,14 +7,16 @@ use Patchlevel\EventSourcing\EventBus\EventBus; use Patchlevel\EventSourcing\EventBus\Message; use Symfony\Component\Lock\LockFactory; +use Symfony\Component\Lock\Store\FlockStore; final class SyncProjectionistEventBusWrapper implements EventBus { public function __construct( - private readonly EventBus $parentEventBus, + private readonly EventBus $parentEventBus, private readonly Projectionist $projectionist, - private readonly LockFactory $lockFactory, - ) { + private readonly LockFactory $lockFactory, + ) + { } public function dispatch(Message ...$messages): void @@ -33,4 +35,15 @@ public function dispatch(Message ...$messages): void $lock->release(); } } + + public static function createWithDefaultLockStrategy(EventBus $parentEventBus, Projectionist $projectionist): self + { + return new self( + $parentEventBus, + $projectionist, + new LockFactory( + new FlockStore() + ) + ); + } } From ac8910ac09ac548140a2873bfe6d132c519b587e Mon Sep 17 00:00:00 2001 From: David Badura Date: Thu, 10 Aug 2023 15:36:51 +0200 Subject: [PATCH 7/7] fix cs --- .../SyncProjectionistEventBusWrapper.php | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/Projection/Projectionist/SyncProjectionistEventBusWrapper.php b/src/Projection/Projectionist/SyncProjectionistEventBusWrapper.php index ee1909699..1f89db4fe 100644 --- a/src/Projection/Projectionist/SyncProjectionistEventBusWrapper.php +++ b/src/Projection/Projectionist/SyncProjectionistEventBusWrapper.php @@ -12,11 +12,10 @@ final class SyncProjectionistEventBusWrapper implements EventBus { public function __construct( - private readonly EventBus $parentEventBus, + private readonly EventBus $parentEventBus, private readonly Projectionist $projectionist, - private readonly LockFactory $lockFactory, - ) - { + private readonly LockFactory $lockFactory, + ) { } public function dispatch(Message ...$messages): void @@ -42,8 +41,8 @@ public static function createWithDefaultLockStrategy(EventBus $parentEventBus, P $parentEventBus, $projectionist, new LockFactory( - new FlockStore() - ) + new FlockStore(), + ), ); } }