diff --git a/src/Chronos.Data/Workspaces/WorkspaceSync.cs b/src/Chronos.Data/Workspaces/WorkspaceSync.cs index 9819427d..593da3ac 100644 --- a/src/Chronos.Data/Workspaces/WorkspaceSync.cs +++ b/src/Chronos.Data/Workspaces/WorkspaceSync.cs @@ -388,21 +388,30 @@ public sealed class WorkspaceSync(WorkspaceStore store, string clientName) return await FollowNameLockedAsync(project, home, cancellationToken).ConfigureAwait(false); } + /// + /// under the project's lock. It runs after a pull that succeeded, so it never fails: whatever + /// the home or the copy answers (unreachable, a refusal such as a 403, a file in use) is logged and the name is followed + /// next time; only a cancellation is passed on. + /// private static async Task FollowNameLockedAsync(WorkspaceProject project, IProjectHome home, CancellationToken cancellationToken) { - SequenceProject? listed; try { - listed = (await home.ListAsync(cancellationToken).ConfigureAwait(false)).FirstOrDefault(p => p.Id == project.ProjectId); + var listed = (await home.ListAsync(cancellationToken).ConfigureAwait(false)).FirstOrDefault(p => p.Id == project.ProjectId); + var bundle = project.ReadBundle(); + if (listed is null || bundle.Workspace is not { } workspace || (workspace.Name == listed.Name && workspace.IsMain == listed.IsMain)) return false; + project.WriteBundle(bundle with { Workspace = workspace with { Name = listed.Name, IsMain = listed.IsMain } }); + return true; } catch (Exception ex) when (ex is HomeUnavailableException or ProjectException) { return false; } - var bundle = project.ReadBundle(); - if (listed is null || bundle.Workspace is not { } workspace || (workspace.Name == listed.Name && workspace.IsMain == listed.IsMain)) return false; - project.WriteBundle(bundle with { Workspace = workspace with { Name = listed.Name, IsMain = listed.IsMain } }); - return true; + catch (Exception ex) when (ex is not OperationCanceledException || !cancellationToken.IsCancellationRequested) + { + Log.Warning("workspace", $"The name of project {project.ProjectId} could not be followed from its home; tried again at the next pull", ex); + return false; + } } /// diff --git a/tests/Chronos.Data.Tests/Workspaces/WorkspaceFixture.cs b/tests/Chronos.Data.Tests/Workspaces/WorkspaceFixture.cs index 6f8fab68..5c8175a5 100644 --- a/tests/Chronos.Data.Tests/Workspaces/WorkspaceFixture.cs +++ b/tests/Chronos.Data.Tests/Workspaces/WorkspaceFixture.cs @@ -197,6 +197,41 @@ internal sealed class CountingHome(IProjectHome home) : IProjectHome public Task PushAsync(Guid projectId, ProjectVersion version, CancellationToken cancellationToken) => home.PushAsync(projectId, version, cancellationToken); } +/// +/// A home whose list of projects fails with an error no other call gives (a server's 403 or 500 passed through as it is), the +/// rest passed through. +/// +internal sealed class FailingListHome(IProjectHome home) : IProjectHome +{ + public int Lists { get; private set; } + + public Task> ListAsync(CancellationToken cancellationToken) + { + Lists++; + throw new InvalidOperationException("The server refused the list (403)."); + } + + public Task SequenceAsync(CancellationToken cancellationToken) => home.SequenceAsync(cancellationToken); + + public Task CreateAsync(string name, Guid? fromVersionId, string? author, CancellationToken cancellationToken) => + home.CreateAsync(name, fromVersionId, author, cancellationToken); + + public Task HeadAsync(Guid projectId, CancellationToken cancellationToken) => home.HeadAsync(projectId, cancellationToken); + + public Task GetVersionAsync(Guid projectId, Guid versionId, CancellationToken cancellationToken) => + home.GetVersionAsync(projectId, versionId, cancellationToken); + + public Task> VersionsAfterAsync(Guid projectId, int afterNumber, int limit, CancellationToken cancellationToken) => + home.VersionsAfterAsync(projectId, afterNumber, limit, cancellationToken); + + public Task> HistoryAsync(Guid projectId, int? beforeNumber, int limit, CancellationToken cancellationToken) => + home.HistoryAsync(projectId, beforeNumber, limit, cancellationToken); + + public Task PushAsync(Guid projectId, ProjectVersion version, CancellationToken cancellationToken) => home.PushAsync(projectId, version, cancellationToken); + + public Task FramesAsync(CancellationToken cancellationToken) => home.FramesAsync(cancellationToken); +} + /// A home that cannot be reached, standing for a server out of reach or an archive offline. internal sealed class UnreachableHome(IProjectHome home) : IProjectHome { diff --git a/tests/Chronos.Data.Tests/Workspaces/WorkspaceTests.cs b/tests/Chronos.Data.Tests/Workspaces/WorkspaceTests.cs index df216601..b93d3b77 100644 --- a/tests/Chronos.Data.Tests/Workspaces/WorkspaceTests.cs +++ b/tests/Chronos.Data.Tests/Workspaces/WorkspaceTests.cs @@ -256,6 +256,29 @@ public abstract class WorkspaceTests(TestContext context) : DatabaseTest(context Assert.SequenceEqual([0], working.ExcludedFrames); } + [Test] + public async Task APullThatSucceededNeverFailsOnTheNameItFollows() + { + var fx = await FixtureAsync(); + var (_, one) = fx.Workspace("one"); + var (_, two) = fx.Workspace("two"); + var id = (await fx.CheckoutAsync(one)).ProjectId; + await fx.CheckoutAsync(two, id); + await EditAsync(one, id, s => s with { Stack = new StackSettings { Mode = StackSetting.Median } }, fx.Ct); + await one.CommitAsync(id, cancellationToken: fx.Ct); + await one.PushAsync(id, fx.Home, fx.Ct); + + // The home refuses its list (a server's 403) when the pull, done, follows the project's name: the pull stands. + var home = new FailingListHome(fx.Home); + var name = two.Store.Find(id)!.ReadBundle().Workspace!.Name; + var pulled = await two.PullAsync(id, home, fx.Ct); + Assert.Equal(1, home.Lists); + Assert.Equal((1, WorkspaceState.InSync), (pulled.Pulled.Count, pulled.Status.State)); + Assert.Equal(StackSetting.Median, (await two.OpenAsync(id, fx.Ct)).Working.Stack.Mode); + Assert.Equal(name, two.Store.Find(id)!.ReadBundle().Workspace!.Name); + Assert.False(await two.FollowNameAsync(id, home, fx.Ct)); + } + [Test] public async Task SeveralLocalVersionsAreReplayedOnTopOfTheHomesWhenItMovedMeanwhile() {