ArchiveIndexer.SyncAsync and the ingest's in-step append (and the first frame of an API sequence) read the sequence row and upsert the whole row from that read in one transaction. On PostgreSQL (READ COMMITTED) a PATCH accepted between the read and the upsert was written back over: its settings were lost and settings_version went back, so the PC that pushed them later pulled the old settings over its edit. The row is now read with SequenceRepository.GetForUpdateAsync (FOR UPDATE, as EditAsync takes it). A PATCH committed first is then moved by the insertion like any position; one arriving later sees the new frames version and gets 409 frames_conflict. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
141 lines
7.4 KiB
C#
141 lines
7.4 KiB
C#
using Titano.Core.Diagnostics.Synthetic;
|
|
using Titano.Data.Archiving;
|
|
using Titano.Data.Processing;
|
|
using Titano.Data.Repositories;
|
|
using Titano.Data.Tests.Infrastructure;
|
|
using Titano.Data.Tests.Processing;
|
|
using Titano.Testing;
|
|
|
|
namespace Titano.Data.Tests.Repositories;
|
|
|
|
/// <summary>Client edits of a sequence row (<see cref="SequenceRepository.EditAsync"/>) on both dialects: the hero frame the Library shows.</summary>
|
|
public abstract class SequenceEditTests(TestContext context) : RepositoryTest(context)
|
|
{
|
|
[Test]
|
|
public async Task TheHeroFrameIsSetKeptAndCleared()
|
|
{
|
|
var db = await OpenTitanoAsync();
|
|
var archive = TestData.Archive();
|
|
await db.Archives.UpsertAsync(archive, Ct);
|
|
var sequence = TestData.Sequence(archive.Id);
|
|
await db.Sequences.UpsertAsync(sequence, Ct);
|
|
var frames = Enumerable.Range(0, 5).Select(i => TestData.Frame(sequence.Id, i)).ToList();
|
|
await db.Frames.BulkInsertAsync(frames, Ct);
|
|
|
|
var set = await db.Sequences.EditAsync(sequence.Id, new SequenceEdit { SettingsJson = "{\"heroFrame\":3}", HeroFrameId = frames[3].Id }, null, Ct);
|
|
Assert.Equal(SequenceEditOutcome.Updated, set.Outcome);
|
|
Assert.Equal(frames[3].Id, set.Sequence!.HeroFrameId);
|
|
Assert.Equal(frames[3].Id, (await db.Sequences.GetAsync(sequence.Id, Ct))!.HeroFrameId);
|
|
|
|
var kept = await db.Sequences.EditAsync(sequence.Id, new SequenceEdit { Rating = 4 }, set.Sequence.SettingsVersion, Ct);
|
|
Assert.Equal(frames[3].Id, kept.Sequence!.HeroFrameId, "an edit without a hero leaves it alone");
|
|
|
|
var cleared = await db.Sequences.EditAsync(sequence.Id, new SequenceEdit { HeroFrameId = Guid.Empty }, null, Ct);
|
|
Assert.Null(cleared.Sequence!.HeroFrameId, "Guid.Empty goes back to the first frame");
|
|
}
|
|
|
|
[Test]
|
|
public async Task AFrameOfAnotherSequenceIsNotAHero()
|
|
{
|
|
var db = await OpenTitanoAsync();
|
|
var archive = TestData.Archive();
|
|
await db.Archives.UpsertAsync(archive, Ct);
|
|
var mine = TestData.Sequence(archive.Id, title: "Mine");
|
|
var other = TestData.Sequence(archive.Id, title: "Other");
|
|
await db.Sequences.UpsertAsync(mine, Ct);
|
|
await db.Sequences.UpsertAsync(other, Ct);
|
|
var own = TestData.Frame(mine.Id, 0);
|
|
var foreign = TestData.Frame(other.Id, 0);
|
|
await db.Frames.BulkInsertAsync([own, foreign], Ct);
|
|
|
|
await db.Sequences.EditAsync(mine.Id, new SequenceEdit { HeroFrameId = own.Id }, null, Ct);
|
|
var result = await db.Sequences.EditAsync(mine.Id, new SequenceEdit { HeroFrameId = foreign.Id }, null, Ct);
|
|
Assert.Equal(SequenceEditOutcome.Updated, result.Outcome);
|
|
Assert.Equal(own.Id, result.Sequence!.HeroFrameId);
|
|
}
|
|
|
|
[Test]
|
|
public async Task AConflictLeavesTheHeroAsItWas()
|
|
{
|
|
var db = await OpenTitanoAsync();
|
|
var archive = TestData.Archive();
|
|
await db.Archives.UpsertAsync(archive, Ct);
|
|
var sequence = TestData.Sequence(archive.Id);
|
|
await db.Sequences.UpsertAsync(sequence, Ct);
|
|
var frame = TestData.Frame(sequence.Id, 0);
|
|
await db.Frames.BulkInsertAsync([frame], Ct);
|
|
|
|
var result = await db.Sequences.EditAsync(sequence.Id, new SequenceEdit { HeroFrameId = frame.Id }, sequence.SettingsVersion + 7, Ct);
|
|
Assert.Equal(SequenceEditOutcome.Conflict, result.Outcome);
|
|
Assert.Null((await db.Sequences.GetAsync(sequence.Id, Ct))!.HeroFrameId);
|
|
}
|
|
|
|
[Test]
|
|
public async Task AnEditWaitsForARowWrittenBackFromALockedRead()
|
|
{
|
|
var db = await OpenTitanoAsync();
|
|
var archive = TestData.Archive();
|
|
await db.Archives.UpsertAsync(archive, Ct);
|
|
var sequence = TestData.Sequence(archive.Id);
|
|
await db.Sequences.UpsertAsync(sequence, Ct);
|
|
|
|
// An index commit reads the row and writes it back with its frames; a settings edit (the server's PATCH) arrives meanwhile.
|
|
Task<SequenceEditResult> edit;
|
|
await using (var tx = await db.BeginAsync(Ct))
|
|
{
|
|
var read = Assert.NotNull(await tx.Sequences.GetForUpdateAsync(sequence.Id, Ct));
|
|
edit = db.Sequences.EditAsync(sequence.Id, new SequenceEdit { SettingsJson = """{"excludedFrames":[1]}""" }, read.SettingsVersion, read.FramesVersion, Ct);
|
|
await Task.WhenAny(edit, Task.Delay(300, Ct));
|
|
Assert.False(edit.IsCompleted, "the edit waits for the row");
|
|
await tx.Sequences.UpsertAsync(read with { FramesVersion = read.FramesVersion + 1 }, Ct);
|
|
await tx.CommitAsync(Ct);
|
|
}
|
|
|
|
// It then finds the frames changed: written against the old ones, it is refused rather than undone.
|
|
var result = await edit;
|
|
Assert.Equal(SequenceEditOutcome.Conflict, result.Outcome);
|
|
var stored = (await db.Sequences.GetAsync(sequence.Id, Ct))!;
|
|
Assert.Equal((sequence.SettingsVersion, sequence.FramesVersion + 1), (stored.SettingsVersion, stored.FramesVersion));
|
|
|
|
// Without frames in question, it applies on top of the committed row.
|
|
var later = await db.Sequences.EditAsync(sequence.Id, new SequenceEdit { Rating = 3 }, stored.SettingsVersion, Ct);
|
|
Assert.Equal((sequence.SettingsVersion + 1, sequence.FramesVersion + 1, 3), (later.Sequence!.SettingsVersion, later.Sequence.FramesVersion, later.Sequence.Rating));
|
|
}
|
|
}
|
|
|
|
public sealed class SqliteSequenceEditTests(TestContext context) : SequenceEditTests(context)
|
|
{
|
|
protected override Backend Backend => Backend.SqliteMemory;
|
|
}
|
|
|
|
[RequiresEnvironment(PgTestServer.Variable)]
|
|
public sealed class PostgresSequenceEditTests(TestContext context) : SequenceEditTests(context)
|
|
{
|
|
protected override Backend Backend => Backend.Postgres;
|
|
}
|
|
|
|
/// <summary>Project writes of the engine (advice, sync) carry the project's hero frame to the index, so the Library shows it.</summary>
|
|
public sealed class ProjectStoreHeroTests(TestContext context)
|
|
{
|
|
[Test]
|
|
public async Task AdviceThatPicksAHeroReachesTheIndex()
|
|
{
|
|
await using var fixture = await ProcessingFixture.CreateAsync(context);
|
|
var sequence = await fixture.AddAsync(SyntheticSequence.Create(new SyntheticSequenceOptions { Width = 64, Height = 48, FrameCount = 6 }));
|
|
var frames = await fixture.Db.Frames.ListBySequenceAsync(sequence.Id, cancellationToken: fixture.Ct);
|
|
var store = new ProjectStore(fixture.Db, new SequenceWriter(new ArchiveServiceOptions { Owner = "test", LeaseWait = TimeSpan.FromSeconds(20) }));
|
|
|
|
Assert.True(await store.UpdateAsync(fixture.Archive.Paths, sequence, p => p with { HeroFrame = 4 }, fixture.Ct));
|
|
Assert.Equal(frames[4].Id, (await fixture.Db.Sequences.GetAsync(sequence.Id, fixture.Ct))!.HeroFrameId);
|
|
|
|
Assert.True(await store.UpdateAsync(fixture.Archive.Paths, sequence, p => p with { Exposure = p.Exposure with { ExposureOffsetEv = 0.5 } }, fixture.Ct));
|
|
Assert.Equal(frames[4].Id, (await fixture.Db.Sequences.GetAsync(sequence.Id, fixture.Ct))!.HeroFrameId, "other changes keep the hero");
|
|
|
|
Assert.True(await store.UpdateAsync(fixture.Archive.Paths, sequence, p => p with { HeroFrame = null }, fixture.Ct));
|
|
Assert.Null((await fixture.Db.Sequences.GetAsync(sequence.Id, fixture.Ct))!.HeroFrameId);
|
|
|
|
Assert.True(await store.UpdateAsync(fixture.Archive.Paths, sequence, p => p with { HeroFrame = 99 }, fixture.Ct));
|
|
Assert.Null((await fixture.Db.Sequences.GetAsync(sequence.Id, fixture.Ct))!.HeroFrameId, "an index past the last frame shows the first");
|
|
}
|
|
}
|