Update all usages of CreateContext to use either Run or Write

This commit is contained in:
Dean Herbert
2022-01-21 17:08:20 +09:00
parent da0a803e6c
commit 114c9e8c1f
22 changed files with 230 additions and 250 deletions

View File

@ -21,7 +21,7 @@ namespace osu.Game.Tests.Database
[Test]
public void TestConstructRealm()
{
RunTestWithRealm((realmFactory, _) => { realmFactory.CreateContext().Refresh(); });
RunTestWithRealm((realmFactory, _) => { realmFactory.Run(realm => realm.Refresh()); });
}
[Test]
@ -46,23 +46,21 @@ namespace osu.Game.Tests.Database
{
bool callbackRan = false;
using (var context = realmFactory.CreateContext())
realmFactory.Run(realm =>
{
var subscription = context.All<BeatmapInfo>().QueryAsyncWithNotifications((sender, changes, error) =>
var subscription = realm.All<BeatmapInfo>().QueryAsyncWithNotifications((sender, changes, error) =>
{
using (realmFactory.CreateContext())
realmFactory.Run(_ =>
{
callbackRan = true;
}
});
});
// Force the callback above to run.
using (realmFactory.CreateContext())
{
}
realmFactory.Run(r => r.Refresh());
subscription?.Dispose();
}
});
Assert.IsTrue(callbackRan);
});
@ -78,12 +76,12 @@ namespace osu.Game.Tests.Database
Task.Factory.StartNew(() =>
{
using (realmFactory.CreateContext())
realmFactory.Run(_ =>
{
hasThreadedUsage.Set();
stopThreadedUsage.Wait();
}
});
}, TaskCreationOptions.LongRunning | TaskCreationOptions.HideScheduler);
hasThreadedUsage.Wait();

View File

@ -23,9 +23,9 @@ namespace osu.Game.Tests.Database
{
RunTestWithRealm((realmFactory, _) =>
{
ILive<BeatmapInfo> beatmap = realmFactory.CreateContext().Write(r => r.Add(new BeatmapInfo(CreateRuleset(), new BeatmapDifficulty(), new BeatmapMetadata()))).ToLive(realmFactory);
ILive<BeatmapInfo> beatmap = realmFactory.Run(realm => realm.Write(r => r.Add(new BeatmapInfo(CreateRuleset(), new BeatmapDifficulty(), new BeatmapMetadata()))).ToLive(realmFactory));
ILive<BeatmapInfo> beatmap2 = realmFactory.CreateContext().All<BeatmapInfo>().First().ToLive(realmFactory);
ILive<BeatmapInfo> beatmap2 = realmFactory.Run(realm => realm.All<BeatmapInfo>().First().ToLive(realmFactory));
Assert.AreEqual(beatmap, beatmap2);
});
@ -38,14 +38,14 @@ namespace osu.Game.Tests.Database
{
var beatmap = new BeatmapInfo(CreateRuleset(), new BeatmapDifficulty(), new BeatmapMetadata());
ILive<BeatmapInfo> liveBeatmap;
ILive<BeatmapInfo>? liveBeatmap = null;
using (var context = realmFactory.CreateContext())
realmFactory.Run(realm =>
{
context.Write(r => r.Add(beatmap));
realm.Write(r => r.Add(beatmap));
liveBeatmap = beatmap.ToLive(realmFactory);
}
});
using (var migratedStorage = new TemporaryNativeStorage("realm-test-migration-target"))
{
@ -53,7 +53,7 @@ namespace osu.Game.Tests.Database
storage.Migrate(migratedStorage);
Assert.IsFalse(liveBeatmap.PerformRead(l => l.Hidden));
Assert.IsFalse(liveBeatmap?.PerformRead(l => l.Hidden));
}
});
}
@ -67,8 +67,7 @@ namespace osu.Game.Tests.Database
var liveBeatmap = beatmap.ToLive(realmFactory);
using (var context = realmFactory.CreateContext())
context.Write(r => r.Add(beatmap));
realmFactory.Run(realm => realm.Write(r => r.Add(beatmap)));
Assert.IsFalse(liveBeatmap.PerformRead(l => l.Hidden));
});
@ -99,12 +98,12 @@ namespace osu.Game.Tests.Database
ILive<BeatmapInfo>? liveBeatmap = null;
Task.Factory.StartNew(() =>
{
using (var threadContext = realmFactory.CreateContext())
realmFactory.Run(threadContext =>
{
var beatmap = threadContext.Write(r => r.Add(new BeatmapInfo(CreateRuleset(), new BeatmapDifficulty(), new BeatmapMetadata())));
liveBeatmap = beatmap.ToLive(realmFactory);
}
});
}, TaskCreationOptions.LongRunning | TaskCreationOptions.HideScheduler).WaitSafely();
Debug.Assert(liveBeatmap != null);
@ -128,12 +127,12 @@ namespace osu.Game.Tests.Database
ILive<BeatmapInfo>? liveBeatmap = null;
Task.Factory.StartNew(() =>
{
using (var threadContext = realmFactory.CreateContext())
realmFactory.Run(threadContext =>
{
var beatmap = threadContext.Write(r => r.Add(new BeatmapInfo(CreateRuleset(), new BeatmapDifficulty(), new BeatmapMetadata())));
liveBeatmap = beatmap.ToLive(realmFactory);
}
});
}, TaskCreationOptions.LongRunning | TaskCreationOptions.HideScheduler).WaitSafely();
Debug.Assert(liveBeatmap != null);
@ -170,12 +169,12 @@ namespace osu.Game.Tests.Database
Task.Factory.StartNew(() =>
{
using (var threadContext = realmFactory.CreateContext())
realmFactory.Run(threadContext =>
{
var beatmap = threadContext.Write(r => r.Add(new BeatmapInfo(CreateRuleset(), new BeatmapDifficulty(), new BeatmapMetadata())));
liveBeatmap = beatmap.ToLive(realmFactory);
}
});
}, TaskCreationOptions.LongRunning | TaskCreationOptions.HideScheduler).WaitSafely();
Debug.Assert(liveBeatmap != null);
@ -189,13 +188,13 @@ namespace osu.Game.Tests.Database
});
// Can't be used, even from within a valid context.
using (realmFactory.CreateContext())
realmFactory.Run(threadContext =>
{
Assert.Throws<InvalidOperationException>(() =>
{
var __ = liveBeatmap.Value;
});
}
});
}, TaskCreationOptions.LongRunning | TaskCreationOptions.HideScheduler).WaitSafely();
});
}
@ -208,12 +207,12 @@ namespace osu.Game.Tests.Database
ILive<BeatmapInfo>? liveBeatmap = null;
Task.Factory.StartNew(() =>
{
using (var threadContext = realmFactory.CreateContext())
realmFactory.Run(threadContext =>
{
var beatmap = threadContext.Write(r => r.Add(new BeatmapInfo(CreateRuleset(), new BeatmapDifficulty(), new BeatmapMetadata())));
liveBeatmap = beatmap.ToLive(realmFactory);
}
});
}, TaskCreationOptions.LongRunning | TaskCreationOptions.HideScheduler).WaitSafely();
Debug.Assert(liveBeatmap != null);
@ -235,50 +234,50 @@ namespace osu.Game.Tests.Database
{
int changesTriggered = 0;
using (var updateThreadContext = realmFactory.CreateContext())
realmFactory.Run(outerRealm =>
{
updateThreadContext.All<BeatmapInfo>().QueryAsyncWithNotifications(gotChange);
outerRealm.All<BeatmapInfo>().QueryAsyncWithNotifications(gotChange);
ILive<BeatmapInfo>? liveBeatmap = null;
Task.Factory.StartNew(() =>
{
using (var threadContext = realmFactory.CreateContext())
realmFactory.Run(innerRealm =>
{
var ruleset = CreateRuleset();
var beatmap = threadContext.Write(r => r.Add(new BeatmapInfo(ruleset, new BeatmapDifficulty(), new BeatmapMetadata())));
var beatmap = innerRealm.Write(r => r.Add(new BeatmapInfo(ruleset, new BeatmapDifficulty(), new BeatmapMetadata())));
// add a second beatmap to ensure that a full refresh occurs below.
// not just a refresh from the resolved Live.
threadContext.Write(r => r.Add(new BeatmapInfo(ruleset, new BeatmapDifficulty(), new BeatmapMetadata())));
innerRealm.Write(r => r.Add(new BeatmapInfo(ruleset, new BeatmapDifficulty(), new BeatmapMetadata())));
liveBeatmap = beatmap.ToLive(realmFactory);
}
});
}, TaskCreationOptions.LongRunning | TaskCreationOptions.HideScheduler).WaitSafely();
Debug.Assert(liveBeatmap != null);
// not yet seen by main context
Assert.AreEqual(0, updateThreadContext.All<BeatmapInfo>().Count());
Assert.AreEqual(0, outerRealm.All<BeatmapInfo>().Count());
Assert.AreEqual(0, changesTriggered);
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
Assert.AreEqual(2, updateThreadContext.All<BeatmapInfo>().Count());
Assert.AreEqual(2, outerRealm.All<BeatmapInfo>().Count());
Assert.AreEqual(1, changesTriggered);
// can access properties without a crash.
Assert.IsFalse(resolved.Hidden);
// ReSharper disable once AccessToDisposedClosure
updateThreadContext.Write(r =>
outerRealm.Write(r =>
{
// can use with the main context.
r.Remove(resolved);
});
});
}
});
void gotChange(IRealmCollection<BeatmapInfo> sender, ChangeSet changes, Exception error)
{

View File

@ -60,15 +60,12 @@ namespace osu.Game.Tests.Database
KeyBindingContainer testContainer = new TestKeyBindingContainer();
// Add some excess bindings for an action which only supports 1.
using (var realm = realmContextFactory.CreateContext())
using (var transaction = realm.BeginWrite())
realmContextFactory.Write(realm =>
{
realm.Add(new RealmKeyBinding(GlobalAction.Back, new KeyCombination(InputKey.A)));
realm.Add(new RealmKeyBinding(GlobalAction.Back, new KeyCombination(InputKey.S)));
realm.Add(new RealmKeyBinding(GlobalAction.Back, new KeyCombination(InputKey.D)));
transaction.Commit();
}
});
Assert.That(queryCount(GlobalAction.Back), Is.EqualTo(3));
@ -79,13 +76,13 @@ namespace osu.Game.Tests.Database
private int queryCount(GlobalAction? match = null)
{
using (var realm = realmContextFactory.CreateContext())
return realmContextFactory.Run(realm =>
{
var results = realm.All<RealmKeyBinding>();
if (match.HasValue)
results = results.Where(k => k.ActionInt == (int)match.Value);
return results.Count();
}
});
}
[Test]
@ -95,26 +92,26 @@ namespace osu.Game.Tests.Database
keyBindingStore.Register(testContainer, Enumerable.Empty<RulesetInfo>());
using (var primaryRealm = realmContextFactory.CreateContext())
realmContextFactory.Run(outerRealm =>
{
var backBinding = primaryRealm.All<RealmKeyBinding>().Single(k => k.ActionInt == (int)GlobalAction.Back);
var backBinding = outerRealm.All<RealmKeyBinding>().Single(k => k.ActionInt == (int)GlobalAction.Back);
Assert.That(backBinding.KeyCombination.Keys, Is.EquivalentTo(new[] { InputKey.Escape }));
var tsr = ThreadSafeReference.Create(backBinding);
using (var threadedContext = realmContextFactory.CreateContext())
realmContextFactory.Run(innerRealm =>
{
var binding = threadedContext.ResolveReference(tsr);
threadedContext.Write(() => binding.KeyCombination = new KeyCombination(InputKey.BackSpace));
}
var binding = innerRealm.ResolveReference(tsr);
innerRealm.Write(() => binding.KeyCombination = new KeyCombination(InputKey.BackSpace));
});
Assert.That(backBinding.KeyCombination.Keys, Is.EquivalentTo(new[] { InputKey.BackSpace }));
// check still correct after re-query.
backBinding = primaryRealm.All<RealmKeyBinding>().Single(k => k.ActionInt == (int)GlobalAction.Back);
backBinding = outerRealm.All<RealmKeyBinding>().Single(k => k.ActionInt == (int)GlobalAction.Back);
Assert.That(backBinding.KeyCombination.Keys, Is.EquivalentTo(new[] { InputKey.BackSpace }));
}
});
}
[TearDown]