1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-18 14:52:55 +08:00
osu-lazer/osu.Game.Tests/Database/GeneralUsageTests.cs

105 lines
3.0 KiB
C#
Raw Normal View History

2021-10-13 14:45:01 +08:00
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
using System;
using System.Threading;
using System.Threading.Tasks;
using NUnit.Framework;
using osu.Game.Beatmaps;
using osu.Game.Database;
namespace osu.Game.Tests.Database
{
[TestFixture]
public class GeneralUsageTests : RealmTest
{
/// <summary>
/// Just test the construction of a new database works.
/// </summary>
[Test]
public void TestConstructRealm()
{
RunTestWithRealm((realm, _) => { realm.Run(r => r.Refresh()); });
}
[Test]
public void TestBlockOperations()
{
RunTestWithRealm((realm, _) =>
{
using (realm.BlockAllOperations())
{
}
});
}
2021-11-29 17:54:01 +08:00
/// <summary>
/// Test to ensure that a `CreateContext` call nested inside a subscription doesn't cause any deadlocks
/// due to context fetching semaphores.
/// </summary>
[Test]
2021-11-29 17:54:01 +08:00
public void TestNestedContextCreationWithSubscription()
{
RunTestWithRealm((realm, _) =>
{
bool callbackRan = false;
realm.RegisterCustomSubscription(r =>
{
2022-06-24 20:25:23 +08:00
var subscription = r.All<BeatmapInfo>().QueryAsyncWithNotifications((_, _, _) =>
2021-11-29 17:54:01 +08:00
{
realm.Run(_ =>
2021-11-29 17:54:01 +08:00
{
callbackRan = true;
});
2021-11-29 17:54:01 +08:00
});
2021-11-29 17:54:01 +08:00
// Force the callback above to run.
realm.Run(rr => rr.Refresh());
subscription?.Dispose();
return null;
});
Assert.IsTrue(callbackRan);
});
}
[Test]
public void TestBlockOperationsWithContention()
{
RunTestWithRealm((realm, _) =>
{
ManualResetEventSlim stopThreadedUsage = new ManualResetEventSlim();
ManualResetEventSlim hasThreadedUsage = new ManualResetEventSlim();
Task.Factory.StartNew(() =>
{
realm.Run(_ =>
{
hasThreadedUsage.Set();
stopThreadedUsage.Wait(60000);
});
}, TaskCreationOptions.LongRunning | TaskCreationOptions.HideScheduler);
hasThreadedUsage.Wait(60000);
Assert.Throws<TimeoutException>(() =>
{
using (realm.BlockAllOperations())
{
}
});
stopThreadedUsage.Set();
// Ensure we can block a second time after the usage has ended.
using (realm.BlockAllOperations())
{
}
});
}
}
}