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.
|
|
|
|
|
2021-09-30 22:59:26 +08:00
|
|
|
using System;
|
2022-06-27 18:34:42 +08:00
|
|
|
using System.Linq;
|
2021-09-30 22:59:26 +08:00
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using NUnit.Framework;
|
2022-06-27 18:34:42 +08:00
|
|
|
using osu.Framework.Extensions;
|
2021-11-19 18:07:21 +08:00
|
|
|
using osu.Game.Beatmaps;
|
2021-12-01 14:09:51 +08:00
|
|
|
using osu.Game.Database;
|
2022-06-27 18:34:42 +08:00
|
|
|
using osu.Game.Tests.Resources;
|
2021-09-30 22:59:26 +08: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 18:59:58 +08:00
|
|
|
RunTestWithRealm((realm, _) => { realm.Run(r => r.Refresh()); });
|
2021-09-30 22:59:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void TestBlockOperations()
|
|
|
|
{
|
2022-01-24 18:59:58 +08:00
|
|
|
RunTestWithRealm((realm, _) =>
|
2021-09-30 22:59:26 +08:00
|
|
|
{
|
2022-07-02 11:35:29 +08:00
|
|
|
using (realm.BlockAllOperations("testing"))
|
2021-09-30 22:59:26 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-06-27 18:34:42 +08:00
|
|
|
[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));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-06-28 16:07:49 +08:00
|
|
|
[Test]
|
|
|
|
public void TestAsyncWriteWhileBlocking()
|
|
|
|
{
|
|
|
|
RunTestWithRealm((realm, _) =>
|
|
|
|
{
|
|
|
|
Task writeTask;
|
|
|
|
|
2022-07-02 11:35:29 +08:00
|
|
|
using (realm.BlockAllOperations("testing"))
|
2022-06-28 16:07:49 +08:00
|
|
|
{
|
|
|
|
writeTask = realm.WriteAsync(r => r.Add(TestResources.CreateTestBeatmapSetInfo()));
|
|
|
|
Thread.Sleep(100);
|
|
|
|
Assert.That(writeTask.IsCompleted, Is.False);
|
|
|
|
}
|
|
|
|
|
|
|
|
writeTask.WaitSafely();
|
|
|
|
|
|
|
|
realm.Run(r => r.Refresh());
|
|
|
|
Assert.That(realm.Run(r => r.All<BeatmapSetInfo>().Count()), Is.EqualTo(1));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-06-27 18:34:42 +08:00
|
|
|
[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();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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>
|
2021-11-29 15:27:16 +08:00
|
|
|
[Test]
|
2021-11-29 17:54:01 +08:00
|
|
|
public void TestNestedContextCreationWithSubscription()
|
2021-11-29 15:27:16 +08:00
|
|
|
{
|
2022-01-24 18:59:58 +08:00
|
|
|
RunTestWithRealm((realm, _) =>
|
2021-11-29 15:27:16 +08:00
|
|
|
{
|
|
|
|
bool callbackRan = false;
|
|
|
|
|
2022-01-24 18:59:58 +08:00
|
|
|
realm.RegisterCustomSubscription(r =>
|
2021-11-29 15:27:16 +08:00
|
|
|
{
|
2022-06-24 20:25:23 +08:00
|
|
|
var subscription = r.All<BeatmapInfo>().QueryAsyncWithNotifications((_, _, _) =>
|
2021-11-29 17:54:01 +08:00
|
|
|
{
|
2022-01-24 18:59:58 +08:00
|
|
|
realm.Run(_ =>
|
2021-11-29 17:54:01 +08:00
|
|
|
{
|
|
|
|
callbackRan = true;
|
2022-01-21 16:08:20 +08:00
|
|
|
});
|
2021-11-29 17:54:01 +08:00
|
|
|
});
|
2021-11-29 15:27:16 +08:00
|
|
|
|
2021-11-29 17:54:01 +08:00
|
|
|
// Force the callback above to run.
|
2022-01-24 18:59:58 +08:00
|
|
|
realm.Run(rr => rr.Refresh());
|
2021-11-29 15:27:16 +08:00
|
|
|
|
2021-12-01 14:09:51 +08:00
|
|
|
subscription?.Dispose();
|
2022-01-21 18:39:49 +08:00
|
|
|
return null;
|
2022-01-21 16:08:20 +08:00
|
|
|
});
|
2021-11-29 15:27:16 +08:00
|
|
|
|
|
|
|
Assert.IsTrue(callbackRan);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-09-30 22:59:26 +08:00
|
|
|
[Test]
|
|
|
|
public void TestBlockOperationsWithContention()
|
|
|
|
{
|
2022-01-24 18:59:58 +08:00
|
|
|
RunTestWithRealm((realm, _) =>
|
2021-09-30 22:59:26 +08:00
|
|
|
{
|
|
|
|
ManualResetEventSlim stopThreadedUsage = new ManualResetEventSlim();
|
|
|
|
ManualResetEventSlim hasThreadedUsage = new ManualResetEventSlim();
|
|
|
|
|
|
|
|
Task.Factory.StartNew(() =>
|
|
|
|
{
|
2022-01-24 18:59:58 +08:00
|
|
|
realm.Run(_ =>
|
2021-09-30 22:59:26 +08:00
|
|
|
{
|
|
|
|
hasThreadedUsage.Set();
|
|
|
|
|
2022-06-23 13:33:20 +08:00
|
|
|
stopThreadedUsage.Wait(60000);
|
2022-01-21 16:08:20 +08:00
|
|
|
});
|
2021-09-30 22:59:26 +08:00
|
|
|
}, TaskCreationOptions.LongRunning | TaskCreationOptions.HideScheduler);
|
|
|
|
|
2022-06-23 13:33:20 +08:00
|
|
|
hasThreadedUsage.Wait(60000);
|
2021-09-30 22:59:26 +08:00
|
|
|
|
|
|
|
Assert.Throws<TimeoutException>(() =>
|
|
|
|
{
|
2022-07-02 11:35:29 +08:00
|
|
|
using (realm.BlockAllOperations("testing"))
|
2021-09-30 22:59:26 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
stopThreadedUsage.Set();
|
2022-01-25 13:00:58 +08:00
|
|
|
|
|
|
|
// Ensure we can block a second time after the usage has ended.
|
2022-07-02 11:35:29 +08:00
|
|
|
using (realm.BlockAllOperations("testing"))
|
2022-01-25 13:00:58 +08:00
|
|
|
{
|
|
|
|
}
|
2021-09-30 22:59:26 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|