Avoid leaving left-over files after test run completes

This commit is contained in:
Dean Herbert 2022-07-26 14:56:41 +09:00
parent a7598c62ac
commit 59d3cc52c4
4 changed files with 23 additions and 35 deletions

View File

@ -142,7 +142,6 @@ namespace osu.Game.Tests.Database
{ {
Task.Run(async () => Task.Run(async () =>
{ {
// ReSharper disable once AccessToDisposedClosure
var beatmapSet = await importer.Import(new ImportTask(TestResources.GetTestBeatmapStream(), "renatus.osz")); var beatmapSet = await importer.Import(new ImportTask(TestResources.GetTestBeatmapStream(), "renatus.osz"));
Assert.NotNull(beatmapSet); Assert.NotNull(beatmapSet);

View File

@ -32,31 +32,29 @@ namespace osu.Game.Tests.Database
[Test] [Test]
public void TestAccessAfterStorageMigrate() public void TestAccessAfterStorageMigrate()
{ {
RunTestWithRealm((realm, storage) => using (var migratedStorage = new TemporaryNativeStorage("realm-test-migration-target"))
{ {
var beatmap = new BeatmapInfo(CreateRuleset(), new BeatmapDifficulty(), new BeatmapMetadata()); RunTestWithRealm((realm, storage) =>
Live<BeatmapInfo>? liveBeatmap = null;
realm.Run(r =>
{ {
r.Write(_ => r.Add(beatmap)); var beatmap = new BeatmapInfo(CreateRuleset(), new BeatmapDifficulty(), new BeatmapMetadata());
liveBeatmap = beatmap.ToLive(realm); Live<BeatmapInfo>? liveBeatmap = null;
});
realm.Run(r =>
{
r.Write(_ => r.Add(beatmap));
liveBeatmap = beatmap.ToLive(realm);
});
using (var migratedStorage = new TemporaryNativeStorage("realm-test-migration-target"))
{
migratedStorage.DeleteDirectory(string.Empty); migratedStorage.DeleteDirectory(string.Empty);
using (realm.BlockAllOperations("testing")) using (realm.BlockAllOperations("testing"))
{
storage.Migrate(migratedStorage); storage.Migrate(migratedStorage);
}
Assert.IsFalse(liveBeatmap?.PerformRead(l => l.Hidden)); Assert.IsFalse(liveBeatmap?.PerformRead(l => l.Hidden));
} });
}); }
} }
[Test] [Test]
@ -341,14 +339,12 @@ namespace osu.Game.Tests.Database
liveBeatmap.PerformRead(resolved => liveBeatmap.PerformRead(resolved =>
{ {
// retrieval causes an implicit refresh. even changes that aren't related to the retrieval are fired at this point. // retrieval causes an implicit refresh. even changes that aren't related to the retrieval are fired at this point.
// ReSharper disable once AccessToDisposedClosure
Assert.AreEqual(2, outerRealm.All<BeatmapInfo>().Count()); Assert.AreEqual(2, outerRealm.All<BeatmapInfo>().Count());
Assert.AreEqual(1, changesTriggered); Assert.AreEqual(1, changesTriggered);
// can access properties without a crash. // can access properties without a crash.
Assert.IsFalse(resolved.Hidden); Assert.IsFalse(resolved.Hidden);
// ReSharper disable once AccessToDisposedClosure
outerRealm.Write(r => outerRealm.Write(r =>
{ {
// can use with the main context. // can use with the main context.

View File

@ -4,11 +4,11 @@
using System; using System;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using System.Threading.Tasks; using System.Threading.Tasks;
using JetBrains.Annotations;
using NUnit.Framework; using NUnit.Framework;
using osu.Framework.Extensions; using osu.Framework.Extensions;
using osu.Framework.Logging; using osu.Framework.Logging;
using osu.Framework.Platform; using osu.Framework.Platform;
using osu.Framework.Testing;
using osu.Game.Beatmaps; using osu.Game.Beatmaps;
using osu.Game.Database; using osu.Game.Database;
using osu.Game.IO; using osu.Game.IO;
@ -20,22 +20,15 @@ namespace osu.Game.Tests.Database
[TestFixture] [TestFixture]
public abstract class RealmTest public abstract class RealmTest
{ {
private static readonly TemporaryNativeStorage storage; protected void RunTestWithRealm([InstantHandle] Action<RealmAccess, OsuStorage> testAction, [CallerMemberName] string caller = "")
static RealmTest()
{
storage = new TemporaryNativeStorage("realm-test");
storage.DeleteDirectory(string.Empty);
}
protected void RunTestWithRealm(Action<RealmAccess, OsuStorage> testAction, [CallerMemberName] string caller = "")
{ {
using (HeadlessGameHost host = new CleanRunHeadlessGameHost(callingMethodName: caller)) using (HeadlessGameHost host = new CleanRunHeadlessGameHost(callingMethodName: caller))
{ {
host.Run(new RealmTestGame(() => host.Run(new RealmTestGame(() =>
{ {
// ReSharper disable once AccessToDisposedClosure var defaultStorage = host.Storage;
var testStorage = new OsuStorage(host, storage.GetStorageForDirectory(caller));
var testStorage = new OsuStorage(host, defaultStorage);
using (var realm = new RealmAccess(testStorage, OsuGameBase.CLIENT_DATABASE_FILENAME)) using (var realm = new RealmAccess(testStorage, OsuGameBase.CLIENT_DATABASE_FILENAME))
{ {
@ -58,7 +51,7 @@ namespace osu.Game.Tests.Database
{ {
host.Run(new RealmTestGame(async () => host.Run(new RealmTestGame(async () =>
{ {
var testStorage = storage.GetStorageForDirectory(caller); var testStorage = host.Storage;
using (var realm = new RealmAccess(testStorage, OsuGameBase.CLIENT_DATABASE_FILENAME)) using (var realm = new RealmAccess(testStorage, OsuGameBase.CLIENT_DATABASE_FILENAME))
{ {
@ -116,7 +109,7 @@ namespace osu.Game.Tests.Database
private class RealmTestGame : Framework.Game private class RealmTestGame : Framework.Game
{ {
public RealmTestGame(Func<Task> work) public RealmTestGame([InstantHandle] Func<Task> work)
{ {
// ReSharper disable once AsyncVoidLambda // ReSharper disable once AsyncVoidLambda
Scheduler.Add(async () => Scheduler.Add(async () =>
@ -126,7 +119,7 @@ namespace osu.Game.Tests.Database
}); });
} }
public RealmTestGame(Action work) public RealmTestGame([InstantHandle] Action work)
{ {
Scheduler.Add(() => Scheduler.Add(() =>
{ {

View File

@ -6,6 +6,7 @@
using System; using System;
using System.IO; using System.IO;
using System.Threading.Tasks; using System.Threading.Tasks;
using JetBrains.Annotations;
using NUnit.Framework; using NUnit.Framework;
using osu.Framework.Allocation; using osu.Framework.Allocation;
using osu.Framework.Bindables; using osu.Framework.Bindables;
@ -27,7 +28,6 @@ namespace osu.Game.Tournament.Tests.NonVisual
{ {
var osu = new TestTournament(runOnLoadComplete: () => var osu = new TestTournament(runOnLoadComplete: () =>
{ {
// ReSharper disable once AccessToDisposedClosure
var storage = host.Storage.GetStorageForDirectory(Path.Combine("tournaments", "default")); var storage = host.Storage.GetStorageForDirectory(Path.Combine("tournaments", "default"));
using (var stream = storage.CreateFileSafely("bracket.json")) using (var stream = storage.CreateFileSafely("bracket.json"))
@ -85,7 +85,7 @@ namespace osu.Game.Tournament.Tests.NonVisual
public new Task BracketLoadTask => base.BracketLoadTask; public new Task BracketLoadTask => base.BracketLoadTask;
public TestTournament(bool resetRuleset = false, Action runOnLoadComplete = null) public TestTournament(bool resetRuleset = false, [InstantHandle] Action runOnLoadComplete = null)
{ {
this.resetRuleset = resetRuleset; this.resetRuleset = resetRuleset;
this.runOnLoadComplete = runOnLoadComplete; this.runOnLoadComplete = runOnLoadComplete;