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