Skip to content

Commit 0607f9c

Browse files
committed
allow to change time using duration
1 parent 915da56 commit 0607f9c

File tree

3 files changed

+48
-15
lines changed

3 files changed

+48
-15
lines changed

packages/Ecotone/src/Lite/Test/ConfiguredMessagingSystemWithTestSupport.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Ecotone\Messaging\MessageChannel;
1414
use Ecotone\Messaging\MessageHeaders;
1515
use Ecotone\Messaging\MessagePublisher;
16+
use Ecotone\Messaging\Scheduling\Duration;
1617
use Ecotone\Messaging\Scheduling\EcotoneClockInterface;
1718
use Ecotone\Modelling\AggregateFlow\SaveAggregate\AggregateResolver\AggregateDefinitionRegistry;
1819
use Ecotone\Modelling\CommandBus;
@@ -134,21 +135,26 @@ public function replaceWith(ConfiguredMessagingSystem $messagingSystem): void
134135
$this->configuredMessagingSystem->replaceWith($messagingSystem);
135136
}
136137

137-
public function withCurrentTime(DateTimeImmutable $targetTime): self
138+
public function changeTime(DateTimeImmutable|Duration $time): self
138139
{
139140
$psrClock = $this->getStaticPsrClockFromContainer();
140141

141-
if ($psrClock->hasBeenChanged() && $targetTime <= $psrClock->now()) {
142+
if ($time instanceof Duration) {
143+
$psrClock->sleep($time);
144+
return $this;
145+
}
146+
147+
if ($psrClock->hasBeenChanged() && $time <= $psrClock->now()) {
142148
throw new InvalidArgumentException(
143149
sprintf(
144150
'Cannot move time backwards. Current clock time: %s, requested time: %s',
145151
$psrClock->now()->format('Y-m-d H:i:s.u'),
146-
$targetTime->format('Y-m-d H:i:s.u')
152+
$time->format('Y-m-d H:i:s.u')
147153
)
148154
);
149155
}
150156

151-
$psrClock->setCurrentTime($targetTime);
157+
$psrClock->setCurrentTime($time);
152158

153159
return $this;
154160
}

packages/Ecotone/src/Lite/Test/FlowTestSupport.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Ecotone\Messaging\MessagingException;
2020
use Ecotone\Messaging\PollableChannel;
2121
use Ecotone\Messaging\Scheduling\Clock;
22+
use Ecotone\Messaging\Scheduling\Duration;
2223
use Ecotone\Messaging\Scheduling\EcotoneClockInterface;
2324
use Ecotone\Messaging\Scheduling\TimeSpan;
2425
use Ecotone\Messaging\Support\Assert;
@@ -211,21 +212,26 @@ public function waitTill(TimeSpan|DateTimeInterface $time): self
211212
return $this;
212213
}
213214

214-
public function withCurrentTime(DateTimeImmutable $targetTime): self
215+
public function changeTime(DateTimeImmutable|Duration $time): self
215216
{
216217
$psrClock = $this->getStaticPsrClockFromContainer();
217218

218-
if ($psrClock->hasBeenChanged() && $targetTime <= $psrClock->now()) {
219+
if ($time instanceof Duration) {
220+
$psrClock->sleep($time);
221+
return $this;
222+
}
223+
224+
if ($psrClock->hasBeenChanged() && $time <= $psrClock->now()) {
219225
throw new InvalidArgumentException(
220226
\sprintf(
221227
'Cannot move time backwards. Current clock time: %s, requested time: %s',
222228
$psrClock->now()->format('Y-m-d H:i:s.u'),
223-
$targetTime->format('Y-m-d H:i:s.u')
229+
$time->format('Y-m-d H:i:s.u')
224230
)
225231
);
226232
}
227233

228-
$psrClock->setCurrentTime($targetTime);
234+
$psrClock->setCurrentTime($time);
229235

230236
return $this;
231237
}

packages/Ecotone/tests/Messaging/Integration/Scheduling/DelayedMessageAgainstGlobalClockTest.php

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public function test_delayed_message_observes_clock_changes_natively_by_moving_t
8989
);
9090
}
9191

92-
public function test_delayed_message_is_released_when_moving_time_forward_using_with_current_time(): void
92+
public function test_delayed_message_is_released_when_moving_time_forward_using_change_time(): void
9393
{
9494
$ecotoneTestSupport = EcotoneLite::bootstrapFlowTesting(
9595
[OrderService::class, NotificationService::class, CustomNotifier::class],
@@ -104,13 +104,34 @@ public function test_delayed_message_is_released_when_moving_time_forward_using_
104104
$ecotoneTestSupport->run('notifications');
105105
$this->assertCount(0, $notifier->getNotificationsOf('placedOrder'));
106106

107-
$ecotoneTestSupport->withCurrentTime(new \DateTimeImmutable('2025-08-11 16:01:01'));
107+
$ecotoneTestSupport->changeTime(new \DateTimeImmutable('2025-08-11 16:01:01'));
108108
$ecotoneTestSupport->run('notifications');
109109

110110
$this->assertCount(1, $notifier->getNotificationsOf('placedOrder'));
111111
}
112112

113-
public function test_first_with_current_time_call_allows_any_time(): void
113+
public function test_delayed_message_is_released_when_advancing_time_using_duration(): void
114+
{
115+
$ecotoneTestSupport = EcotoneLite::bootstrapFlowTesting(
116+
[OrderService::class, NotificationService::class, CustomNotifier::class],
117+
[ClockInterface::class => new StaticPsrClock('2025-08-11 16:00:00'), new OrderService(), new NotificationService(), $notifier = new CustomNotifier()],
118+
enableAsynchronousProcessing: [
119+
SimpleMessageChannelBuilder::createQueueChannel('notifications', true),
120+
]
121+
);
122+
123+
$ecotoneTestSupport->sendCommandWithRoutingKey('order.register', new PlaceOrder('123'));
124+
125+
$ecotoneTestSupport->run('notifications');
126+
$this->assertCount(0, $notifier->getNotificationsOf('placedOrder'));
127+
128+
$ecotoneTestSupport->changeTime(Duration::minutes(2));
129+
$ecotoneTestSupport->run('notifications');
130+
131+
$this->assertCount(1, $notifier->getNotificationsOf('placedOrder'));
132+
}
133+
134+
public function test_first_change_time_call_allows_any_time(): void
114135
{
115136
$ecotoneTestSupport = EcotoneLite::bootstrapFlowTesting(
116137
[OrderService::class, NotificationService::class, CustomNotifier::class],
@@ -120,12 +141,12 @@ public function test_first_with_current_time_call_allows_any_time(): void
120141
]
121142
);
122143

123-
$ecotoneTestSupport->withCurrentTime(new \DateTimeImmutable('2020-01-01 12:00:00'));
144+
$ecotoneTestSupport->changeTime(new \DateTimeImmutable('2020-01-01 12:00:00'));
124145

125146
$this->assertEquals('2020-01-01 12:00:00', $ecotoneTestSupport->getServiceFromContainer(ClockInterface::class)->now()->format('Y-m-d H:i:s'));
126147
}
127148

128-
public function test_with_current_time_throws_exception_when_moving_backwards_after_first_setup(): void
149+
public function test_change_time_throws_exception_when_moving_backwards_after_first_setup(): void
129150
{
130151
$ecotoneTestSupport = EcotoneLite::bootstrapFlowTesting(
131152
[OrderService::class, NotificationService::class, CustomNotifier::class],
@@ -135,11 +156,11 @@ public function test_with_current_time_throws_exception_when_moving_backwards_af
135156
]
136157
);
137158

138-
$ecotoneTestSupport->withCurrentTime(new \DateTimeImmutable('2025-08-11 17:00:00'));
159+
$ecotoneTestSupport->changeTime(new \DateTimeImmutable('2025-08-11 17:00:00'));
139160

140161
$this->expectException(\InvalidArgumentException::class);
141162
$this->expectExceptionMessage('Cannot move time backwards');
142163

143-
$ecotoneTestSupport->withCurrentTime(new \DateTimeImmutable('2025-08-11 16:30:00'));
164+
$ecotoneTestSupport->changeTime(new \DateTimeImmutable('2025-08-11 16:30:00'));
144165
}
145166
}

0 commit comments

Comments
 (0)