1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-11 14:17:26 +08:00

Add test coverage of realm async writes

This commit is contained in:
Dean Herbert 2022-06-27 19:34:42 +09:00
parent 83982d258d
commit 6203885040

View File

@ -2,11 +2,14 @@
// See the LICENCE file in the repository root for full licence text. // See the LICENCE file in the repository root for full licence text.
using System; using System;
using System.Linq;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using NUnit.Framework; using NUnit.Framework;
using osu.Framework.Extensions;
using osu.Game.Beatmaps; using osu.Game.Beatmaps;
using osu.Game.Database; using osu.Game.Database;
using osu.Game.Tests.Resources;
namespace osu.Game.Tests.Database namespace osu.Game.Tests.Database
{ {
@ -33,6 +36,64 @@ namespace osu.Game.Tests.Database
}); });
} }
[Test]
public void TestAsyncWriteAsync()
{
RunTestWithRealmAsync(async (realm, _) =>
{
await realm.WriteAsync(r => r.Add(TestResources.CreateTestBeatmapSetInfo()));
realm.Run(r => r.Refresh());
Assert.That(realm.Run(r => r.All<BeatmapSetInfo>().Count()), Is.EqualTo(1));
});
}
[Test]
public void TestAsyncWrite()
{
RunTestWithRealm((realm, _) =>
{
realm.WriteAsync(r => r.Add(TestResources.CreateTestBeatmapSetInfo())).WaitSafely();
realm.Run(r => r.Refresh());
Assert.That(realm.Run(r => r.All<BeatmapSetInfo>().Count()), Is.EqualTo(1));
});
}
[Test]
public void TestAsyncWriteAfterDisposal()
{
RunTestWithRealm((realm, _) =>
{
realm.Dispose();
Assert.ThrowsAsync<ObjectDisposedException>(() => realm.WriteAsync(r => r.Add(TestResources.CreateTestBeatmapSetInfo())));
});
}
[Test]
public void TestAsyncWriteBeforeDisposal()
{
ManualResetEventSlim resetEvent = new ManualResetEventSlim();
RunTestWithRealm((realm, _) =>
{
var writeTask = realm.WriteAsync(r =>
{
// ensure that disposal blocks for our execution
Assert.That(resetEvent.Wait(100), Is.False);
r.Add(TestResources.CreateTestBeatmapSetInfo());
});
realm.Dispose();
resetEvent.Set();
writeTask.WaitSafely();
});
}
/// <summary> /// <summary>
/// Test to ensure that a `CreateContext` call nested inside a subscription doesn't cause any deadlocks /// Test to ensure that a `CreateContext` call nested inside a subscription doesn't cause any deadlocks
/// due to context fetching semaphores. /// due to context fetching semaphores.