2022-01-23 19:28:13 +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.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
using NUnit.Framework;
|
|
|
|
using osu.Framework.Allocation;
|
|
|
|
using osu.Game.Beatmaps;
|
|
|
|
using osu.Game.Rulesets;
|
|
|
|
using osu.Game.Tests.Resources;
|
|
|
|
using Realms;
|
|
|
|
|
|
|
|
#nullable enable
|
|
|
|
|
|
|
|
namespace osu.Game.Tests.Database
|
|
|
|
{
|
|
|
|
[TestFixture]
|
|
|
|
public class RealmSubscriptionRegistrationTests : RealmTest
|
|
|
|
{
|
|
|
|
[Test]
|
|
|
|
public void TestSubscriptionWithContextLoss()
|
|
|
|
{
|
|
|
|
IEnumerable<BeatmapSetInfo>? resolvedItems = null;
|
|
|
|
ChangeSet? lastChanges = null;
|
|
|
|
|
2022-01-24 18:59:58 +08:00
|
|
|
RunTestWithRealm((realm, _) =>
|
2022-01-23 19:28:13 +08:00
|
|
|
{
|
2022-01-24 18:59:58 +08:00
|
|
|
realm.Write(r => r.Add(TestResources.CreateTestBeatmapSetInfo()));
|
2022-01-23 19:28:13 +08:00
|
|
|
|
2022-01-24 18:59:58 +08:00
|
|
|
var registration = realm.RegisterForNotifications(r => r.All<BeatmapSetInfo>(), onChanged);
|
2022-01-23 19:28:13 +08:00
|
|
|
|
|
|
|
testEventsArriving(true);
|
|
|
|
|
|
|
|
// All normal until here.
|
|
|
|
// Now let's yank the main realm context.
|
|
|
|
resolvedItems = null;
|
|
|
|
lastChanges = null;
|
|
|
|
|
2022-01-24 18:59:58 +08:00
|
|
|
using (realm.BlockAllOperations())
|
2022-01-23 19:28:13 +08:00
|
|
|
Assert.That(resolvedItems, Is.Empty);
|
|
|
|
|
2022-01-24 18:59:58 +08:00
|
|
|
realm.Write(r => r.Add(TestResources.CreateTestBeatmapSetInfo()));
|
2022-01-23 19:28:13 +08:00
|
|
|
|
|
|
|
testEventsArriving(true);
|
|
|
|
|
|
|
|
// Now let's try unsubscribing.
|
|
|
|
resolvedItems = null;
|
|
|
|
lastChanges = null;
|
|
|
|
|
|
|
|
registration.Dispose();
|
|
|
|
|
2022-01-24 18:59:58 +08:00
|
|
|
realm.Write(r => r.Add(TestResources.CreateTestBeatmapSetInfo()));
|
2022-01-23 19:28:13 +08:00
|
|
|
|
|
|
|
testEventsArriving(false);
|
|
|
|
|
|
|
|
// And make sure even after another context loss we don't get firings.
|
2022-01-24 18:59:58 +08:00
|
|
|
using (realm.BlockAllOperations())
|
2022-01-23 19:28:13 +08:00
|
|
|
Assert.That(resolvedItems, Is.Null);
|
|
|
|
|
2022-01-24 18:59:58 +08:00
|
|
|
realm.Write(r => r.Add(TestResources.CreateTestBeatmapSetInfo()));
|
2022-01-23 19:28:13 +08:00
|
|
|
|
|
|
|
testEventsArriving(false);
|
|
|
|
|
|
|
|
void testEventsArriving(bool shouldArrive)
|
|
|
|
{
|
2022-01-24 18:59:58 +08:00
|
|
|
realm.Run(r => r.Refresh());
|
2022-01-23 19:28:13 +08:00
|
|
|
|
|
|
|
if (shouldArrive)
|
|
|
|
Assert.That(resolvedItems, Has.One.Items);
|
|
|
|
else
|
|
|
|
Assert.That(resolvedItems, Is.Null);
|
|
|
|
|
2022-01-24 18:59:58 +08:00
|
|
|
realm.Write(r =>
|
2022-01-23 19:28:13 +08:00
|
|
|
{
|
2022-01-24 18:59:58 +08:00
|
|
|
r.RemoveAll<BeatmapSetInfo>();
|
|
|
|
r.RemoveAll<RulesetInfo>();
|
2022-01-23 19:28:13 +08:00
|
|
|
});
|
|
|
|
|
2022-01-24 18:59:58 +08:00
|
|
|
realm.Run(r => r.Refresh());
|
2022-01-23 19:28:13 +08:00
|
|
|
|
|
|
|
if (shouldArrive)
|
|
|
|
Assert.That(lastChanges?.DeletedIndices, Has.One.Items);
|
|
|
|
else
|
|
|
|
Assert.That(lastChanges, Is.Null);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
void onChanged(IRealmCollection<BeatmapSetInfo> sender, ChangeSet? changes, Exception error)
|
|
|
|
{
|
|
|
|
if (changes == null)
|
|
|
|
resolvedItems = sender;
|
|
|
|
|
|
|
|
lastChanges = changes;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void TestCustomRegisterWithContextLoss()
|
|
|
|
{
|
2022-01-24 18:59:58 +08:00
|
|
|
RunTestWithRealm((realm, _) =>
|
2022-01-23 19:28:13 +08:00
|
|
|
{
|
|
|
|
BeatmapSetInfo? beatmapSetInfo = null;
|
|
|
|
|
2022-01-24 18:59:58 +08:00
|
|
|
realm.Write(r => r.Add(TestResources.CreateTestBeatmapSetInfo()));
|
2022-01-23 19:28:13 +08:00
|
|
|
|
2022-01-24 18:59:58 +08:00
|
|
|
var subscription = realm.RegisterCustomSubscription(r =>
|
2022-01-23 19:28:13 +08:00
|
|
|
{
|
2022-01-24 18:59:58 +08:00
|
|
|
beatmapSetInfo = r.All<BeatmapSetInfo>().First();
|
2022-01-23 19:28:13 +08:00
|
|
|
|
|
|
|
return new InvokeOnDisposal(() => beatmapSetInfo = null);
|
|
|
|
});
|
|
|
|
|
|
|
|
Assert.That(beatmapSetInfo, Is.Not.Null);
|
|
|
|
|
2022-01-24 18:59:58 +08:00
|
|
|
using (realm.BlockAllOperations())
|
2022-01-23 19:28:13 +08:00
|
|
|
{
|
|
|
|
// custom disposal action fired when context lost.
|
|
|
|
Assert.That(beatmapSetInfo, Is.Null);
|
|
|
|
}
|
|
|
|
|
|
|
|
// re-registration after context restore.
|
2022-01-24 18:59:58 +08:00
|
|
|
realm.Run(r => r.Refresh());
|
2022-01-23 19:28:13 +08:00
|
|
|
Assert.That(beatmapSetInfo, Is.Not.Null);
|
|
|
|
|
|
|
|
subscription.Dispose();
|
|
|
|
|
|
|
|
Assert.That(beatmapSetInfo, Is.Null);
|
|
|
|
|
2022-01-24 18:59:58 +08:00
|
|
|
using (realm.BlockAllOperations())
|
2022-01-23 19:28:13 +08:00
|
|
|
Assert.That(beatmapSetInfo, Is.Null);
|
|
|
|
|
2022-01-24 18:59:58 +08:00
|
|
|
realm.Run(r => r.Refresh());
|
2022-01-23 19:28:13 +08:00
|
|
|
Assert.That(beatmapSetInfo, Is.Null);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|