2021-09-30 22:59:26 +08: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 13:56:41 +08:00
|
|
|
using JetBrains.Annotations;
|
2021-09-30 22:59:26 +08:00
|
|
|
using NUnit.Framework;
|
2021-10-11 14:26:16 +08:00
|
|
|
using osu.Framework.Extensions;
|
2021-09-30 22:59:26 +08:00
|
|
|
using osu.Framework.Logging;
|
|
|
|
using osu.Framework.Platform;
|
2021-11-19 18:07:21 +08:00
|
|
|
using osu.Game.Beatmaps;
|
2021-09-30 22:59:26 +08:00
|
|
|
using osu.Game.Database;
|
2021-12-14 12:52:27 +08:00
|
|
|
using osu.Game.IO;
|
2021-10-11 14:26:16 +08:00
|
|
|
using osu.Game.Models;
|
2021-11-19 18:07:21 +08:00
|
|
|
using osu.Game.Rulesets;
|
2021-09-30 22:59:26 +08:00
|
|
|
|
|
|
|
namespace osu.Game.Tests.Database
|
|
|
|
{
|
|
|
|
[TestFixture]
|
|
|
|
public abstract partial class RealmTest
|
|
|
|
{
|
2022-07-26 13:56:41 +08:00
|
|
|
protected void RunTestWithRealm([InstantHandle] Action<RealmAccess, OsuStorage> testAction, [CallerMemberName] string caller = "")
|
2021-09-30 22:59:26 +08:00
|
|
|
{
|
2021-12-24 19:17:20 +08:00
|
|
|
using (HeadlessGameHost host = new CleanRunHeadlessGameHost(callingMethodName: caller))
|
2021-09-30 22:59:26 +08:00
|
|
|
{
|
2021-10-08 14:58:22 +08:00
|
|
|
host.Run(new RealmTestGame(() =>
|
2021-09-30 22:59:26 +08:00
|
|
|
{
|
2022-07-26 13:56:41 +08:00
|
|
|
var defaultStorage = host.Storage;
|
|
|
|
|
|
|
|
var testStorage = new OsuStorage(host, defaultStorage);
|
2021-10-08 14:58:22 +08:00
|
|
|
|
2022-03-30 12:34:48 +08:00
|
|
|
using (var realm = new RealmAccess(testStorage, OsuGameBase.CLIENT_DATABASE_FILENAME))
|
2021-10-08 14:58:22 +08:00
|
|
|
{
|
2022-01-24 18:59:58 +08:00
|
|
|
Logger.Log($"Running test using realm file {testStorage.GetFullPath(realm.Filename)}");
|
|
|
|
testAction(realm, testStorage);
|
2021-09-30 22:59:26 +08:00
|
|
|
|
2022-01-24 18:59:58 +08:00
|
|
|
realm.Dispose();
|
2021-10-01 09:40:55 +08:00
|
|
|
|
2022-01-24 18:59:58 +08:00
|
|
|
Logger.Log($"Final database size: {getFileSize(testStorage, realm)}");
|
|
|
|
realm.Compact();
|
|
|
|
Logger.Log($"Final database size after compact: {getFileSize(testStorage, realm)}");
|
2021-10-08 14:58:22 +08:00
|
|
|
}
|
|
|
|
}));
|
|
|
|
}
|
2021-09-30 22:59:26 +08:00
|
|
|
}
|
|
|
|
|
2022-01-24 18:59:58 +08:00
|
|
|
protected void RunTestWithRealmAsync(Func<RealmAccess, Storage, Task> testAction, [CallerMemberName] string caller = "")
|
2021-09-30 22:59:26 +08:00
|
|
|
{
|
2021-12-24 19:17:20 +08:00
|
|
|
using (HeadlessGameHost host = new CleanRunHeadlessGameHost(callingMethodName: caller))
|
2021-09-30 22:59:26 +08:00
|
|
|
{
|
2021-10-08 14:58:22 +08:00
|
|
|
host.Run(new RealmTestGame(async () =>
|
2021-09-30 22:59:26 +08:00
|
|
|
{
|
2022-07-26 13:56:41 +08:00
|
|
|
var testStorage = host.Storage;
|
2021-10-08 14:58:22 +08:00
|
|
|
|
2022-03-30 12:34:48 +08:00
|
|
|
using (var realm = new RealmAccess(testStorage, OsuGameBase.CLIENT_DATABASE_FILENAME))
|
2021-10-08 14:58:22 +08:00
|
|
|
{
|
2022-01-24 18:59:58 +08:00
|
|
|
Logger.Log($"Running test using realm file {testStorage.GetFullPath(realm.Filename)}");
|
|
|
|
await testAction(realm, testStorage);
|
2021-10-08 14:58:22 +08:00
|
|
|
|
2022-01-24 18:59:58 +08:00
|
|
|
realm.Dispose();
|
2021-10-08 14:58:22 +08:00
|
|
|
|
2022-01-24 18:59:58 +08:00
|
|
|
Logger.Log($"Final database size: {getFileSize(testStorage, realm)}");
|
|
|
|
realm.Compact();
|
2021-10-08 14:58:22 +08:00
|
|
|
}
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
}
|
2021-09-30 22:59:26 +08:00
|
|
|
|
2021-11-19 18:07:21 +08:00
|
|
|
protected static BeatmapSetInfo CreateBeatmapSet(RulesetInfo ruleset)
|
2021-10-11 14:26:16 +08:00
|
|
|
{
|
|
|
|
RealmFile createRealmFile() => new RealmFile { Hash = Guid.NewGuid().ToString().ComputeSHA2Hash() };
|
|
|
|
|
2021-11-19 18:07:21 +08:00
|
|
|
var metadata = new BeatmapMetadata
|
2021-10-11 14:26:16 +08:00
|
|
|
{
|
|
|
|
Title = "My Love",
|
|
|
|
Artist = "Kuba Oms"
|
|
|
|
};
|
|
|
|
|
2021-11-19 18:07:21 +08:00
|
|
|
var beatmapSet = new BeatmapSetInfo
|
2021-10-11 14:26:16 +08:00
|
|
|
{
|
|
|
|
Beatmaps =
|
|
|
|
{
|
2021-11-19 18:07:21 +08: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 14:26:16 +08: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 18:07:21 +08:00
|
|
|
protected static RulesetInfo CreateRuleset() =>
|
2022-01-27 14:23:16 +08:00
|
|
|
new RulesetInfo("osu", "osu!", string.Empty, 0) { Available = true };
|
2021-10-11 14:26:16 +08:00
|
|
|
|
2021-10-08 14:58:22 +08:00
|
|
|
private partial class RealmTestGame : Framework.Game
|
|
|
|
{
|
2022-07-26 13:56:41 +08:00
|
|
|
public RealmTestGame([InstantHandle] Func<Task> work)
|
2021-10-08 14:58:22 +08:00
|
|
|
{
|
|
|
|
// ReSharper disable once AsyncVoidLambda
|
|
|
|
Scheduler.Add(async () =>
|
|
|
|
{
|
|
|
|
await work().ConfigureAwait(true);
|
|
|
|
Exit();
|
|
|
|
});
|
|
|
|
}
|
2021-09-30 22:59:26 +08:00
|
|
|
|
2022-07-26 13:56:41 +08:00
|
|
|
public RealmTestGame([InstantHandle] Action work)
|
2021-10-08 14:58:22 +08:00
|
|
|
{
|
|
|
|
Scheduler.Add(() =>
|
|
|
|
{
|
|
|
|
work();
|
|
|
|
Exit();
|
|
|
|
});
|
|
|
|
}
|
2021-09-30 22:59:26 +08:00
|
|
|
}
|
2021-10-02 22:01:44 +08:00
|
|
|
|
2022-01-24 18:59:58 +08:00
|
|
|
private static long getFileSize(Storage testStorage, RealmAccess realm)
|
2021-10-02 22:01:44 +08:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2022-01-24 18:59:58 +08:00
|
|
|
using (var stream = testStorage.GetStream(realm.Filename))
|
2021-10-04 13:30:22 +08:00
|
|
|
return stream?.Length ?? 0;
|
2021-10-02 22:01:44 +08:00
|
|
|
}
|
|
|
|
catch
|
|
|
|
{
|
|
|
|
// windows runs may error due to file still being open.
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2021-09-30 22:59:26 +08:00
|
|
|
}
|
|
|
|
}
|