Skip to content

Commit bb385e5

Browse files
committed
fixes
1 parent bab3253 commit bb385e5

File tree

2 files changed

+45
-5
lines changed

2 files changed

+45
-5
lines changed

packages/Ecotone/src/Test/StaticPsrClock.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,22 @@ final class StaticPsrClock implements ClockInterface, SleepInterface
1616
{
1717
private ?DateTimeImmutable $frozenTime = null;
1818
private bool $hasBeenChanged = false;
19+
private Duration $sleepDuration;
1920

20-
public function __construct(?string $now = null)
21+
public function __construct(private ?string $now = null)
2122
{
22-
if ($now !== null) {
23-
$this->frozenTime = new DateTimeImmutable($now);
24-
}
23+
$this->sleepDuration = Duration::zero();
2524
}
2625

2726
public function now(): DateTimeImmutable
2827
{
29-
return $this->frozenTime ?? new DateTimeImmutable();
28+
if ($this->frozenTime !== null) {
29+
return $this->frozenTime;
30+
}
31+
32+
$now = $this->now === null ? new DateTimeImmutable() : new DateTimeImmutable($this->now);
33+
34+
return $now->modify("+{$this->sleepDuration->zeroIfNegative()->inMicroseconds()} microseconds");
3035
}
3136

3237
public function sleep(Duration $duration): void

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,17 @@ public function test_time_advances_before_change_time_is_called(): void
175175
$this->assertGreaterThan($time1, $time2);
176176
}
177177

178+
public function test_time_advances_when_constructed_with_now_string(): void
179+
{
180+
$clock = new StaticPsrClock('now');
181+
182+
$time1 = $clock->now();
183+
usleep(1000);
184+
$time2 = $clock->now();
185+
186+
$this->assertGreaterThan($time1, $time2);
187+
}
188+
178189
public function test_time_freezes_after_change_time_with_duration(): void
179190
{
180191
$ecotoneTestSupport = EcotoneLite::bootstrapFlowTesting(
@@ -194,4 +205,28 @@ public function test_time_freezes_after_change_time_with_duration(): void
194205

195206
$this->assertEquals($time1, $time2);
196207
}
208+
209+
public function test_time_advances_when_constructed_with_null(): void
210+
{
211+
$clock = new StaticPsrClock(null);
212+
213+
$time1 = $clock->now();
214+
usleep(1000);
215+
$time2 = $clock->now();
216+
217+
$this->assertGreaterThan($time1, $time2);
218+
}
219+
220+
public function test_time_freezes_after_sleep_is_called(): void
221+
{
222+
$clock = new StaticPsrClock('now');
223+
224+
$clock->sleep(Duration::seconds(1));
225+
226+
$time1 = $clock->now();
227+
usleep(1000);
228+
$time2 = $clock->now();
229+
230+
$this->assertEquals($time1, $time2);
231+
}
197232
}

0 commit comments

Comments
 (0)