-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
remove headers once they are propagated (#134)
* add test scenario using Ecotone Lite * remove the headers once they are propagated in case of asynchronous projection when first event has a header which does not exist in the next one, header will remain * Add header propagation test for multiple events * add asynchronous passing test * add asynchronous passing test * Add new tests and solution to protect from propagated headers * removing already propagated headers works for async handlers. need to find a way to keep them when message is handled synchronously * Stream based source * enable testing async scenarios --------- Co-authored-by: Dariusz Gafka <dariuszgafka@gmail.com> Co-authored-by: Dariusz Gafka <dgafka.mail@gmail.com>
- Loading branch information
1 parent
e100ba0
commit 91096ef
Showing
17 changed files
with
533 additions
and
20 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
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
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
69 changes: 69 additions & 0 deletions
69
...otone/tests/Modelling/Fixture/MetadataPropagatingWithDoubleEventHandlers/OrderService.php
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,69 @@ | ||
<?php | ||
|
||
namespace Test\Ecotone\Modelling\Fixture\MetadataPropagatingWithDoubleEventHandlers; | ||
|
||
use Ecotone\Messaging\Attribute\Asynchronous; | ||
use Ecotone\Messaging\Conversion\MediaType; | ||
use Ecotone\Modelling\Attribute\CommandHandler; | ||
use Ecotone\Modelling\Attribute\EventHandler; | ||
use Ecotone\Modelling\Attribute\QueryHandler; | ||
use Ecotone\Modelling\CommandBus; | ||
use Ecotone\Modelling\EventBus; | ||
use InvalidArgumentException; | ||
|
||
class OrderService | ||
{ | ||
private array $notificationHeaders = []; | ||
|
||
private array $notifyWithCustomHeaders = []; | ||
|
||
#[CommandHandler('placeOrder')] | ||
public function doSomething($command, array $headers, EventBus $eventBus): void | ||
{ | ||
$eventBus->publish(new OrderWasPlaced()); | ||
} | ||
|
||
#[CommandHandler('failAction')] | ||
public function failAction(): void | ||
{ | ||
throw new InvalidArgumentException('failed action'); | ||
} | ||
|
||
#[Asynchronous("orders")] | ||
#[EventHandler(endpointId: 'notifyOne')] | ||
public function notifyOne(OrderWasPlaced $event, array $headers, CommandBus $commandBus): void | ||
{ | ||
$commandBus->sendWithRouting('sendNotification', [], MediaType::APPLICATION_X_PHP_ARRAY, $this->notifyWithCustomHeaders); | ||
} | ||
|
||
#[Asynchronous('orders')] | ||
#[EventHandler(endpointId: 'notifyTwo')] | ||
public function notifyTwo(OrderWasPlaced $event, array $headers, CommandBus $commandBus): void | ||
{ | ||
$commandBus->sendWithRouting('sendNotification', [], MediaType::APPLICATION_X_PHP_ARRAY, $this->notifyWithCustomHeaders); | ||
} | ||
|
||
#[CommandHandler('setCustomNotificationHeaders')] | ||
public function notifyWithCustomerHeaders(array $payload, array $headers): void | ||
{ | ||
$this->notifyWithCustomHeaders = $headers; | ||
} | ||
|
||
#[CommandHandler('sendNotification')] | ||
public function sendNotification($command, array $headers): void | ||
{ | ||
$this->notificationHeaders[] = $headers; | ||
} | ||
|
||
#[QueryHandler('getNotificationHeaders')] | ||
public function getNotificationHeaders(): array | ||
{ | ||
return \end($this->notificationHeaders); | ||
} | ||
|
||
#[QueryHandler('getAllNotificationHeaders')] | ||
public function getAllNotificationHeaders(): array | ||
{ | ||
return $this->notificationHeaders; | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
...one/tests/Modelling/Fixture/MetadataPropagatingWithDoubleEventHandlers/OrderWasPlaced.php
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,7 @@ | ||
<?php | ||
|
||
namespace Test\Ecotone\Modelling\Fixture\MetadataPropagatingWithDoubleEventHandlers; | ||
|
||
class OrderWasPlaced | ||
{ | ||
} |
61 changes: 61 additions & 0 deletions
61
packages/Ecotone/tests/Modelling/Unit/MetadataPropagatingTest.php
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,61 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Test\Ecotone\Modelling\Unit; | ||
|
||
use Ecotone\Lite\EcotoneLite; | ||
use Ecotone\Messaging\Channel\SimpleMessageChannelBuilder; | ||
use Ecotone\Messaging\Config\ServiceConfiguration; | ||
use Ecotone\Messaging\Endpoint\ExecutionPollingMetadata; | ||
use PHPUnit\Framework\TestCase; | ||
use Test\Ecotone\Modelling\Fixture\MetadataPropagatingWithDoubleEventHandlers\OrderService; | ||
|
||
final class MetadataPropagatingTest extends TestCase | ||
{ | ||
public function test_propagating_headers_to_all_published_synchronous_event_handlers(): void | ||
{ | ||
$ecotoneTestSupport = EcotoneLite::bootstrapFlowTesting( | ||
classesToResolve: [OrderService::class], | ||
containerOrAvailableServices: [new OrderService()] | ||
); | ||
|
||
$ecotoneTestSupport->sendCommandWithRoutingKey( | ||
'placeOrder', | ||
metadata: [ | ||
'userId' => '123' | ||
] | ||
); | ||
|
||
$notifications = $ecotoneTestSupport->sendQueryWithRouting('getAllNotificationHeaders'); | ||
$this->assertCount(2, $notifications); | ||
$this->assertEquals('123', $notifications[0]['userId']); | ||
$this->assertEquals('123', $notifications[1]['userId']); | ||
} | ||
|
||
public function test_propagating_headers_to_all_published_asynchronous_event_handlers(): void | ||
{ | ||
$ecotoneTestSupport = EcotoneLite::bootstrapFlowTesting( | ||
classesToResolve: [OrderService::class], | ||
containerOrAvailableServices: [new OrderService()], | ||
configuration: ServiceConfiguration::createWithAsynchronicityOnly() | ||
->withExtensionObjects([ | ||
SimpleMessageChannelBuilder::createQueueChannel('orders') | ||
]) | ||
); | ||
|
||
$ecotoneTestSupport->sendCommandWithRoutingKey( | ||
'placeOrder', | ||
metadata: [ | ||
'userId' => '123' | ||
] | ||
); | ||
|
||
$ecotoneTestSupport->run('orders', ExecutionPollingMetadata::createWithTestingSetup(2)); | ||
$notifications = $ecotoneTestSupport->sendQueryWithRouting('getAllNotificationHeaders'); | ||
|
||
$this->assertCount(2, $notifications); | ||
$this->assertEquals('123', $notifications[0]['userId']); | ||
$this->assertEquals('123', $notifications[1]['userId']); | ||
} | ||
} |
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
38 changes: 38 additions & 0 deletions
38
...ventSourcing/tests/Fixture/MetadataPropagationWithAsyncProjection/NotificationService.php
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,38 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Test\Ecotone\EventSourcing\Fixture\MetadataPropagationWithAsyncProjection; | ||
|
||
use Ecotone\Messaging\Attribute\Asynchronous; | ||
use Ecotone\Modelling\Attribute\EventHandler; | ||
use Ecotone\Modelling\Attribute\QueryHandler; | ||
|
||
final class NotificationService | ||
{ | ||
private int $fooCounter = 0; | ||
|
||
#[Asynchronous(OrderProjection::CHANNEL)] | ||
#[EventHandler(endpointId: 'notification_service.order_created')] | ||
public function when(OrderCreated $event, array $metadata): void | ||
{ | ||
if (array_key_exists('foo', $metadata)) { | ||
$this->fooCounter++; | ||
} | ||
} | ||
|
||
#[Asynchronous(OrderProjection::CHANNEL)] | ||
#[EventHandler(endpointId: 'notification_service.another_order_created')] | ||
public function another(OrderCreated $event, array $metadata): void | ||
{ | ||
if (array_key_exists('foo', $metadata)) { | ||
$this->fooCounter++; | ||
} | ||
} | ||
|
||
#[QueryHandler("getNotificationCountWithFoo")] | ||
public function getNotificationCountWithFoo(): int | ||
{ | ||
return $this->fooCounter; | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
packages/PdoEventSourcing/tests/Fixture/MetadataPropagationWithAsyncProjection/Order.php
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,32 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Test\Ecotone\EventSourcing\Fixture\MetadataPropagationWithAsyncProjection; | ||
|
||
use Ecotone\Modelling\Attribute\AggregateIdentifier; | ||
use Ecotone\Modelling\Attribute\CommandHandler; | ||
use Ecotone\Modelling\Attribute\EventSourcingAggregate; | ||
use Ecotone\Modelling\Attribute\EventSourcingHandler; | ||
use Ecotone\Modelling\WithAggregateVersioning; | ||
|
||
#[EventSourcingAggregate] | ||
final class Order | ||
{ | ||
use WithAggregateVersioning; | ||
|
||
#[AggregateIdentifier] | ||
private int $id; | ||
|
||
#[CommandHandler(routingKey: 'order.create')] | ||
public static function create(int $id): array | ||
{ | ||
return [new OrderCreated($id), new ProductAddedToOrder($id)]; | ||
} | ||
|
||
#[EventSourcingHandler] | ||
public function applyOrderCreated(OrderCreated $event): void | ||
{ | ||
$this->id = $event->id; | ||
} | ||
} |
Oops, something went wrong.