fix(settings): a read of the archive's settings that overlapped an edit waits for it and reads again
A read of the open archive's settings that an Archive row edit overlapped was dropped and left to the edit's end, so a folder change could complete while the rows still showed the previous archive. The read now waits until no edit is being written and reads again, so the retarget and the reload on showing Settings are done only once the rows show what the archive holds. A smoke test edits a row while Settings reads the archive, with the settings lease held elsewhere. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
4 files changed
+135
-71
No files matched your search
@@ -6,7 +6,8 @@ namespace Titano.App.Tests.Settings;
|
||||
/// <summary>
|
||||
/// Settings › Archive never shows a read of the archive's settings that overlapped an edit being written: on a slow share
|
||||
/// that read can hold archive.json as it was before the edit, and would undo the edit on the rows and in this PC's
|
||||
/// defaults while the archive keeps it.
|
||||
/// defaults while the archive keeps it. Its reader waits for the edits instead and reads again, so the read is done only
|
||||
/// once the rows show what the archive holds.
|
||||
/// </summary>
|
||||
public sealed class ArchiveReadGateTests
|
||||
{
|
||||
@@ -14,69 +15,79 @@ public sealed class ArchiveReadGateTests
|
||||
public void AReadWithNoEditMeanwhileIsShown()
|
||||
{
|
||||
var gate = new ArchiveReadGate();
|
||||
Assert.True(gate.IsCurrent(gate.BeginRead(), out var again));
|
||||
Assert.False(again);
|
||||
Assert.True(gate.IsCurrent(gate.BeginRead()));
|
||||
Assert.True(gate.WrittenAsync().IsCompleted, "no edit is being written");
|
||||
|
||||
// An edit written and ended before the read began is in the file it reads.
|
||||
gate.BeginWrite();
|
||||
Assert.False(gate.EndWrite(), "no read overlapped it");
|
||||
Assert.True(gate.IsCurrent(gate.BeginRead(), out again));
|
||||
Assert.False(again);
|
||||
gate.EndWrite();
|
||||
Assert.True(gate.IsCurrent(gate.BeginRead()));
|
||||
Assert.True(gate.WrittenAsync().IsCompleted);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AReadThatAnEditOverlapsIsReadAgainWhenTheEditIsWritten()
|
||||
public void AReadThatAnEditOverlapsWaitsForTheEditAndReadsAgain()
|
||||
{
|
||||
// Settings is shown and reads archive.json; the user commits a template before the read answers.
|
||||
var gate = new ArchiveReadGate();
|
||||
var read = gate.BeginRead();
|
||||
gate.BeginWrite();
|
||||
Assert.False(gate.IsCurrent(read, out var again), "the read may hold the file before the edit");
|
||||
Assert.False(again, "the edit is still being written: its end reads again");
|
||||
Assert.True(gate.EndWrite(), "the edit's end reads the rows again");
|
||||
Assert.False(gate.IsCurrent(read), "the read may hold the file before the edit");
|
||||
var written = gate.WrittenAsync();
|
||||
Assert.False(written.IsCompleted, "the edit is still being written");
|
||||
gate.EndWrite();
|
||||
Assert.True(written.IsCompleted, "the reader reads again once the edit is written");
|
||||
Assert.True(gate.IsCurrent(gate.BeginRead()));
|
||||
|
||||
// That read is shown, and asks nothing more of later edits.
|
||||
Assert.True(gate.IsCurrent(gate.BeginRead(), out again));
|
||||
gate.BeginWrite();
|
||||
Assert.False(gate.EndWrite());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AReadOverlappingAnEditAlreadyWrittenIsReadAgainAtOnce()
|
||||
{
|
||||
// The edit began and ended while the read was out.
|
||||
var gate = new ArchiveReadGate();
|
||||
var read = gate.BeginRead();
|
||||
gate.BeginWrite();
|
||||
Assert.False(gate.EndWrite(), "the read was not dropped yet");
|
||||
Assert.False(gate.IsCurrent(read, out var again));
|
||||
Assert.True(again, "nothing else reads again: the read does");
|
||||
|
||||
// A read that began while an edit was being written, which ended before the read answered.
|
||||
gate.BeginWrite();
|
||||
// The edit began and ended while the read was out: the reader reads again at once.
|
||||
read = gate.BeginRead();
|
||||
Assert.False(gate.EndWrite());
|
||||
Assert.False(gate.IsCurrent(read, out again));
|
||||
Assert.True(again);
|
||||
Assert.True(gate.IsCurrent(gate.BeginRead(), out again));
|
||||
gate.BeginWrite();
|
||||
gate.EndWrite();
|
||||
Assert.False(gate.IsCurrent(read));
|
||||
Assert.True(gate.WrittenAsync().IsCompleted);
|
||||
Assert.True(gate.IsCurrent(gate.BeginRead()));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void OnlyTheLastOfSeveralEditsReadsAgain()
|
||||
public void AReadBegunWhileAnEditIsWrittenIsNeverShown()
|
||||
{
|
||||
// The rows are read again (a folder change, the screen shown) while an edit is still being written.
|
||||
var gate = new ArchiveReadGate();
|
||||
gate.BeginWrite();
|
||||
var read = gate.BeginRead();
|
||||
Assert.False(gate.IsCurrent(read));
|
||||
var written = gate.WrittenAsync();
|
||||
gate.EndWrite();
|
||||
Assert.False(gate.IsCurrent(read), "it began before the edit was written");
|
||||
Assert.True(written.IsCompleted);
|
||||
Assert.True(gate.IsCurrent(gate.BeginRead()));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AReaderGoesOnOnlyOnceTheLastOfSeveralEditsIsWrittenAndNeverInsideIt()
|
||||
{
|
||||
// A proxy size and a thumbnail size picked one after the other, both overlapping a read.
|
||||
var gate = new ArchiveReadGate();
|
||||
var read = gate.BeginRead();
|
||||
gate.BeginWrite();
|
||||
gate.BeginWrite();
|
||||
Assert.False(gate.IsCurrent(read, out var again));
|
||||
Assert.False(again);
|
||||
Assert.False(gate.EndWrite(), "another edit is still being written");
|
||||
// A read begun meanwhile is dropped too, and left to the last edit.
|
||||
read = gate.BeginRead();
|
||||
Assert.False(gate.IsCurrent(read, out again));
|
||||
Assert.False(again);
|
||||
Assert.True(gate.EndWrite());
|
||||
Assert.False(gate.IsCurrent(read));
|
||||
var written = gate.WrittenAsync();
|
||||
Assert.Same(written, gate.WrittenAsync());
|
||||
gate.EndWrite();
|
||||
Assert.False(written.IsCompleted, "another edit is still being written");
|
||||
|
||||
// The reader goes on once the last edit ends, never inside its EndWrite: the writer finishes first.
|
||||
var thread = Environment.CurrentManagedThreadId;
|
||||
var ending = false;
|
||||
var inside = written.ContinueWith(_ => ending && Environment.CurrentManagedThreadId == thread, CancellationToken.None,
|
||||
TaskContinuationOptions.ExecuteSynchronously, TaskScheduler.Default);
|
||||
ending = true;
|
||||
gate.EndWrite();
|
||||
ending = false;
|
||||
Assert.True(written.IsCompleted);
|
||||
Assert.False(await inside, "the reader did not go on inside EndWrite");
|
||||
Assert.True(gate.IsCurrent(gate.BeginRead()));
|
||||
Assert.Throws<InvalidOperationException>(() => gate.EndWrite());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user