2021-10-13 06:45:01 +00: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.
|
|
|
|
|
2021-09-30 14:59:26 +00:00
|
|
|
using System;
|
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using NUnit.Framework;
|
2021-11-19 10:07:21 +00:00
|
|
|
using osu.Game.Beatmaps;
|
2021-12-01 06:09:51 +00:00
|
|
|
using osu.Game.Database;
|
2021-09-30 14:59:26 +00:00
|
|
|
|
|
|
|
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()
|
|
|
|
{
|
2022-01-24 10:59:58 +00:00
|
|
|
RunTestWithRealm((realm, _) => { realm.Run(r => r.Refresh()); });
|
2021-09-30 14:59:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void TestBlockOperations()
|
|
|
|
{
|
2022-01-24 10:59:58 +00:00
|
|
|
RunTestWithRealm((realm, _) =>
|
2021-09-30 14:59:26 +00:00
|
|
|
{
|
2022-01-24 10:59:58 +00:00
|
|
|
using (realm.BlockAllOperations())
|
2021-09-30 14:59:26 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-11-29 09:54:01 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Test to ensure that a `CreateContext` call nested inside a subscription doesn't cause any deadlocks
|
|
|
|
/// due to context fetching semaphores.
|
|
|
|
/// </summary>
|
2021-11-29 07:27:16 +00:00
|
|
|
[Test]
|
2021-11-29 09:54:01 +00:00
|
|
|
public void TestNestedContextCreationWithSubscription()
|
2021-11-29 07:27:16 +00:00
|
|
|
{
|
2022-01-24 10:59:58 +00:00
|
|
|
RunTestWithRealm((realm, _) =>
|
2021-11-29 07:27:16 +00:00
|
|
|
{
|
|
|
|
bool callbackRan = false;
|
|
|
|
|
2022-01-24 10:59:58 +00:00
|
|
|
realm.RegisterCustomSubscription(r =>
|
2021-11-29 07:27:16 +00:00
|
|
|
{
|
2022-01-24 10:59:58 +00:00
|
|
|
var subscription = r.All<BeatmapInfo>().QueryAsyncWithNotifications((sender, changes, error) =>
|
2021-11-29 09:54:01 +00:00
|
|
|
{
|
2022-01-24 10:59:58 +00:00
|
|
|
realm.Run(_ =>
|
2021-11-29 09:54:01 +00:00
|
|
|
{
|
|
|
|
callbackRan = true;
|
2022-01-21 08:08:20 +00:00
|
|
|
});
|
2021-11-29 09:54:01 +00:00
|
|
|
});
|
2021-11-29 07:27:16 +00:00
|
|
|
|
2021-11-29 09:54:01 +00:00
|
|
|
// Force the callback above to run.
|
2022-01-24 10:59:58 +00:00
|
|
|
realm.Run(rr => rr.Refresh());
|
2021-11-29 07:27:16 +00:00
|
|
|
|
2021-12-01 06:09:51 +00:00
|
|
|
subscription?.Dispose();
|
2022-01-21 10:39:49 +00:00
|
|
|
return null;
|
2022-01-21 08:08:20 +00:00
|
|
|
});
|
2021-11-29 07:27:16 +00:00
|
|
|
|
|
|
|
Assert.IsTrue(callbackRan);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-09-30 14:59:26 +00:00
|
|
|
[Test]
|
|
|
|
public void TestBlockOperationsWithContention()
|
|
|
|
{
|
2022-01-24 10:59:58 +00:00
|
|
|
RunTestWithRealm((realm, _) =>
|
2021-09-30 14:59:26 +00:00
|
|
|
{
|
|
|
|
ManualResetEventSlim stopThreadedUsage = new ManualResetEventSlim();
|
|
|
|
ManualResetEventSlim hasThreadedUsage = new ManualResetEventSlim();
|
|
|
|
|
|
|
|
Task.Factory.StartNew(() =>
|
|
|
|
{
|
2022-01-24 10:59:58 +00:00
|
|
|
realm.Run(_ =>
|
2021-09-30 14:59:26 +00:00
|
|
|
{
|
|
|
|
hasThreadedUsage.Set();
|
|
|
|
|
|
|
|
stopThreadedUsage.Wait();
|
2022-01-21 08:08:20 +00:00
|
|
|
});
|
2021-09-30 14:59:26 +00:00
|
|
|
}, TaskCreationOptions.LongRunning | TaskCreationOptions.HideScheduler);
|
|
|
|
|
|
|
|
hasThreadedUsage.Wait();
|
|
|
|
|
|
|
|
Assert.Throws<TimeoutException>(() =>
|
|
|
|
{
|
2022-01-24 10:59:58 +00:00
|
|
|
using (realm.BlockAllOperations())
|
2021-09-30 14:59:26 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
stopThreadedUsage.Set();
|
2022-01-25 05:00:58 +00:00
|
|
|
|
|
|
|
// Ensure we can block a second time after the usage has ended.
|
|
|
|
using (realm.BlockAllOperations())
|
|
|
|
{
|
|
|
|
}
|
2021-09-30 14:59:26 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|