diff --git a/osu.Game.Tests/Database/GeneralUsageTests.cs b/osu.Game.Tests/Database/GeneralUsageTests.cs index 3e8b6091fd..fec1a0dedd 100644 --- a/osu.Game.Tests/Database/GeneralUsageTests.cs +++ b/osu.Game.Tests/Database/GeneralUsageTests.cs @@ -5,6 +5,9 @@ using System; using System.Threading; using System.Threading.Tasks; using NUnit.Framework; +using osu.Game.Database; +using osu.Game.Models; +using Realms; #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().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] public void TestBlockOperationsWithContention() {