From 401ffcfbb4325a9880314ab7b045de7c0a509727 Mon Sep 17 00:00:00 2001 From: Tim Haasdyk Date: Tue, 10 Dec 2024 17:09:34 +0100 Subject: [PATCH] SyncFixture: Prevent parallel cleanup of the same directory --- .../Fixtures/SyncFixture.cs | 20 ++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/backend/FwLite/FwLiteProjectSync.Tests/Fixtures/SyncFixture.cs b/backend/FwLite/FwLiteProjectSync.Tests/Fixtures/SyncFixture.cs index 7a1279ba1..88ce37c96 100644 --- a/backend/FwLite/FwLiteProjectSync.Tests/Fixtures/SyncFixture.cs +++ b/backend/FwLite/FwLiteProjectSync.Tests/Fixtures/SyncFixture.cs @@ -17,13 +17,17 @@ public class SyncFixture : IAsyncLifetime _services.ServiceProvider.GetRequiredService(); public IServiceProvider Services => _services.ServiceProvider; private readonly string _projectName; + private readonly string _projectFolder; private readonly IDisposable _cleanup; + private static readonly Lock _preCleanupLock = new(); + private static readonly Dictionary _preCleanupDone = []; public static SyncFixture Create([CallerMemberName] string projectName = "", [CallerMemberName] string projectFolder = "") => new(projectName, projectFolder); private SyncFixture(string projectName, string projectFolder) { _projectName = projectName; + _projectFolder = projectFolder; var crdtServices = new ServiceCollection() .AddSyncServices(projectFolder); var rootServiceProvider = crdtServices.BuildServiceProvider(); @@ -31,15 +35,26 @@ private SyncFixture(string projectName, string projectFolder) _services = rootServiceProvider.CreateAsyncScope(); } - public SyncFixture(): this("sena-3_" + Guid.NewGuid().ToString("N"), "FwLiteSyncFixture") + public SyncFixture() : this("sena-3_" + Guid.NewGuid().ToString().Split("-")[0], "FwLiteSyncFixture") { } public async Task InitializeAsync() { + using (_preCleanupLock.EnterScope()) + { + if (!_preCleanupDone.ContainsKey(_projectFolder)) + { + _preCleanupDone.Add(_projectFolder, true); + if (Path.Exists(_projectFolder)) + { + Directory.Delete(_projectFolder, true); + } + } + } + var projectsFolder = _services.ServiceProvider.GetRequiredService>().Value .ProjectsFolder; - if (Path.Exists(projectsFolder)) Directory.Delete(projectsFolder, true); Directory.CreateDirectory(projectsFolder); _services.ServiceProvider.GetRequiredService() .NewProject(new FwDataProject(_projectName, projectsFolder), "en", "en"); @@ -47,7 +62,6 @@ public async Task InitializeAsync() var crdtProjectsFolder = _services.ServiceProvider.GetRequiredService>().Value.ProjectPath; - if (Path.Exists(crdtProjectsFolder)) Directory.Delete(crdtProjectsFolder, true); Directory.CreateDirectory(crdtProjectsFolder); var crdtProject = await _services.ServiceProvider.GetRequiredService() .CreateProject(new(_projectName, FwProjectId: FwDataApi.ProjectId, SeedNewProjectData: false));