From 401ffcfbb4325a9880314ab7b045de7c0a509727 Mon Sep 17 00:00:00 2001 From: Tim Haasdyk Date: Tue, 10 Dec 2024 17:09:34 +0100 Subject: [PATCH 1/2] 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)); From bfe6233071efe34433a88ffa5f7a11b0642178f8 Mon Sep 17 00:00:00 2001 From: Tim Haasdyk Date: Wed, 18 Dec 2024 10:19:37 +0100 Subject: [PATCH 2/2] Use Hashset and lock syntax --- .../FwLiteProjectSync.Tests/Fixtures/SyncFixture.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/backend/FwLite/FwLiteProjectSync.Tests/Fixtures/SyncFixture.cs b/backend/FwLite/FwLiteProjectSync.Tests/Fixtures/SyncFixture.cs index 88ce37c96..ecb454bb9 100644 --- a/backend/FwLite/FwLiteProjectSync.Tests/Fixtures/SyncFixture.cs +++ b/backend/FwLite/FwLiteProjectSync.Tests/Fixtures/SyncFixture.cs @@ -20,7 +20,7 @@ public class SyncFixture : IAsyncLifetime private readonly string _projectFolder; private readonly IDisposable _cleanup; private static readonly Lock _preCleanupLock = new(); - private static readonly Dictionary _preCleanupDone = []; + private static readonly HashSet _preCleanupDone = []; public static SyncFixture Create([CallerMemberName] string projectName = "", [CallerMemberName] string projectFolder = "") => new(projectName, projectFolder); @@ -41,11 +41,11 @@ public SyncFixture() : this("sena-3_" + Guid.NewGuid().ToString().Split("-")[0], public async Task InitializeAsync() { - using (_preCleanupLock.EnterScope()) + lock (_preCleanupLock) { - if (!_preCleanupDone.ContainsKey(_projectFolder)) + if (!_preCleanupDone.Contains(_projectFolder)) { - _preCleanupDone.Add(_projectFolder, true); + _preCleanupDone.Add(_projectFolder); if (Path.Exists(_projectFolder)) { Directory.Delete(_projectFolder, true);