2021-09-30 14:59:26 +00:00
|
|
|
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
|
|
|
|
// See the LICENCE file in the repository root for full licence text.
|
|
|
|
|
|
|
|
using System;
|
|
|
|
using System.Runtime.CompilerServices;
|
|
|
|
using System.Threading.Tasks;
|
2022-07-26 05:56:41 +00:00
|
|
|
using JetBrains.Annotations;
|
2021-09-30 14:59:26 +00:00
|
|
|
using NUnit.Framework;
|
2021-10-11 06:26:16 +00:00
|
|
|
using osu.Framework.Extensions;
|
2021-09-30 14:59:26 +00:00
|
|
|
using osu.Framework.Logging;
|
|
|
|
using osu.Framework.Platform;
|
2021-11-19 10:07:21 +00:00
|
|
|
using osu.Game.Beatmaps;
|
2021-09-30 14:59:26 +00:00
|
|
|
using osu.Game.Database;
|
2021-12-14 04:52:27 +00:00
|
|
|
using osu.Game.IO;
|
2021-10-11 06:26:16 +00:00
|
|
|
using osu.Game.Models;
|
2021-11-19 10:07:21 +00:00
|
|
|
using osu.Game.Rulesets;
|
2021-09-30 14:59:26 +00:00
|
|
|
|
|
|
|
namespace osu.Game.Tests.Database
|
|
|
|
{
|
|
|
|
[TestFixture]
|
|
|
|
public abstract partial class RealmTest
|
|
|
|
{
|
2022-07-26 05:56:41 +00:00
|
|
|
protected void RunTestWithRealm([InstantHandle] Action<RealmAccess, OsuStorage> testAction, [CallerMemberName] string caller = "")
|
2021-09-30 14:59:26 +00:00
|
|
|
{
|
2021-12-24 11:17:20 +00:00
|
|
|
using (HeadlessGameHost host = new CleanRunHeadlessGameHost(callingMethodName: caller))
|
2021-09-30 14:59:26 +00:00
|
|
|
{
|
2021-10-08 06:58:22 +00:00
|
|
|
host.Run(new RealmTestGame(() =>
|
2021-09-30 14:59:26 +00:00
|
|
|
{
|
2022-07-26 05:56:41 +00:00
|
|
|
var defaultStorage = host.Storage;
|
|
|
|
|
|
|
|
var testStorage = new OsuStorage(host, defaultStorage);
|
2021-10-08 06:58:22 +00:00
|
|
|
|
2022-03-30 04:34:48 +00:00
|
|
|
using (var realm = new RealmAccess(testStorage, OsuGameBase.CLIENT_DATABASE_FILENAME))
|
2021-10-08 06:58:22 +00:00
|
|
|
{
|
2022-01-24 10:59:58 +00:00
|
|
|
Logger.Log($"Running test using realm file {testStorage.GetFullPath(realm.Filename)}");
|
|
|
|
testAction(realm, testStorage);
|
2021-09-30 14:59:26 +00:00
|
|
|
|
2023-10-20 06:38:00 +00:00
|
|
|
// ReSharper disable once DisposeOnUsingVariable
|
2022-01-24 10:59:58 +00:00
|
|
|
realm.Dispose();
|
2021-10-01 01:40:55 +00:00
|
|
|
|
2022-01-24 10:59:58 +00:00
|
|
|
Logger.Log($"Final database size: {getFileSize(testStorage, realm)}");
|
|
|
|
realm.Compact();
|
|
|
|
Logger.Log($"Final database size after compact: {getFileSize(testStorage, realm)}");
|
2021-10-08 06:58:22 +00:00
|
|
|
}
|
|
|
|
}));
|
|
|
|
}
|
2021-09-30 14:59:26 +00:00
|
|
|
}
|
|
|
|
|
2022-01-24 10:59:58 +00:00
|
|
|
protected void RunTestWithRealmAsync(Func<RealmAccess, Storage, Task> testAction, [CallerMemberName] string caller = "")
|
2021-09-30 14:59:26 +00:00
|
|
|
{
|
2021-12-24 11:17:20 +00:00
|
|
|
using (HeadlessGameHost host = new CleanRunHeadlessGameHost(callingMethodName: caller))
|
2021-09-30 14:59:26 +00:00
|
|
|
{
|
2021-10-08 06:58:22 +00:00
|
|
|
host.Run(new RealmTestGame(async () =>
|
2021-09-30 14:59:26 +00:00
|
|
|
{
|
2022-07-26 05:56:41 +00:00
|
|
|
var testStorage = host.Storage;
|
2021-10-08 06:58:22 +00:00
|
|
|
|
2022-03-30 04:34:48 +00:00
|
|
|
using (var realm = new RealmAccess(testStorage, OsuGameBase.CLIENT_DATABASE_FILENAME))
|
2021-10-08 06:58:22 +00:00
|
|
|
{
|
2022-01-24 10:59:58 +00:00
|
|
|
Logger.Log($"Running test using realm file {testStorage.GetFullPath(realm.Filename)}");
|
|
|
|
await testAction(realm, testStorage);
|
2021-10-08 06:58:22 +00:00
|
|
|
|
2023-10-20 06:38:00 +00:00
|
|
|
// ReSharper disable once DisposeOnUsingVariable
|
2022-01-24 10:59:58 +00:00
|
|
|
realm.Dispose();
|
2021-10-08 06:58:22 +00:00
|
|
|
|
2022-01-24 10:59:58 +00:00
|
|
|
Logger.Log($"Final database size: {getFileSize(testStorage, realm)}");
|
|
|
|
realm.Compact();
|
2021-10-08 06:58:22 +00:00
|
|
|
}
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
}
|
2021-09-30 14:59:26 +00:00
|
|
|
|
2021-11-19 10:07:21 +00:00
|
|
|
protected static BeatmapSetInfo CreateBeatmapSet(RulesetInfo ruleset)
|
2021-10-11 06:26:16 +00:00
|
|
|
{
|
|
|
|
RealmFile createRealmFile() => new RealmFile { Hash = Guid.NewGuid().ToString().ComputeSHA2Hash() };
|
|
|
|
|
2021-11-19 10:07:21 +00:00
|
|
|
var metadata = new BeatmapMetadata
|
2021-10-11 06:26:16 +00:00
|
|
|
{
|
|
|
|
Title = "My Love",
|
|
|
|
Artist = "Kuba Oms"
|
|
|
|
};
|
|
|
|
|
2021-11-19 10:07:21 +00:00
|
|
|
var beatmapSet = new BeatmapSetInfo
|
2021-10-11 06:26:16 +00:00
|
|
|
{
|
|
|
|
Beatmaps =
|
|
|
|
{
|
2021-11-19 10:07:21 +00:00
|
|
|
new BeatmapInfo(ruleset, new BeatmapDifficulty(), metadata) { DifficultyName = "Easy", },
|
|
|
|
new BeatmapInfo(ruleset, new BeatmapDifficulty(), metadata) { DifficultyName = "Normal", },
|
|
|
|
new BeatmapInfo(ruleset, new BeatmapDifficulty(), metadata) { DifficultyName = "Hard", },
|
|
|
|
new BeatmapInfo(ruleset, new BeatmapDifficulty(), metadata) { DifficultyName = "Insane", }
|
2021-10-11 06:26:16 +00:00
|
|
|
},
|
|
|
|
Files =
|
|
|
|
{
|
|
|
|
new RealmNamedFileUsage(createRealmFile(), "test [easy].osu"),
|
|
|
|
new RealmNamedFileUsage(createRealmFile(), "test [normal].osu"),
|
|
|
|
new RealmNamedFileUsage(createRealmFile(), "test [hard].osu"),
|
|
|
|
new RealmNamedFileUsage(createRealmFile(), "test [insane].osu"),
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
for (int i = 0; i < 8; i++)
|
|
|
|
beatmapSet.Files.Add(new RealmNamedFileUsage(createRealmFile(), $"hitsound{i}.mp3"));
|
|
|
|
|
|
|
|
foreach (var b in beatmapSet.Beatmaps)
|
|
|
|
b.BeatmapSet = beatmapSet;
|
|
|
|
|
|
|
|
return beatmapSet;
|
|
|
|
}
|
|
|
|
|
2021-11-19 10:07:21 +00:00
|
|
|
protected static RulesetInfo CreateRuleset() =>
|
2022-01-27 06:23:16 +00:00
|
|
|
new RulesetInfo("osu", "osu!", string.Empty, 0) { Available = true };
|
2021-10-11 06:26:16 +00:00
|
|
|
|
2021-10-08 06:58:22 +00:00
|
|
|
private partial class RealmTestGame : Framework.Game
|
|
|
|
{
|
2022-07-26 05:56:41 +00:00
|
|
|
public RealmTestGame([InstantHandle] Func<Task> work)
|
2021-10-08 06:58:22 +00:00
|
|
|
{
|
|
|
|
// ReSharper disable once AsyncVoidLambda
|
|
|
|
Scheduler.Add(async () =>
|
|
|
|
{
|
|
|
|
await work().ConfigureAwait(true);
|
|
|
|
Exit();
|
|
|
|
});
|
|
|
|
}
|
2021-09-30 14:59:26 +00:00
|
|
|
|
2022-07-26 05:56:41 +00:00
|
|
|
public RealmTestGame([InstantHandle] Action work)
|
2021-10-08 06:58:22 +00:00
|
|
|
{
|
|
|
|
Scheduler.Add(() =>
|
|
|
|
{
|
|
|
|
work();
|
|
|
|
Exit();
|
|
|
|
});
|
|
|
|
}
|
2021-09-30 14:59:26 +00:00
|
|
|
}
|
2021-10-02 14:01:44 +00:00
|
|
|
|
2022-01-24 10:59:58 +00:00
|
|
|
private static long getFileSize(Storage testStorage, RealmAccess realm)
|
2021-10-02 14:01:44 +00:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2022-01-24 10:59:58 +00:00
|
|
|
using (var stream = testStorage.GetStream(realm.Filename))
|
2021-10-04 05:30:22 +00:00
|
|
|
return stream?.Length ?? 0;
|
2021-10-02 14:01:44 +00:00
|
|
|
}
|
|
|
|
catch
|
|
|
|
{
|
|
|
|
// windows runs may error due to file still being open.
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2021-09-30 14:59:26 +00:00
|
|
|
}
|
|
|
|
}
|