Skip to content

Commit

Permalink
fixed: FakeClock value changes must be atomic
Browse files Browse the repository at this point in the history
  • Loading branch information
martinzima committed Jun 24, 2024
1 parent d86cc06 commit 2853441
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
1 change: 1 addition & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### Fixed
- fixed LambdaCommandBusExtensions return values
- fixed: FakeClock value changes must be atomic

### Changed

Expand Down
26 changes: 19 additions & 7 deletions Revo.Testing/Core/FakeClock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ namespace Revo.Testing.Core
{
public static class FakeClock
{
private static readonly AsyncLocal<FakeClockImpl> ClockLocal = new AsyncLocal<FakeClockImpl>();
private static readonly object SetupLock = new object();
private static readonly AsyncLocal<FakeClockImpl> ClockLocal = new();
private static readonly object SetupLock = new();

public static DateTimeOffset Now
{
get { return Clock.Now; }
set { Clock.Now = value; }
get => Clock.Now;
set => Clock.Now = value;
}

private static FakeClockImpl Clock
Expand All @@ -30,21 +30,33 @@ private static FakeClockImpl Clock

public static void Setup()
{
Now = DateTimeOffset.Now;

lock (SetupLock)
{
if (!(Revo.Core.Core.Clock.Current is FakeClockImpl))
{
Revo.Core.Core.Clock.SetClock(() => Clock);
}
}

Now = DateTimeOffset.Now;
}

public class FakeClockImpl : IClock
{
public DateTimeOffset Now { get; set; }
private DateTimeOffsetBoxed boxed = new(DateTimeOffset.Now);

public DateTimeOffset Now
{
get => boxed.Value;
set => boxed = new DateTimeOffsetBoxed(value);
}

public DateTimeOffset UtcNow => Now.ToUniversalTime();
}

private class DateTimeOffsetBoxed(DateTimeOffset value)
{
public DateTimeOffset Value { get; } = value;
}
}
}

0 comments on commit 2853441

Please sign in to comment.