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:
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user