Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,11 @@ public static async Task Stop(CancellationToken cancellationToken = default)
await semaphoreSlim.WaitAsync(cancellationToken);
try
{
embeddedDatabase?.Dispose();
if (embeddedDatabase != null)
{
await embeddedDatabase.DisposeAndDeleteServerTempFiles(cancellationToken);
}

embeddedDatabase = null;
}
finally
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
namespace ServiceControl.Persistence.Tests;

using System.IO;
using System.Linq;
using NUnit.Framework;
using TestHelper;

[TestFixture]
public class EmbeddedDatabaseExtensionsTests
{
[Test]
public void DeleteServerTempFiles_should_delete_only_files_of_the_given_process()
{
var root = Path.Combine(TestContext.CurrentContext.WorkDirectory, "Tests", Path.GetRandomFileName());
var tempDirectory = Path.Combine(root, "RavenDBServer", "Temp");
Directory.CreateDirectory(tempDirectory);

try
{
string[] owned = ["ravendb-100-1-0000000000000000001.journal-a", "ravendb-100-1-compression.0000000000.buffers-b"];
string[] others = ["ravendb-1000-1-0000000000000000001.journal-c", "ravendb-200-1-0000000000000000001.journal-d"];
var binary = Path.Combine(root, "RavenDBServer", "Raven.Server.dll");

foreach (var name in owned.Concat(others))
{
File.WriteAllText(Path.Combine(tempDirectory, name), string.Empty);
}
File.WriteAllText(binary, string.Empty);

EmbeddedDatabaseExtensions.DeleteServerTempFiles(tempDirectory, 100);

Assert.Multiple(() =>
{
Assert.That(owned.Select(n => Path.Combine(tempDirectory, n)), Has.None.Matches<string>(File.Exists));
Assert.That(others.Select(n => Path.Combine(tempDirectory, n)), Has.All.Matches<string>(File.Exists));
Assert.That(File.Exists(binary), Is.True);
});
}
finally
{
Directory.Delete(root, recursive: true);
}
}

[Test]
public void DeleteServerTempFiles_should_ignore_missing_directory() =>
Assert.DoesNotThrow(() => EmbeddedDatabaseExtensions.DeleteServerTempFiles(Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()), 100));
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public async Task TearDown()
}

await embeddedServer.DeleteDatabase(databaseName);
await embeddedServer.DeleteDatabase(((RavenPersisterSettings)PersistenceSettings).ThroughputDatabaseName);
}

public PersistenceSettings PersistenceSettings { get; private set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,11 @@ public static async Task Stop(CancellationToken cancellationToken = default)
await semaphoreSlim.WaitAsync(cancellationToken);
try
{
embeddedDatabase?.Dispose();
if (embeddedDatabase != null)
{
await embeddedDatabase.DisposeAndDeleteServerTempFiles(cancellationToken);
}

embeddedDatabase = null;
}
finally
Expand Down
30 changes: 30 additions & 0 deletions src/TestHelper/EmbeddedDatabaseExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
namespace TestHelper;

using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Raven.Client.ServerWide.Operations;
Expand All @@ -25,4 +26,33 @@ await store.Maintenance.Server.SendAsync(
new DeleteDatabasesOperation(dbName, hardDelete: true, timeToWaitForConfirmation: TimeSpan.FromSeconds(30)),
cancellationToken);
}

// Delete only the stopped server's PID batch so concurrent runs sharing this folder are untouched.
public static async Task DisposeAndDeleteServerTempFiles(this EmbeddedDatabase database, CancellationToken cancellationToken = default)
{
int processId;
try
{
processId = await EmbeddedServer.Instance.GetServerProcessIdAsync(cancellationToken);
}
finally
{
database.Dispose();
}

DeleteServerTempFiles(Path.Combine(Path.GetDirectoryName(typeof(EmbeddedDatabase).Assembly.Location)!, "RavenDBServer", "Temp"), processId);
}

public static void DeleteServerTempFiles(string tempDirectory, int processId)
{
if (!Directory.Exists(tempDirectory))
{
return;
}

foreach (var file in Directory.EnumerateFiles(tempDirectory, $"ravendb-{processId}-*"))
{
File.Delete(file);
}
}
}
Loading