-
-
Notifications
You must be signed in to change notification settings - Fork 21
feat(scheduling): Sync Static Global Clock with Clock Service (Quick Solution) #618
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jb-needhelp
wants to merge
4
commits into
ecotoneframework:main
Choose a base branch
from
jb-needhelp:feature/sync-global-clock
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
e83d776
Refactor `Clock` to introduce `resetGlobalClock` and improve default …
jb-needhelp 76cd333
Fix `StaticGlobalClockTest` setup and teardown to properly reset glob…
jb-needhelp f2be802
Introduce test fixtures for scheduling and enhance `Clock` handling.
jb-needhelp ca9e195
Merge branch 'main' into feature/sync-global-clock
dgafka File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
27 changes: 27 additions & 0 deletions
27
packages/Ecotone/tests/Messaging/Fixture/Scheduling/CustomNotifier.php
This file contains hidden or 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,27 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Test\Ecotone\Messaging\Fixture\Scheduling; | ||
|
|
||
| /* | ||
| * licence Apache-2.0 | ||
| */ | ||
| class CustomNotifier | ||
| { | ||
| private array $notifications = []; | ||
|
|
||
| public function notify(string $eventName, $value): void | ||
| { | ||
| if (!isset($this->notifications[$eventName])) { | ||
| $this->notifications[$eventName] = []; | ||
| } | ||
|
|
||
| $this->notifications[$eventName][] = $value; | ||
| } | ||
|
|
||
| public function getNotificationsOf(string $eventName): array | ||
| { | ||
| return $this->notifications[$eventName] ?? []; | ||
| } | ||
| } |
23 changes: 23 additions & 0 deletions
23
packages/Ecotone/tests/Messaging/Fixture/Scheduling/NotificationService.php
This file contains hidden or 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,23 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Test\Ecotone\Messaging\Fixture\Scheduling; | ||
|
|
||
| use Ecotone\Messaging\Attribute\Asynchronous; | ||
| use Ecotone\Messaging\Attribute\Endpoint\Delayed; | ||
| use Ecotone\Modelling\Attribute\EventHandler; | ||
|
|
||
| /** | ||
| * licence Apache-2.0 | ||
| */ | ||
| class NotificationService | ||
| { | ||
| #[Asynchronous('notifications')] | ||
| #[Delayed(1000 * 60)] // 60 seconds | ||
| #[EventHandler(endpointId: 'notifyOrderWasPlaced')] | ||
| public function notify(OrderWasPlaced $event, CustomNotifier $notifier): void | ||
| { | ||
| $notifier->notify('placedOrder', $event->orderId); | ||
| } | ||
| } |
20 changes: 20 additions & 0 deletions
20
packages/Ecotone/tests/Messaging/Fixture/Scheduling/OrderService.php
This file contains hidden or 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,20 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Test\Ecotone\Messaging\Fixture\Scheduling; | ||
|
|
||
| use Ecotone\Modelling\Attribute\CommandHandler; | ||
| use Ecotone\Modelling\EventBus; | ||
|
|
||
| /** | ||
| * licence Apache-2.0 | ||
| */ | ||
| final class OrderService | ||
| { | ||
| #[CommandHandler('order.register')] | ||
| public function handle(PlaceOrder $command, EventBus $eventBus): void | ||
| { | ||
| $eventBus->publish(new OrderWasPlaced($command->orderId)); | ||
| } | ||
| } |
15 changes: 15 additions & 0 deletions
15
packages/Ecotone/tests/Messaging/Fixture/Scheduling/OrderWasPlaced.php
This file contains hidden or 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,15 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Test\Ecotone\Messaging\Fixture\Scheduling; | ||
|
|
||
| /** | ||
| * licence Apache-2.0 | ||
| */ | ||
| final class OrderWasPlaced | ||
| { | ||
| public function __construct(public string $orderId) | ||
| { | ||
| } | ||
| } |
15 changes: 15 additions & 0 deletions
15
packages/Ecotone/tests/Messaging/Fixture/Scheduling/PlaceOrder.php
This file contains hidden or 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,15 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Test\Ecotone\Messaging\Fixture\Scheduling; | ||
|
|
||
| /** | ||
| * licence Apache-2.0 | ||
| */ | ||
| final class PlaceOrder | ||
| { | ||
| public function __construct(public string $orderId) | ||
| { | ||
| } | ||
| } |
33 changes: 33 additions & 0 deletions
33
packages/Ecotone/tests/Messaging/Fixture/Scheduling/StaticPsrClock.php
This file contains hidden or 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,33 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Test\Ecotone\Messaging\Fixture\Scheduling; | ||
|
|
||
| use DateTimeImmutable; | ||
| use Ecotone\Messaging\Scheduling\Duration; | ||
| use Ecotone\Messaging\Scheduling\SleepInterface; | ||
| use Psr\Clock\ClockInterface; | ||
|
|
||
| /** | ||
| * licence Apache-2.0 | ||
| */ | ||
| final class StaticPsrClock implements ClockInterface, SleepInterface | ||
| { | ||
| private static DateTimeImmutable $now; | ||
|
|
||
| public function __construct(string $now) | ||
| { | ||
| self::$now = new DateTimeImmutable($now); | ||
| } | ||
|
|
||
| public function now(): DateTimeImmutable | ||
| { | ||
| return self::$now; | ||
| } | ||
|
|
||
| public function sleep(Duration $duration): void | ||
| { | ||
| self::$now = self::$now->modify("+{$duration->zeroIfNegative()->inMicroseconds()} microseconds"); | ||
| } | ||
| } |
67 changes: 67 additions & 0 deletions
67
...s/Ecotone/tests/Messaging/Integration/Scheduling/DelayedMessageAgainstGlobalClockTest.php
This file contains hidden or 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,67 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Test\Ecotone\Messaging\Integration\Scheduling; | ||
|
|
||
| use Ecotone\Lite\EcotoneLite; | ||
| use Ecotone\Messaging\Channel\SimpleMessageChannelBuilder; | ||
| use Ecotone\Messaging\Scheduling\Clock; | ||
| use Ecotone\Messaging\Scheduling\Duration; | ||
| use Ecotone\Messaging\Scheduling\EcotoneClockInterface; | ||
| use PHPUnit\Framework\TestCase; | ||
| use Test\Ecotone\Messaging\Fixture\Scheduling\CustomNotifier; | ||
| use Test\Ecotone\Messaging\Fixture\Scheduling\NotificationService; | ||
| use Test\Ecotone\Messaging\Fixture\Scheduling\OrderService; | ||
| use Test\Ecotone\Messaging\Fixture\Scheduling\PlaceOrder; | ||
| use Test\Ecotone\Messaging\Fixture\Scheduling\StaticPsrClock; | ||
|
|
||
| /** | ||
| * Class StaticGlobalClockTest | ||
| * @package Test\Ecotone\Messaging\Unit\Scheduling | ||
| * @author JB Cagumbay <cagumbay.jb@gmail.com> | ||
| * | ||
| * @internal | ||
| */ | ||
| /** | ||
| * licence Apache-2.0 | ||
| * @internal | ||
| */ | ||
| class DelayedMessageAgainstGlobalClockTest extends TestCase | ||
| { | ||
| protected function setUp(): void | ||
| { | ||
| parent::tearDown(); | ||
| Clock::resetToNativeClock(); | ||
| } | ||
|
|
||
| protected function tearDown(): void | ||
| { | ||
| parent::tearDown(); | ||
| Clock::resetToNativeClock(); | ||
| } | ||
|
|
||
| public function test_delayed_message_observes_clock_changes() | ||
| { | ||
| $ecotoneTestSupport = EcotoneLite::bootstrapFlowTesting( | ||
| [EcotoneClockInterface::class, OrderService::class, NotificationService::class, CustomNotifier::class], | ||
| [$clock = new Clock(new StaticPsrClock('2025-08-11 16:00:00')), new OrderService(), new NotificationService(), $notifier = new CustomNotifier()], | ||
| enableAsynchronousProcessing: [ | ||
| // 1. Turn on Delayable In Memory Pollable Channel | ||
| SimpleMessageChannelBuilder::createQueueChannel('notifications', true) | ||
| ] | ||
| ); | ||
|
|
||
| $ecotoneTestSupport->sendCommandWithRoutingKey('order.register', new PlaceOrder('123')); | ||
|
|
||
| $clock->sleep(Duration::minutes(1)); | ||
|
|
||
| // 2. Releasing messages awaiting for 60 seconds | ||
| $ecotoneTestSupport->run('notifications', releaseAwaitingFor: Clock::get()->now()); | ||
|
|
||
| $this->assertEquals( | ||
| 1, | ||
| count($notifier->getNotificationsOf('placedOrder')) | ||
| ); | ||
| } | ||
| } | ||
This file contains hidden or 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
66 changes: 66 additions & 0 deletions
66
packages/Ecotone/tests/Messaging/Unit/Scheduling/StaticGlobalClockTest.php
This file contains hidden or 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,66 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Test\Ecotone\Messaging\Unit\Scheduling; | ||
|
|
||
| use Ecotone\Messaging\Scheduling\Clock; | ||
| use Ecotone\Messaging\Scheduling\DatePoint; | ||
| use Ecotone\Messaging\Scheduling\NativeClock; | ||
| use PHPUnit\Framework\TestCase; | ||
| use Test\Ecotone\Messaging\Fixture\Scheduling\StaticPsrClock; | ||
|
|
||
| /** | ||
| * Class StaticGlobalClockTest | ||
| * @package Test\Ecotone\Messaging\Unit\Scheduling | ||
| * @author JB Cagumbay <cagumbay.jb@gmail.com> | ||
| * | ||
| * @internal | ||
| */ | ||
| /** | ||
| * licence Apache-2.0 | ||
| * @internal | ||
| */ | ||
| class StaticGlobalClockTest extends TestCase | ||
| { | ||
| protected function setUp(): void | ||
| { | ||
| parent::tearDown(); | ||
| Clock::resetToNativeClock(); | ||
| } | ||
|
|
||
| protected function tearDown(): void | ||
| { | ||
| parent::tearDown(); | ||
| Clock::resetToNativeClock(); | ||
| } | ||
|
|
||
| public function test_when_clock_is_not_instantiated_returns_default_native_clock() | ||
| { | ||
| $now = new DatePoint('now'); | ||
| $globalClock = Clock::get(); | ||
|
|
||
| $this->assertInstanceOf(NativeClock::class, $globalClock); | ||
| $this->assertEquals($now->format('Y-m-d H:i:s'), $globalClock->now()->format('Y-m-d H:i:s')); | ||
| } | ||
|
|
||
| public function test_when_clock_is_not_instantiated_with_null_internal_clock_returns_default_native_clock() | ||
| { | ||
| $now = new DatePoint('now'); | ||
| $clock = new Clock(); | ||
| $globalClock = Clock::get(); | ||
|
|
||
| $this->assertInstanceOf(NativeClock::class, $globalClock); | ||
| $this->assertEquals($now->format('Y-m-d H:i:s'), $globalClock->now()->format('Y-m-d H:i:s')); | ||
| } | ||
|
|
||
| public function test_when_clock_is_not_instantiated_with_not_null_internal_clock_returns_ecotone_clock() | ||
| { | ||
| $clock = new Clock(new StaticPsrClock('2025-08-11 16:00:00')); | ||
|
|
||
| $globalClock = Clock::get(); | ||
|
|
||
| $this->assertInstanceOf(Clock::class, $globalClock); | ||
| $this->assertEquals('2025-08-11 16:00:00', $globalClock->now()->format('Y-m-d H:i:s')); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@dgafka just out of curiosity, can you explain why instance of
EcotoneClockInterfaceon this test is reinstantiated? It's the reason why I added this if condition inClock::__constructor();