Update tests to run inside a `GameHost` to allow running on update thread

This commit is contained in:
Dean Herbert 2021-10-08 15:58:22 +09:00
parent a16ffd2976
commit b946011292
1 changed files with 51 additions and 25 deletions

View File

@ -4,7 +4,6 @@
using System;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using Nito.AsyncEx;
using NUnit.Framework;
using osu.Framework.Logging;
using osu.Framework.Platform;
@ -28,7 +27,9 @@ static RealmTest()
protected void RunTestWithRealm(Action<RealmContextFactory, Storage> testAction, [CallerMemberName] string caller = "")
{
AsyncContext.Run(() =>
using (HeadlessGameHost host = new CleanRunHeadlessGameHost(caller))
{
host.Run(new RealmTestGame(() =>
{
var testStorage = storage.GetStorageForDirectory(caller);
@ -43,12 +44,15 @@ protected void RunTestWithRealm(Action<RealmContextFactory, Storage> testAction,
realmFactory.Compact();
Logger.Log($"Final database size after compact: {getFileSize(testStorage, realmFactory)}");
}
});
}));
}
}
protected void RunTestWithRealmAsync(Func<RealmContextFactory, Storage, Task> testAction, [CallerMemberName] string caller = "")
{
AsyncContext.Run(async () =>
using (HeadlessGameHost host = new CleanRunHeadlessGameHost(caller))
{
host.Run(new RealmTestGame(async () =>
{
var testStorage = storage.GetStorageForDirectory(caller);
@ -61,11 +65,33 @@ protected void RunTestWithRealmAsync(Func<RealmContextFactory, Storage, Task> te
Logger.Log($"Final database size: {getFileSize(testStorage, realmFactory)}");
realmFactory.Compact();
Logger.Log($"Final database size after compact: {getFileSize(testStorage, realmFactory)}");
}
}));
}
}
private class RealmTestGame : Framework.Game
{
public RealmTestGame(Func<Task> work)
{
// ReSharper disable once AsyncVoidLambda
Scheduler.Add(async () =>
{
await work().ConfigureAwait(true);
Exit();
});
}
public RealmTestGame(Action work)
{
Scheduler.Add(() =>
{
work();
Exit();
});
}
}
private static long getFileSize(Storage testStorage, RealmContextFactory realmFactory)
{
try