Skip to content

Commit

Permalink
SyncFixture: Prevent parallel cleanup of the same directory (#1311)
Browse files Browse the repository at this point in the history
* SyncFixture: Prevent parallel cleanup of the same directory

* Use Hashset and lock syntax
  • Loading branch information
myieye authored Dec 18, 2024
1 parent f2101d5 commit 67896c5
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions backend/FwLite/FwLiteProjectSync.Tests/Fixtures/SyncFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,37 +17,51 @@ public class SyncFixture : IAsyncLifetime
_services.ServiceProvider.GetRequiredService<CrdtFwdataProjectSyncService>();
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 HashSet<string> _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();
_cleanup = Defer.Action(() => rootServiceProvider.Dispose());
_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()
{
lock (_preCleanupLock)
{
if (!_preCleanupDone.Contains(_projectFolder))
{
_preCleanupDone.Add(_projectFolder);
if (Path.Exists(_projectFolder))
{
Directory.Delete(_projectFolder, true);
}
}
}

var projectsFolder = _services.ServiceProvider.GetRequiredService<IOptions<FwDataBridgeConfig>>().Value
.ProjectsFolder;
if (Path.Exists(projectsFolder)) Directory.Delete(projectsFolder, true);
Directory.CreateDirectory(projectsFolder);
_services.ServiceProvider.GetRequiredService<IProjectLoader>()
.NewProject(new FwDataProject(_projectName, projectsFolder), "en", "en");
FwDataApi = _services.ServiceProvider.GetRequiredService<FwDataFactory>().GetFwDataMiniLcmApi(_projectName, false);

var crdtProjectsFolder =
_services.ServiceProvider.GetRequiredService<IOptions<LcmCrdtConfig>>().Value.ProjectPath;
if (Path.Exists(crdtProjectsFolder)) Directory.Delete(crdtProjectsFolder, true);
Directory.CreateDirectory(crdtProjectsFolder);
var crdtProject = await _services.ServiceProvider.GetRequiredService<CrdtProjectsService>()
.CreateProject(new(_projectName, FwProjectId: FwDataApi.ProjectId, SeedNewProjectData: false));
Expand Down

0 comments on commit 67896c5

Please sign in to comment.