1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-14 18:33:20 +08:00

Add test coverage of potential deeadlock scenario with nested realm context fetching

This commit is contained in:
Dean Herbert 2021-11-29 16:27:16 +09:00
parent 448b2d1190
commit 54798eabc9

View File

@ -5,6 +5,9 @@ using System;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using NUnit.Framework; using NUnit.Framework;
using osu.Game.Database;
using osu.Game.Models;
using Realms;
#nullable enable #nullable enable
@ -33,6 +36,37 @@ namespace osu.Game.Tests.Database
}); });
} }
[Test]
public void TestNestedContextCreation()
{
RunTestWithRealm((realmFactory, _) =>
{
var mainContext = realmFactory.Context;
bool callbackRan = false;
var subscription = mainContext.All<RealmBeatmap>().SubscribeForNotifications((sender, changes, error) =>
{
realmFactory.CreateContext();
callbackRan = true;
});
Task.Factory.StartNew(() =>
{
using (var threadContext = realmFactory.CreateContext())
{
threadContext.Write(r => r.Add(new RealmBeatmap(CreateRuleset(), new RealmBeatmapDifficulty(), new RealmBeatmapMetadata())));
}
}, TaskCreationOptions.LongRunning | TaskCreationOptions.HideScheduler).Wait();
// will create a context but also run the callback above (Refresh is implicitly run when getting a new context).
realmFactory.CreateContext();
Assert.IsTrue(callbackRan);
subscription.Dispose();
});
}
[Test] [Test]
public void TestBlockOperationsWithContention() public void TestBlockOperationsWithContention()
{ {