1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-06 06:57:39 +08:00

Add test for WorkingBeatmap leakage

This commit is contained in:
Dean Herbert 2018-06-13 15:12:10 +09:00 committed by smoogipoo
parent b97c415c50
commit caeddc861a

View File

@ -1,9 +1,12 @@
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using System;
using System.Linq;
using osu.Framework.Allocation;
using osu.Framework.Graphics.Shapes;
using osu.Framework.Lists;
using osu.Framework.Logging;
using osu.Game.Beatmaps;
using osu.Game.Rulesets;
using osu.Game.Rulesets.Mods;
@ -43,6 +46,7 @@ namespace osu.Game.Tests.Visual
Player p = null;
AddStep(ruleset.RulesetInfo.Name, () => p = loadPlayerFor(ruleset));
AddUntilStep(() => ContinueCondition(p));
}
else
{
@ -51,6 +55,20 @@ namespace osu.Game.Tests.Visual
Player p = null;
AddStep(r.Name, () => p = loadPlayerFor(r));
AddUntilStep(() => ContinueCondition(p));
AddAssert("no leaked beatmaps", () =>
{
p = null;
GC.Collect();
GC.WaitForPendingFinalizers();
int count = 0;
workingWeakReferences.ForEachAlive(_ => count++);
Logger.Log($"reference count {count}");
return count == 1;
});
}
}
}
@ -59,21 +77,29 @@ namespace osu.Game.Tests.Visual
protected virtual IBeatmap CreateBeatmap(Ruleset ruleset) => new TestBeatmap(ruleset.RulesetInfo);
private readonly WeakList<WorkingBeatmap> workingWeakReferences = new WeakList<WorkingBeatmap>();
private Player loadPlayerFor(RulesetInfo ri) => loadPlayerFor(ri.CreateInstance());
private Player loadPlayerFor(Ruleset r)
{
var beatmap = CreateBeatmap(r);
var working = new TestWorkingBeatmap(beatmap);
Beatmap.Value = new TestWorkingBeatmap(beatmap);
workingWeakReferences.Add(working);
Beatmap.Value = working;
Beatmap.Value.Mods.Value = new[] { r.GetAllMods().First(m => m is ModNoFail) };
if (Player != null)
Remove(Player);
Player?.Exit();
var player = CreatePlayer(r);
LoadComponentAsync(player, LoadScreen);
LoadComponentAsync(player, p =>
{
Player = p;
LoadScreen(p);
});
return player;
}