From 4d11794d31d0822e0738f49d5adb09492843a021 Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Wed, 17 Nov 2021 11:06:43 +0900 Subject: [PATCH] Add test coverage of `GetPlayableBeatmap` timeout and cancellation --- osu.Game.Tests/Beatmaps/WorkingBeatmapTest.cs | 102 ++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 osu.Game.Tests/Beatmaps/WorkingBeatmapTest.cs diff --git a/osu.Game.Tests/Beatmaps/WorkingBeatmapTest.cs b/osu.Game.Tests/Beatmaps/WorkingBeatmapTest.cs new file mode 100644 index 0000000000..02e4a87a7a --- /dev/null +++ b/osu.Game.Tests/Beatmaps/WorkingBeatmapTest.cs @@ -0,0 +1,102 @@ +// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. +// See the LICENCE file in the repository root for full licence text. + +using System; +using System.Collections.Generic; +using System.Threading; +using System.Threading.Tasks; +using NUnit.Framework; +using osu.Game.Beatmaps; +using osu.Game.Rulesets; +using osu.Game.Rulesets.Objects; +using osu.Game.Rulesets.Osu; +using osu.Game.Rulesets.Osu.Beatmaps; + +namespace osu.Game.Tests.Beatmaps +{ + [TestFixture] + public class WorkingBeatmapTest + { + [Test] + public void TestGetPlayableSuccess() + { + var working = new TestNeverLoadsWorkingBeatmap(); + + working.ResetEvent.Set(); + + Assert.NotNull(working.GetPlayableBeatmap(new OsuRuleset().RulesetInfo)); + } + + [Test] + public void TestGetPlayableCancellationToken() + { + var working = new TestNeverLoadsWorkingBeatmap(); + + var cts = new CancellationTokenSource(); + var loadStarted = new ManualResetEventSlim(); + var loadCompleted = new ManualResetEventSlim(); + + Task.Factory.StartNew(() => + { + loadStarted.Set(); + Assert.Throws(() => working.GetPlayableBeatmap(new OsuRuleset().RulesetInfo, cancellationToken: cts.Token)); + loadCompleted.Set(); + }, TaskCreationOptions.LongRunning); + + Assert.IsTrue(loadStarted.Wait(10000)); + + cts.Cancel(); + + Assert.IsTrue(loadCompleted.Wait(10000)); + + working.ResetEvent.Set(); + } + + [Test] + public void TestGetPlayableDefaultTimeout() + { + var working = new TestNeverLoadsWorkingBeatmap(); + + Assert.Throws(() => working.GetPlayableBeatmap(new OsuRuleset().RulesetInfo)); + + working.ResetEvent.Set(); + } + + public class TestNeverLoadsWorkingBeatmap : TestWorkingBeatmap + { + public ManualResetEventSlim ResetEvent = new ManualResetEventSlim(); + + public TestNeverLoadsWorkingBeatmap() + : base(new Beatmap()) + { + } + + protected override IBeatmapConverter CreateBeatmapConverter(IBeatmap beatmap, Ruleset ruleset) => new TestConverter(beatmap, ResetEvent); + + public class TestConverter : IBeatmapConverter + { + private readonly ManualResetEventSlim resetEvent; + + public TestConverter(IBeatmap beatmap, ManualResetEventSlim resetEvent) + { + this.resetEvent = resetEvent; + Beatmap = beatmap; + } + + public event Action> ObjectConverted; + + protected virtual void OnObjectConverted(HitObject arg1, IEnumerable arg2) => ObjectConverted?.Invoke(arg1, arg2); + + public IBeatmap Beatmap { get; } + + public bool CanConvert() => true; + + public IBeatmap Convert(CancellationToken cancellationToken = default) + { + resetEvent.Wait(cancellationToken); + return new OsuBeatmap(); + } + } + } + } +}