Enhance world simulation and API functionality; implement clock management improvements, ensure proper handling of world states during updates, and refine data persistence methods to prevent resurrecting deleted worlds.
This commit is contained in:
@@ -71,7 +71,31 @@ public sealed class GameTimeTests
|
||||
[Fact]
|
||||
public void ResolveStart_rejects_out_of_range_years()
|
||||
{
|
||||
// Year 10000 cannot even be constructed, so this has to sit just past the accepted range to
|
||||
// actually exercise ResolveStart.
|
||||
Assert.Throws<ArgumentOutOfRangeException>(() =>
|
||||
GameTime.ResolveStart(new DateTime(10000, 1, 1)));
|
||||
GameTime.ResolveStart(new DateTime(9999, 1, 1, 0, 0, 0, DateTimeKind.Unspecified)));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Advance_saturates_instead_of_running_off_the_end_of_the_calendar()
|
||||
{
|
||||
var nearTheEnd = new DateTime(9998, 12, 31, 23, 0, 0, DateTimeKind.Unspecified);
|
||||
|
||||
// A day of real time at x4 is roughly 16 game years - far past DateTime.MaxValue from here.
|
||||
var next = GameTime.Advance(nearTheEnd, TimeSpan.FromDays(1), timeScale: 4, paused: false);
|
||||
|
||||
Assert.Equal(DateTime.MaxValue, next);
|
||||
// Already saturated: the next tick must stay put rather than throw.
|
||||
Assert.Equal(DateTime.MaxValue, GameTime.Advance(next, TimeSpan.FromSeconds(1), 1, paused: false));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Advance_survives_an_absurd_wall_clock_gap()
|
||||
{
|
||||
// The naive product realElapsed.Ticks * 1200 overflows a long well before TimeSpan.MaxValue.
|
||||
var next = GameTime.Advance(GameTime.DefaultStart, TimeSpan.MaxValue, timeScale: 4, paused: false);
|
||||
|
||||
Assert.Equal(DateTime.MaxValue, next);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,51 +22,69 @@ public sealed class WorldSimulationHostTests : IDisposable
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Attach_hydrates_legacy_worlds_without_catch_up_from_created_at()
|
||||
public async Task Startup_seeds_legacy_worlds_without_catching_up_from_created_at()
|
||||
{
|
||||
var legacy = new WorldSummaryDto
|
||||
var legacy = Summary("legacy-11111111", "Legacy") with
|
||||
{
|
||||
Id = "legacy-11111111",
|
||||
Name = "Legacy",
|
||||
Latitude = 31.9,
|
||||
Longitude = -100.5,
|
||||
SizeMeters = 10_000,
|
||||
Status = WorldStatus.Ready,
|
||||
CreatedAt = DateTimeOffset.UtcNow - TimeSpan.FromDays(30),
|
||||
Clock = null,
|
||||
LastTickedAt = null,
|
||||
};
|
||||
await _store.SaveSummaryAsync(legacy);
|
||||
|
||||
// Mimic host startup hydration for worlds that lack a clock.
|
||||
var seeded = legacy with
|
||||
await _host.StartAsync(CancellationToken.None);
|
||||
try
|
||||
{
|
||||
Clock = WorldSimulation.DefaultClock(),
|
||||
LastTickedAt = DateTimeOffset.UtcNow,
|
||||
};
|
||||
await _store.SaveSummaryAsync(seeded);
|
||||
_host.Attach(seeded with { LastTickedAt = null }); // Attach without prior tick → no catch-up
|
||||
var clock = await WaitForClockAsync(legacy.Id);
|
||||
|
||||
// Force attach path used for legacy: Create with catchUp false via AttachSeeded behaviour —
|
||||
// Attach with LastTickedAt null means catchUp is false.
|
||||
var clock = _host.TryGetClock(legacy.Id);
|
||||
// Thirty days of catch-up would be centuries of game time; the seed must start at the default
|
||||
// morning and only drift by the handful of real seconds this test takes.
|
||||
Assert.InRange(clock.GameTime, GameTime.DefaultStart, GameTime.DefaultStart.AddHours(1));
|
||||
|
||||
var stored = await _store.GetSummaryAsync(legacy.Id);
|
||||
Assert.NotNull(stored?.Clock);
|
||||
Assert.NotNull(stored.LastTickedAt);
|
||||
}
|
||||
finally
|
||||
{
|
||||
await _host.StopAsync(CancellationToken.None);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Concurrent_attach_registers_exactly_one_simulation()
|
||||
{
|
||||
var summary = Summary("racy-44444444", "Racy");
|
||||
|
||||
// Every loser of the GetOrAdd race owns an Arch world that must be disposed, not dropped.
|
||||
Parallel.For(0, 32, _ => _host.Attach(summary));
|
||||
|
||||
Assert.True(_host.IsAttached(summary.Id));
|
||||
|
||||
var clock = _host.TryGetClock(summary.Id);
|
||||
Assert.NotNull(clock);
|
||||
Assert.Equal(GameTime.DefaultStart, clock.GameTime);
|
||||
Assert.InRange(clock.GameTime, GameTime.DefaultStart, GameTime.DefaultStart.AddMinutes(5));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Attach_keeps_the_running_clock_when_called_again_with_a_stale_summary()
|
||||
{
|
||||
var summary = Summary("stale-55555555", "Stale");
|
||||
_host.Attach(summary);
|
||||
_host.UpdateClock(summary.Id, new UpdateClockRequest { TimeScale = 4, Paused = true });
|
||||
|
||||
_host.Attach(summary);
|
||||
|
||||
var clock = _host.TryGetClock(summary.Id);
|
||||
Assert.NotNull(clock);
|
||||
Assert.Equal(4, clock.TimeScale);
|
||||
Assert.True(clock.Paused);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task UpdateClock_changes_pause_and_Detach_stops_simulation()
|
||||
{
|
||||
var summary = new WorldSummaryDto
|
||||
{
|
||||
Id = "ready-22222222",
|
||||
Name = "Ready",
|
||||
Latitude = 31.9,
|
||||
Longitude = -100.5,
|
||||
SizeMeters = 10_000,
|
||||
Status = WorldStatus.Ready,
|
||||
CreatedAt = DateTimeOffset.UtcNow,
|
||||
Clock = WorldSimulation.DefaultClock(),
|
||||
LastTickedAt = DateTimeOffset.UtcNow,
|
||||
};
|
||||
var summary = Summary("ready-22222222", "Ready");
|
||||
await _store.SaveSummaryAsync(summary);
|
||||
_host.Attach(summary);
|
||||
|
||||
@@ -85,18 +103,7 @@ public sealed class WorldSimulationHostTests : IDisposable
|
||||
[Fact]
|
||||
public void Overlay_strips_last_ticked_at_for_api()
|
||||
{
|
||||
var summary = new WorldSummaryDto
|
||||
{
|
||||
Id = "ready-33333333",
|
||||
Name = "Ready",
|
||||
Latitude = 31.9,
|
||||
Longitude = -100.5,
|
||||
SizeMeters = 10_000,
|
||||
Status = WorldStatus.Ready,
|
||||
CreatedAt = DateTimeOffset.UtcNow,
|
||||
Clock = WorldSimulation.DefaultClock(),
|
||||
LastTickedAt = DateTimeOffset.UtcNow,
|
||||
};
|
||||
var summary = Summary("ready-33333333", "Ready");
|
||||
_host.Attach(summary);
|
||||
|
||||
var overlaid = _host.Overlay(summary);
|
||||
@@ -104,9 +111,74 @@ public sealed class WorldSimulationHostTests : IDisposable
|
||||
Assert.Null(overlaid.LastTickedAt);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Overlay_strips_last_ticked_at_for_worlds_that_are_not_running()
|
||||
{
|
||||
var overlaid = _host.Overlay(Summary("pending-66666666", "Pending") with
|
||||
{
|
||||
Status = WorldStatus.Pending,
|
||||
});
|
||||
|
||||
Assert.Null(overlaid.LastTickedAt);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The tick loop must not recreate the folder of a world deleted underneath it - that would leave a
|
||||
/// state-only husk that lists as Ready but has no map behind it.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public async Task Persisting_a_deleted_world_does_not_resurrect_it()
|
||||
{
|
||||
var summary = Summary("doomed-77777777", "Doomed");
|
||||
await _store.SaveSummaryAsync(summary);
|
||||
_host.Attach(summary);
|
||||
|
||||
// Dirty the clock, then delete the world the way the endpoint does not: storage only, still attached.
|
||||
_host.UpdateClock(summary.Id, new UpdateClockRequest { TimeScale = 2 });
|
||||
Assert.True(_store.Delete(summary.Id));
|
||||
|
||||
await _host.StartAsync(CancellationToken.None);
|
||||
await _host.StopAsync(CancellationToken.None);
|
||||
|
||||
Assert.Null(await _store.GetSummaryAsync(summary.Id));
|
||||
Assert.False(Directory.Exists(Path.Combine(_root, summary.Id)));
|
||||
Assert.False(_host.IsAttached(summary.Id));
|
||||
}
|
||||
|
||||
private async Task<WorldClockDto> WaitForClockAsync(string id)
|
||||
{
|
||||
for (var attempt = 0; attempt < 100; attempt++)
|
||||
{
|
||||
if (_host.TryGetClock(id) is { } clock) return clock;
|
||||
await Task.Delay(50);
|
||||
}
|
||||
|
||||
throw new TimeoutException($"Simulation for world '{id}' never attached.");
|
||||
}
|
||||
|
||||
private static WorldSummaryDto Summary(string id, string name) => new()
|
||||
{
|
||||
Id = id,
|
||||
Name = name,
|
||||
Latitude = 31.9,
|
||||
Longitude = -100.5,
|
||||
SizeMeters = 10_000,
|
||||
Status = WorldStatus.Ready,
|
||||
CreatedAt = DateTimeOffset.UtcNow,
|
||||
Clock = WorldSimulation.DefaultClock(),
|
||||
LastTickedAt = DateTimeOffset.UtcNow,
|
||||
};
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
foreach (var id in new[] { "legacy-11111111", "ready-22222222", "ready-33333333" })
|
||||
// Detach is a no-op for ids this test never attached, so one list covers every case.
|
||||
string[] ids =
|
||||
[
|
||||
"legacy-11111111", "ready-22222222", "ready-33333333",
|
||||
"racy-44444444", "stale-55555555", "pending-66666666", "doomed-77777777",
|
||||
];
|
||||
|
||||
foreach (var id in ids)
|
||||
_host.Detach(id);
|
||||
|
||||
if (Directory.Exists(_root)) Directory.Delete(_root, recursive: true);
|
||||
|
||||
@@ -80,6 +80,25 @@ public sealed class WorldSimulationTests
|
||||
GameTime.DefaultStart.AddMinutes(17));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Catch_up_skips_a_world_that_was_paused_when_the_host_went_down()
|
||||
{
|
||||
var summary = ReadySummary() with
|
||||
{
|
||||
Clock = WorldSimulation.DefaultClock() with { Paused = true },
|
||||
LastTickedAt = DateTimeOffset.UtcNow - TimeSpan.FromHours(6),
|
||||
};
|
||||
|
||||
using var simulation = WorldSimulation.Create(summary, catchUp: true);
|
||||
|
||||
Assert.Equal(GameTime.DefaultStart, simulation.SnapshotClock().GameTime);
|
||||
|
||||
// Unpausing must not replay the six hours spent paused offline either.
|
||||
var resumed = simulation.Update(new UpdateClockRequest { Paused = false });
|
||||
Assert.False(resumed.Paused);
|
||||
Assert.InRange(resumed.GameTime, GameTime.DefaultStart, GameTime.DefaultStart.AddMinutes(5));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void OverlayForApi_strips_last_ticked_at()
|
||||
{
|
||||
|
||||
@@ -47,6 +47,39 @@ public sealed class WorldStoreTests : IDisposable
|
||||
Assert.Equal(1, await _store.CountAsync());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task TryUpdateSummaryAsync_writes_over_an_existing_state_file()
|
||||
{
|
||||
var summary = Summary("alpha-11111111", "Alpha");
|
||||
await _store.SaveSummaryAsync(summary);
|
||||
|
||||
Assert.True(await _store.TryUpdateSummaryAsync(summary with { Name = "Renamed" }));
|
||||
Assert.Equal("Renamed", (await _store.GetSummaryAsync(summary.Id))?.Name);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task TryUpdateSummaryAsync_refuses_to_recreate_a_deleted_world()
|
||||
{
|
||||
var summary = Summary("alpha-11111111", "Alpha");
|
||||
await _store.SaveSummaryAsync(summary);
|
||||
Assert.True(_store.Delete(summary.Id));
|
||||
|
||||
Assert.False(await _store.TryUpdateSummaryAsync(summary));
|
||||
Assert.False(Directory.Exists(Path.Combine(_root, summary.Id)));
|
||||
Assert.Equal(0, await _store.CountAsync());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task SaveSummaryAsync_leaves_no_scratch_files_behind()
|
||||
{
|
||||
var summary = Summary("alpha-11111111", "Alpha");
|
||||
await _store.SaveSummaryAsync(summary);
|
||||
await _store.SaveSummaryAsync(summary with { Name = "Alpha again" });
|
||||
|
||||
var files = Directory.GetFiles(Path.Combine(_root, summary.Id));
|
||||
Assert.Equal(["state.json"], files.Select(static path => Path.GetFileName(path)).Order().ToArray()!);
|
||||
}
|
||||
|
||||
private static WorldSummaryDto Summary(string id, string name) => new()
|
||||
{
|
||||
Id = id,
|
||||
|
||||
Reference in New Issue
Block a user