diff --git a/src/ServiceControlInstaller.Engine.UnitTests/Validation/PackagedRavenDBTests.cs b/src/ServiceControlInstaller.Engine.UnitTests/Validation/PackagedRavenDBTests.cs index 01de0bd0cb..c32b2b619e 100644 --- a/src/ServiceControlInstaller.Engine.UnitTests/Validation/PackagedRavenDBTests.cs +++ b/src/ServiceControlInstaller.Engine.UnitTests/Validation/PackagedRavenDBTests.cs @@ -77,10 +77,7 @@ await store.Maintenance.SendAsync(new PutIndexesOperation(new IndexDefinition finally { await StopServer(); - if (Directory.Exists(root)) - { - FileUtils.DeleteDirectory(root, recursive: true, contentsOnly: false); - } + await TryDeleteDirectory(root, CancellationToken.None); } async Task StartServer() @@ -143,9 +140,9 @@ async Task StopServer() server.Kill(entireProcessTree: true); } - await server.WaitForExitAsync(CancellationToken.None).WaitAsync(TimeSpan.FromSeconds(15)); - TestContext.Out.WriteLine(await standardOutput.WaitAsync(TimeSpan.FromSeconds(15))); - TestContext.Out.WriteLine(await standardError.WaitAsync(TimeSpan.FromSeconds(15))); + await server.WaitForExitAsync(CancellationToken.None).WaitAsync(TimeSpan.FromSeconds(15), cancellationToken); + await TestContext.Out.WriteLineAsync(await standardOutput.WaitAsync(TimeSpan.FromSeconds(15), cancellationToken)); + await TestContext.Out.WriteLineAsync(await standardError.WaitAsync(TimeSpan.FromSeconds(15), cancellationToken)); } finally { @@ -155,6 +152,30 @@ async Task StopServer() } } + // Cleanup of the temp folder must not fail the test; Directory.Delete keeps the path stable across retries. + static async Task TryDeleteDirectory(string root, CancellationToken cancellationToken, int attempts = 5, TimeSpan? delay = null) + { + var retryDelay = delay ?? TimeSpan.FromMilliseconds(500); + for (var attempt = 1; Directory.Exists(root); attempt++) + { + try + { + Directory.Delete(root, recursive: true); + return; + } + catch (Exception ex) when (ex is IOException or UnauthorizedAccessException) + { + if (attempt == attempts) + { + await TestContext.Out.WriteLineAsync($"WARNING: Could not delete '{root}' after {attempts} attempts: {ex}"); + return; + } + + await Task.Delay(retryDelay * attempt, cancellationToken); + } + } + } + static async Task AssertDocumentAndIndex(IDocumentStore store, CancellationToken cancellationToken) { using var session = store.OpenAsyncSession();