Skip to content

Commit 8ccfbe6

Browse files
boblangleyclaude
andauthored
Don't fail PackagedRavenDBTests when the temp folder can't be deleted (#6038)
Backports the cleanup change to PackagedRavenDBTests from #5969 on master. On Windows, RavenDB server files can still be locked right after the process exits, which failed the test during cleanup. Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 7fea158 commit 8ccfbe6

1 file changed

Lines changed: 28 additions & 7 deletions

File tree

‎src/ServiceControlInstaller.Engine.UnitTests/Validation/PackagedRavenDBTests.cs‎

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,7 @@ await store.Maintenance.SendAsync(new PutIndexesOperation(new IndexDefinition
7777
finally
7878
{
7979
await StopServer();
80-
if (Directory.Exists(root))
81-
{
82-
FileUtils.DeleteDirectory(root, recursive: true, contentsOnly: false);
83-
}
80+
await TryDeleteDirectory(root, CancellationToken.None);
8481
}
8582

8683
async Task StartServer()
@@ -143,9 +140,9 @@ async Task StopServer()
143140
server.Kill(entireProcessTree: true);
144141
}
145142

146-
await server.WaitForExitAsync(CancellationToken.None).WaitAsync(TimeSpan.FromSeconds(15));
147-
TestContext.Out.WriteLine(await standardOutput.WaitAsync(TimeSpan.FromSeconds(15)));
148-
TestContext.Out.WriteLine(await standardError.WaitAsync(TimeSpan.FromSeconds(15)));
143+
await server.WaitForExitAsync(CancellationToken.None).WaitAsync(TimeSpan.FromSeconds(15), cancellationToken);
144+
await TestContext.Out.WriteLineAsync(await standardOutput.WaitAsync(TimeSpan.FromSeconds(15), cancellationToken));
145+
await TestContext.Out.WriteLineAsync(await standardError.WaitAsync(TimeSpan.FromSeconds(15), cancellationToken));
149146
}
150147
finally
151148
{
@@ -155,6 +152,30 @@ async Task StopServer()
155152
}
156153
}
157154

155+
// Cleanup of the temp folder must not fail the test; Directory.Delete keeps the path stable across retries.
156+
static async Task TryDeleteDirectory(string root, CancellationToken cancellationToken, int attempts = 5, TimeSpan? delay = null)
157+
{
158+
var retryDelay = delay ?? TimeSpan.FromMilliseconds(500);
159+
for (var attempt = 1; Directory.Exists(root); attempt++)
160+
{
161+
try
162+
{
163+
Directory.Delete(root, recursive: true);
164+
return;
165+
}
166+
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
167+
{
168+
if (attempt == attempts)
169+
{
170+
await TestContext.Out.WriteLineAsync($"WARNING: Could not delete '{root}' after {attempts} attempts: {ex}");
171+
return;
172+
}
173+
174+
await Task.Delay(retryDelay * attempt, cancellationToken);
175+
}
176+
}
177+
}
178+
158179
static async Task AssertDocumentAndIndex(IDocumentStore store, CancellationToken cancellationToken)
159180
{
160181
using var session = store.OpenAsyncSession();

0 commit comments

Comments
 (0)