Merge pull request #19371 from peppy/fix-working-cache-invalidation

Fix calls to `GetWorkingBeatmap` invalidating cache too often
This commit is contained in:
Dan Balasescu 2022-07-25 18:29:46 +09:00 committed by GitHub
commit 068063a43a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 107 additions and 4 deletions

View File

@ -0,0 +1,100 @@
// 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.Linq;
using NUnit.Framework;
using osu.Framework.Allocation;
using osu.Framework.Audio;
using osu.Framework.Extensions;
using osu.Framework.Platform;
using osu.Framework.Testing;
using osu.Game.Beatmaps;
using osu.Game.Database;
using osu.Game.Rulesets;
using osu.Game.Tests.Resources;
using osu.Game.Tests.Visual;
namespace osu.Game.Tests.Beatmaps
{
[HeadlessTest]
public class WorkingBeatmapManagerTest : OsuTestScene
{
private BeatmapManager beatmaps = null!;
private BeatmapSetInfo importedSet = null!;
[BackgroundDependencyLoader]
private void load(GameHost host, AudioManager audio, RulesetStore rulesets)
{
Dependencies.Cache(beatmaps = new BeatmapManager(LocalStorage, Realm, rulesets, null, audio, Resources, host, Beatmap.Default));
}
[SetUpSteps]
public void SetUpSteps()
{
AddStep("import beatmap", () =>
{
beatmaps.Import(TestResources.GetQuickTestBeatmapForImport()).WaitSafely();
importedSet = beatmaps.GetAllUsableBeatmapSets().First();
});
}
[Test]
public void TestGetWorkingBeatmap() => AddStep("run test", () =>
{
Assert.That(beatmaps.GetWorkingBeatmap(importedSet.Beatmaps.First()), Is.Not.Null);
});
[Test]
public void TestCachedRetrievalNoFiles() => AddStep("run test", () =>
{
var beatmap = importedSet.Beatmaps.First();
Assert.That(beatmap.BeatmapSet?.Files, Is.Empty);
var first = beatmaps.GetWorkingBeatmap(beatmap);
var second = beatmaps.GetWorkingBeatmap(beatmap);
Assert.That(first, Is.SameAs(second));
Assert.That(first.BeatmapInfo.BeatmapSet?.Files, Has.Count.GreaterThan(0));
});
[Test]
public void TestCachedRetrievalWithFiles() => AddStep("run test", () =>
{
var beatmap = Realm.Run(r => r.Find<BeatmapInfo>(importedSet.Beatmaps.First().ID).Detach());
Assert.That(beatmap.BeatmapSet?.Files, Has.Count.GreaterThan(0));
var first = beatmaps.GetWorkingBeatmap(beatmap);
var second = beatmaps.GetWorkingBeatmap(beatmap);
Assert.That(first, Is.SameAs(second));
Assert.That(first.BeatmapInfo.BeatmapSet?.Files, Has.Count.GreaterThan(0));
});
[Test]
public void TestForcedRefetchRetrievalNoFiles() => AddStep("run test", () =>
{
var beatmap = importedSet.Beatmaps.First();
Assert.That(beatmap.BeatmapSet?.Files, Is.Empty);
var first = beatmaps.GetWorkingBeatmap(beatmap);
var second = beatmaps.GetWorkingBeatmap(beatmap, true);
Assert.That(first, Is.Not.SameAs(second));
});
[Test]
public void TestForcedRefetchRetrievalWithFiles() => AddStep("run test", () =>
{
var beatmap = Realm.Run(r => r.Find<BeatmapInfo>(importedSet.Beatmaps.First().ID).Detach());
Assert.That(beatmap.BeatmapSet?.Files, Has.Count.GreaterThan(0));
var first = beatmaps.GetWorkingBeatmap(beatmap);
var second = beatmaps.GetWorkingBeatmap(beatmap, true);
Assert.That(first, Is.Not.SameAs(second));
});
}
}

View File

@ -439,12 +439,15 @@ namespace osu.Game.Beatmaps
{ {
if (beatmapInfo != null) if (beatmapInfo != null)
{ {
// Detached sets don't come with files. if (refetch)
// If we seem to be missing files, now is a good time to re-fetch.
if (refetch || beatmapInfo.IsManaged || beatmapInfo.BeatmapSet?.Files.Count == 0)
{
workingBeatmapCache.Invalidate(beatmapInfo); workingBeatmapCache.Invalidate(beatmapInfo);
// Detached beatmapsets don't come with files as an optimisation (see `RealmObjectExtensions.beatmap_set_mapper`).
// If we seem to be missing files, now is a good time to re-fetch.
bool missingFiles = beatmapInfo.BeatmapSet?.Files.Count == 0;
if (refetch || beatmapInfo.IsManaged || missingFiles)
{
Guid id = beatmapInfo.ID; Guid id = beatmapInfo.ID;
beatmapInfo = Realm.Run(r => r.Find<BeatmapInfo>(id)?.Detach()) ?? beatmapInfo; beatmapInfo = Realm.Run(r => r.Find<BeatmapInfo>(id)?.Detach()) ?? beatmapInfo;
} }