2021-09-30 14:46:16 +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.
|
|
|
|
|
|
|
|
using System;
|
|
|
|
using System.Diagnostics;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using NUnit.Framework;
|
2022-01-03 08:02:15 +00:00
|
|
|
using osu.Framework.Extensions;
|
2021-12-14 04:53:23 +00:00
|
|
|
using osu.Framework.Testing;
|
2021-11-19 10:07:21 +00:00
|
|
|
using osu.Game.Beatmaps;
|
2021-09-30 14:46:16 +00:00
|
|
|
using osu.Game.Database;
|
|
|
|
using Realms;
|
|
|
|
|
|
|
|
namespace osu.Game.Tests.Database
|
|
|
|
{
|
|
|
|
public class RealmLiveTests : RealmTest
|
|
|
|
{
|
2021-10-01 05:30:27 +00:00
|
|
|
[Test]
|
2021-11-26 05:38:39 +00:00
|
|
|
public void TestLiveEquality()
|
2021-10-01 05:30:27 +00:00
|
|
|
{
|
2022-01-24 10:59:58 +00:00
|
|
|
RunTestWithRealm((realm, _) =>
|
2021-10-01 05:30:27 +00:00
|
|
|
{
|
2022-01-26 04:37:33 +00:00
|
|
|
Live<BeatmapInfo> beatmap = realm.Run(r => r.Write(_ => r.Add(new BeatmapInfo(CreateRuleset(), new BeatmapDifficulty(), new BeatmapMetadata()))).ToLive(realm));
|
2021-10-01 05:30:27 +00:00
|
|
|
|
2022-01-26 04:37:33 +00:00
|
|
|
Live<BeatmapInfo> beatmap2 = realm.Run(r => r.All<BeatmapInfo>().First().ToLive(realm));
|
2021-10-01 05:30:27 +00:00
|
|
|
|
2021-11-26 05:38:39 +00:00
|
|
|
Assert.AreEqual(beatmap, beatmap2);
|
2021-10-01 05:30:27 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-12-14 04:53:23 +00:00
|
|
|
[Test]
|
|
|
|
public void TestAccessAfterStorageMigrate()
|
|
|
|
{
|
2022-01-24 10:59:58 +00:00
|
|
|
RunTestWithRealm((realm, storage) =>
|
2021-12-14 04:53:23 +00:00
|
|
|
{
|
2021-11-19 10:07:21 +00:00
|
|
|
var beatmap = new BeatmapInfo(CreateRuleset(), new BeatmapDifficulty(), new BeatmapMetadata());
|
2021-12-14 04:53:23 +00:00
|
|
|
|
2022-01-26 04:37:33 +00:00
|
|
|
Live<BeatmapInfo>? liveBeatmap = null;
|
2021-12-14 04:53:23 +00:00
|
|
|
|
2022-01-24 10:59:58 +00:00
|
|
|
realm.Run(r =>
|
2021-12-14 04:53:23 +00:00
|
|
|
{
|
2022-01-24 10:59:58 +00:00
|
|
|
r.Write(_ => r.Add(beatmap));
|
2021-12-14 04:53:23 +00:00
|
|
|
|
2022-01-24 10:59:58 +00:00
|
|
|
liveBeatmap = beatmap.ToLive(realm);
|
2022-01-21 08:08:20 +00:00
|
|
|
});
|
2021-12-14 04:53:23 +00:00
|
|
|
|
|
|
|
using (var migratedStorage = new TemporaryNativeStorage("realm-test-migration-target"))
|
|
|
|
{
|
|
|
|
migratedStorage.DeleteDirectory(string.Empty);
|
|
|
|
|
2022-01-26 10:03:26 +00:00
|
|
|
using (realm.BlockAllOperations())
|
|
|
|
{
|
|
|
|
storage.Migrate(migratedStorage);
|
|
|
|
}
|
2021-12-14 04:53:23 +00:00
|
|
|
|
2022-01-21 08:08:20 +00:00
|
|
|
Assert.IsFalse(liveBeatmap?.PerformRead(l => l.Hidden));
|
2021-12-14 04:53:23 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-11-29 08:34:56 +00:00
|
|
|
[Test]
|
|
|
|
public void TestAccessAfterAttach()
|
|
|
|
{
|
2022-01-24 10:59:58 +00:00
|
|
|
RunTestWithRealm((realm, _) =>
|
2021-11-29 08:34:56 +00:00
|
|
|
{
|
2021-11-19 10:07:21 +00:00
|
|
|
var beatmap = new BeatmapInfo(CreateRuleset(), new BeatmapDifficulty(), new BeatmapMetadata());
|
2021-11-29 08:34:56 +00:00
|
|
|
|
2022-01-24 10:59:58 +00:00
|
|
|
var liveBeatmap = beatmap.ToLive(realm);
|
2021-11-29 08:34:56 +00:00
|
|
|
|
2022-01-24 10:59:58 +00:00
|
|
|
realm.Run(r => r.Write(_ => r.Add(beatmap)));
|
2021-11-29 08:34:56 +00:00
|
|
|
|
|
|
|
Assert.IsFalse(liveBeatmap.PerformRead(l => l.Hidden));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-11-26 05:39:35 +00:00
|
|
|
[Test]
|
|
|
|
public void TestAccessNonManaged()
|
|
|
|
{
|
2021-11-19 10:07:21 +00:00
|
|
|
var beatmap = new BeatmapInfo(CreateRuleset(), new BeatmapDifficulty(), new BeatmapMetadata());
|
2021-12-14 05:21:23 +00:00
|
|
|
var liveBeatmap = beatmap.ToLiveUnmanaged();
|
2021-11-26 05:39:35 +00:00
|
|
|
|
|
|
|
Assert.IsFalse(beatmap.Hidden);
|
|
|
|
Assert.IsFalse(liveBeatmap.Value.Hidden);
|
|
|
|
Assert.IsFalse(liveBeatmap.PerformRead(l => l.Hidden));
|
|
|
|
|
|
|
|
Assert.Throws<InvalidOperationException>(() => liveBeatmap.PerformWrite(l => l.Hidden = true));
|
|
|
|
|
|
|
|
Assert.IsFalse(beatmap.Hidden);
|
|
|
|
Assert.IsFalse(liveBeatmap.Value.Hidden);
|
|
|
|
Assert.IsFalse(liveBeatmap.PerformRead(l => l.Hidden));
|
|
|
|
}
|
|
|
|
|
2021-09-30 14:46:16 +00:00
|
|
|
[Test]
|
2021-11-30 02:55:13 +00:00
|
|
|
public void TestScopedReadWithoutContext()
|
2021-09-30 14:46:16 +00:00
|
|
|
{
|
2022-01-24 10:59:58 +00:00
|
|
|
RunTestWithRealm((realm, _) =>
|
2021-09-30 14:46:16 +00:00
|
|
|
{
|
2022-01-26 04:37:33 +00:00
|
|
|
Live<BeatmapInfo>? liveBeatmap = null;
|
2021-09-30 14:46:16 +00:00
|
|
|
Task.Factory.StartNew(() =>
|
|
|
|
{
|
2022-01-24 10:59:58 +00:00
|
|
|
realm.Run(threadContext =>
|
2021-09-30 14:46:16 +00:00
|
|
|
{
|
2021-11-19 10:07:21 +00:00
|
|
|
var beatmap = threadContext.Write(r => r.Add(new BeatmapInfo(CreateRuleset(), new BeatmapDifficulty(), new BeatmapMetadata())));
|
2021-09-30 14:46:16 +00:00
|
|
|
|
2022-01-24 10:59:58 +00:00
|
|
|
liveBeatmap = beatmap.ToLive(realm);
|
2022-01-21 08:08:20 +00:00
|
|
|
});
|
2022-01-03 08:02:15 +00:00
|
|
|
}, TaskCreationOptions.LongRunning | TaskCreationOptions.HideScheduler).WaitSafely();
|
2021-09-30 14:46:16 +00:00
|
|
|
|
|
|
|
Debug.Assert(liveBeatmap != null);
|
|
|
|
|
|
|
|
Task.Factory.StartNew(() =>
|
|
|
|
{
|
2021-11-30 02:55:13 +00:00
|
|
|
liveBeatmap.PerformRead(beatmap =>
|
2021-09-30 14:46:16 +00:00
|
|
|
{
|
2021-11-30 02:55:13 +00:00
|
|
|
Assert.IsTrue(beatmap.IsValid);
|
|
|
|
Assert.IsFalse(beatmap.Hidden);
|
|
|
|
});
|
2022-01-03 08:02:15 +00:00
|
|
|
}, TaskCreationOptions.LongRunning | TaskCreationOptions.HideScheduler).WaitSafely();
|
2021-09-30 14:46:16 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
2021-11-30 02:55:13 +00:00
|
|
|
public void TestScopedWriteWithoutContext()
|
2021-09-30 14:46:16 +00:00
|
|
|
{
|
2022-01-24 10:59:58 +00:00
|
|
|
RunTestWithRealm((realm, _) =>
|
2021-09-30 14:46:16 +00:00
|
|
|
{
|
2022-01-26 04:37:33 +00:00
|
|
|
Live<BeatmapInfo>? liveBeatmap = null;
|
2021-09-30 14:46:16 +00:00
|
|
|
Task.Factory.StartNew(() =>
|
|
|
|
{
|
2022-01-24 10:59:58 +00:00
|
|
|
realm.Run(threadContext =>
|
2021-09-30 14:46:16 +00:00
|
|
|
{
|
2021-11-19 10:07:21 +00:00
|
|
|
var beatmap = threadContext.Write(r => r.Add(new BeatmapInfo(CreateRuleset(), new BeatmapDifficulty(), new BeatmapMetadata())));
|
2021-09-30 14:46:16 +00:00
|
|
|
|
2022-01-24 10:59:58 +00:00
|
|
|
liveBeatmap = beatmap.ToLive(realm);
|
2022-01-21 08:08:20 +00:00
|
|
|
});
|
2022-01-03 08:02:15 +00:00
|
|
|
}, TaskCreationOptions.LongRunning | TaskCreationOptions.HideScheduler).WaitSafely();
|
2021-09-30 14:46:16 +00:00
|
|
|
|
|
|
|
Debug.Assert(liveBeatmap != null);
|
|
|
|
|
|
|
|
Task.Factory.StartNew(() =>
|
|
|
|
{
|
2021-11-30 02:55:13 +00:00
|
|
|
liveBeatmap.PerformWrite(beatmap => { beatmap.Hidden = true; });
|
|
|
|
liveBeatmap.PerformRead(beatmap => { Assert.IsTrue(beatmap.Hidden); });
|
2022-01-03 08:02:15 +00:00
|
|
|
}, TaskCreationOptions.LongRunning | TaskCreationOptions.HideScheduler).WaitSafely();
|
2021-09-30 14:46:16 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
2021-11-30 02:55:13 +00:00
|
|
|
public void TestValueAccessNonManaged()
|
|
|
|
{
|
2022-01-24 10:59:58 +00:00
|
|
|
RunTestWithRealm((realm, _) =>
|
2021-11-30 02:55:13 +00:00
|
|
|
{
|
2021-11-19 10:07:21 +00:00
|
|
|
var beatmap = new BeatmapInfo(CreateRuleset(), new BeatmapDifficulty(), new BeatmapMetadata());
|
2022-01-24 10:59:58 +00:00
|
|
|
var liveBeatmap = beatmap.ToLive(realm);
|
2021-11-30 02:55:13 +00:00
|
|
|
|
|
|
|
Assert.DoesNotThrow(() =>
|
|
|
|
{
|
|
|
|
var __ = liveBeatmap.Value;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void TestValueAccessWithOpenContextFails()
|
2021-09-30 14:46:16 +00:00
|
|
|
{
|
2022-01-24 10:59:58 +00:00
|
|
|
RunTestWithRealm((realm, _) =>
|
2021-09-30 14:46:16 +00:00
|
|
|
{
|
2022-01-26 04:37:33 +00:00
|
|
|
Live<BeatmapInfo>? liveBeatmap = null;
|
2021-11-30 03:02:35 +00:00
|
|
|
|
2021-09-30 14:46:16 +00:00
|
|
|
Task.Factory.StartNew(() =>
|
|
|
|
{
|
2022-01-24 10:59:58 +00:00
|
|
|
realm.Run(threadContext =>
|
2021-09-30 14:46:16 +00:00
|
|
|
{
|
2021-11-19 10:07:21 +00:00
|
|
|
var beatmap = threadContext.Write(r => r.Add(new BeatmapInfo(CreateRuleset(), new BeatmapDifficulty(), new BeatmapMetadata())));
|
2021-09-30 14:46:16 +00:00
|
|
|
|
2022-01-24 10:59:58 +00:00
|
|
|
liveBeatmap = beatmap.ToLive(realm);
|
2022-01-21 08:08:20 +00:00
|
|
|
});
|
2022-01-03 08:02:15 +00:00
|
|
|
}, TaskCreationOptions.LongRunning | TaskCreationOptions.HideScheduler).WaitSafely();
|
2021-09-30 14:46:16 +00:00
|
|
|
|
|
|
|
Debug.Assert(liveBeatmap != null);
|
|
|
|
|
|
|
|
Task.Factory.StartNew(() =>
|
|
|
|
{
|
2021-11-30 02:55:13 +00:00
|
|
|
// Can't be used, without a valid context.
|
|
|
|
Assert.Throws<InvalidOperationException>(() =>
|
|
|
|
{
|
|
|
|
var __ = liveBeatmap.Value;
|
|
|
|
});
|
|
|
|
|
|
|
|
// Can't be used, even from within a valid context.
|
2022-01-24 10:59:58 +00:00
|
|
|
realm.Run(threadContext =>
|
2021-11-30 02:55:13 +00:00
|
|
|
{
|
|
|
|
Assert.Throws<InvalidOperationException>(() =>
|
|
|
|
{
|
|
|
|
var __ = liveBeatmap.Value;
|
|
|
|
});
|
2022-01-21 08:08:20 +00:00
|
|
|
});
|
2022-01-03 08:02:15 +00:00
|
|
|
}, TaskCreationOptions.LongRunning | TaskCreationOptions.HideScheduler).WaitSafely();
|
2021-09-30 14:46:16 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void TestValueAccessWithoutOpenContextFails()
|
|
|
|
{
|
2022-01-24 10:59:58 +00:00
|
|
|
RunTestWithRealm((realm, _) =>
|
2021-09-30 14:46:16 +00:00
|
|
|
{
|
2022-01-26 04:37:33 +00:00
|
|
|
Live<BeatmapInfo>? liveBeatmap = null;
|
2021-09-30 14:46:16 +00:00
|
|
|
Task.Factory.StartNew(() =>
|
|
|
|
{
|
2022-01-24 10:59:58 +00:00
|
|
|
realm.Run(threadContext =>
|
2021-09-30 14:46:16 +00:00
|
|
|
{
|
2021-11-19 10:07:21 +00:00
|
|
|
var beatmap = threadContext.Write(r => r.Add(new BeatmapInfo(CreateRuleset(), new BeatmapDifficulty(), new BeatmapMetadata())));
|
2021-09-30 14:46:16 +00:00
|
|
|
|
2022-01-24 10:59:58 +00:00
|
|
|
liveBeatmap = beatmap.ToLive(realm);
|
2022-01-21 08:08:20 +00:00
|
|
|
});
|
2022-01-03 08:02:15 +00:00
|
|
|
}, TaskCreationOptions.LongRunning | TaskCreationOptions.HideScheduler).WaitSafely();
|
2021-09-30 14:46:16 +00:00
|
|
|
|
|
|
|
Debug.Assert(liveBeatmap != null);
|
|
|
|
|
|
|
|
Task.Factory.StartNew(() =>
|
|
|
|
{
|
|
|
|
Assert.Throws<InvalidOperationException>(() =>
|
|
|
|
{
|
|
|
|
var unused = liveBeatmap.Value;
|
|
|
|
});
|
2022-01-03 08:02:15 +00:00
|
|
|
}, TaskCreationOptions.LongRunning | TaskCreationOptions.HideScheduler).WaitSafely();
|
2021-09-30 14:46:16 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void TestLiveAssumptions()
|
|
|
|
{
|
2022-01-24 10:59:58 +00:00
|
|
|
RunTestWithRealm((realm, _) =>
|
2021-09-30 14:46:16 +00:00
|
|
|
{
|
|
|
|
int changesTriggered = 0;
|
|
|
|
|
2022-01-24 10:59:58 +00:00
|
|
|
realm.RegisterCustomSubscription(outerRealm =>
|
2021-09-30 14:46:16 +00:00
|
|
|
{
|
2022-01-21 08:08:20 +00:00
|
|
|
outerRealm.All<BeatmapInfo>().QueryAsyncWithNotifications(gotChange);
|
2022-01-26 04:37:33 +00:00
|
|
|
Live<BeatmapInfo>? liveBeatmap = null;
|
2021-09-30 14:46:16 +00:00
|
|
|
|
|
|
|
Task.Factory.StartNew(() =>
|
|
|
|
{
|
2022-01-24 10:59:58 +00:00
|
|
|
realm.Run(innerRealm =>
|
2021-09-30 14:46:16 +00:00
|
|
|
{
|
|
|
|
var ruleset = CreateRuleset();
|
2022-01-21 08:08:20 +00:00
|
|
|
var beatmap = innerRealm.Write(r => r.Add(new BeatmapInfo(ruleset, new BeatmapDifficulty(), new BeatmapMetadata())));
|
2021-09-30 14:46:16 +00:00
|
|
|
|
|
|
|
// add a second beatmap to ensure that a full refresh occurs below.
|
|
|
|
// not just a refresh from the resolved Live.
|
2022-01-21 08:08:20 +00:00
|
|
|
innerRealm.Write(r => r.Add(new BeatmapInfo(ruleset, new BeatmapDifficulty(), new BeatmapMetadata())));
|
2021-09-30 14:46:16 +00:00
|
|
|
|
2022-01-24 10:59:58 +00:00
|
|
|
liveBeatmap = beatmap.ToLive(realm);
|
2022-01-21 08:08:20 +00:00
|
|
|
});
|
2022-01-03 08:02:15 +00:00
|
|
|
}, TaskCreationOptions.LongRunning | TaskCreationOptions.HideScheduler).WaitSafely();
|
2021-09-30 14:46:16 +00:00
|
|
|
|
|
|
|
Debug.Assert(liveBeatmap != null);
|
|
|
|
|
|
|
|
// not yet seen by main context
|
2022-01-21 08:08:20 +00:00
|
|
|
Assert.AreEqual(0, outerRealm.All<BeatmapInfo>().Count());
|
2021-09-30 14:46:16 +00:00
|
|
|
Assert.AreEqual(0, changesTriggered);
|
|
|
|
|
2021-11-29 06:14:27 +00:00
|
|
|
liveBeatmap.PerformRead(resolved =>
|
|
|
|
{
|
|
|
|
// retrieval causes an implicit refresh. even changes that aren't related to the retrieval are fired at this point.
|
|
|
|
// ReSharper disable once AccessToDisposedClosure
|
2022-01-21 08:08:20 +00:00
|
|
|
Assert.AreEqual(2, outerRealm.All<BeatmapInfo>().Count());
|
2021-11-29 06:14:27 +00:00
|
|
|
Assert.AreEqual(1, changesTriggered);
|
2021-09-30 14:46:16 +00:00
|
|
|
|
2021-11-29 06:14:27 +00:00
|
|
|
// can access properties without a crash.
|
|
|
|
Assert.IsFalse(resolved.Hidden);
|
2021-09-30 14:46:16 +00:00
|
|
|
|
2021-11-29 06:14:27 +00:00
|
|
|
// ReSharper disable once AccessToDisposedClosure
|
2022-01-21 08:08:20 +00:00
|
|
|
outerRealm.Write(r =>
|
2021-11-29 06:14:27 +00:00
|
|
|
{
|
|
|
|
// can use with the main context.
|
|
|
|
r.Remove(resolved);
|
|
|
|
});
|
2021-09-30 14:46:16 +00:00
|
|
|
});
|
2022-01-21 10:39:49 +00:00
|
|
|
|
|
|
|
return null;
|
2022-01-21 08:08:20 +00:00
|
|
|
});
|
2021-09-30 14:46:16 +00:00
|
|
|
|
2021-11-19 10:07:21 +00:00
|
|
|
void gotChange(IRealmCollection<BeatmapInfo> sender, ChangeSet changes, Exception error)
|
2021-09-30 14:46:16 +00:00
|
|
|
{
|
|
|
|
changesTriggered++;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|