Skip to content

Commit bab3253

Browse files
committed
fixes
1 parent 0607f9c commit bab3253

File tree

2 files changed

+39
-5
lines changed

2 files changed

+39
-5
lines changed

packages/Ecotone/src/Test/StaticPsrClock.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,19 @@
1414
*/
1515
final class StaticPsrClock implements ClockInterface, SleepInterface
1616
{
17-
private DateTimeImmutable $currentTime;
17+
private ?DateTimeImmutable $frozenTime = null;
1818
private bool $hasBeenChanged = false;
1919

2020
public function __construct(?string $now = null)
2121
{
22-
$this->currentTime = $now === null ? new DateTimeImmutable() : new DateTimeImmutable($now);
22+
if ($now !== null) {
23+
$this->frozenTime = new DateTimeImmutable($now);
24+
}
2325
}
2426

2527
public function now(): DateTimeImmutable
2628
{
27-
return $this->currentTime;
29+
return $this->frozenTime ?? new DateTimeImmutable();
2830
}
2931

3032
public function sleep(Duration $duration): void
@@ -33,7 +35,8 @@ public function sleep(Duration $duration): void
3335
return;
3436
}
3537

36-
$this->currentTime = $this->currentTime->modify("+{$duration->inMicroseconds()} microseconds");
38+
$this->frozenTime = $this->now()->modify("+{$duration->inMicroseconds()} microseconds");
39+
$this->hasBeenChanged = true;
3740
}
3841

3942
public function hasBeenChanged(): bool
@@ -43,7 +46,7 @@ public function hasBeenChanged(): bool
4346

4447
public function setCurrentTime(DateTimeImmutable $time): void
4548
{
46-
$this->currentTime = $time;
49+
$this->frozenTime = $time;
4750
$this->hasBeenChanged = true;
4851
}
4952
}

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,4 +163,35 @@ public function test_change_time_throws_exception_when_moving_backwards_after_fi
163163

164164
$ecotoneTestSupport->changeTime(new \DateTimeImmutable('2025-08-11 16:30:00'));
165165
}
166+
167+
public function test_time_advances_before_change_time_is_called(): void
168+
{
169+
$clock = new StaticPsrClock();
170+
171+
$time1 = $clock->now();
172+
usleep(1000);
173+
$time2 = $clock->now();
174+
175+
$this->assertGreaterThan($time1, $time2);
176+
}
177+
178+
public function test_time_freezes_after_change_time_with_duration(): void
179+
{
180+
$ecotoneTestSupport = EcotoneLite::bootstrapFlowTesting(
181+
[OrderService::class, NotificationService::class, CustomNotifier::class],
182+
[ClockInterface::class => new StaticPsrClock(), new OrderService(), new NotificationService(), new CustomNotifier()],
183+
enableAsynchronousProcessing: [
184+
SimpleMessageChannelBuilder::createQueueChannel('notifications', true),
185+
]
186+
);
187+
188+
$ecotoneTestSupport->changeTime(Duration::seconds(1));
189+
190+
$clock = $ecotoneTestSupport->getServiceFromContainer(ClockInterface::class);
191+
$time1 = $clock->now();
192+
usleep(1000);
193+
$time2 = $clock->now();
194+
195+
$this->assertEquals($time1, $time2);
196+
}
166197
}

0 commit comments

Comments
 (0)