Ensure request handling for OnlinePlayTestScene runs in a scheduled fashion

This commit is contained in:
Dean Herbert 2022-05-30 16:33:07 +09:00
parent c18dd8c8fb
commit f935f034c2

View File

@ -2,6 +2,7 @@
// See the LICENCE file in the repository root for full licence text.
using System;
using System.Threading.Tasks;
using NUnit.Framework;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
@ -67,7 +68,24 @@ namespace osu.Game.Tests.Visual.OnlinePlay
// To get around this, the BeatmapManager is looked up from the dependencies provided to the children of the test scene instead.
var beatmapManager = dependencies.Get<BeatmapManager>();
((DummyAPIAccess)API).HandleRequest = request => handler.HandleRequest(request, API.LocalUser.Value, beatmapManager);
((DummyAPIAccess)API).HandleRequest = request =>
{
TaskCompletionSource<bool> tcs = new TaskCompletionSource<bool>();
// Because some of the handlers use realm, we need to ensure the game is still alive when firing.
// If we don't, a stray `PerformAsync` could hit an `ObjectDisposedException` if running too late.
Scheduler.Add(() =>
{
bool result = handler.HandleRequest(request, API.LocalUser.Value, beatmapManager);
tcs.SetResult(result);
}, false);
#pragma warning disable RS0030
// We can't GetResultSafely() here (will fail with "Can't use GetResultSafely from inside an async operation."), but Wait is safe enough due to
// the task being a TaskCompletionSource.
return tcs.Task.Result;
#pragma warning restore RS0030
};
});
/// <summary>