From 49e2a8afa3f345ec4dc12b8dc99ec546a3b06e6c Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Tue, 23 Nov 2021 14:08:03 +0900 Subject: [PATCH 1/4] Don't directly reset the database when running tests The containing storage is destroyed anyway, so this is redundant. --- osu.Game/Tests/Visual/OsuTestScene.cs | 5 ----- 1 file changed, 5 deletions(-) diff --git a/osu.Game/Tests/Visual/OsuTestScene.cs b/osu.Game/Tests/Visual/OsuTestScene.cs index fc7bc324ca..2bacfcebca 100644 --- a/osu.Game/Tests/Visual/OsuTestScene.cs +++ b/osu.Game/Tests/Visual/OsuTestScene.cs @@ -90,11 +90,6 @@ namespace osu.Game.Tests.Visual { var factory = new DatabaseContextFactory(LocalStorage); - // only reset the database if not using the host storage. - // if we reset the host storage, it will delete global key bindings. - if (isolatedHostStorage == null) - factory.ResetDatabase(); - using (var usage = factory.Get()) usage.Migrate(); return factory; From 5631e75f16073892a81b1c4e6af5959049ee2b0c Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Tue, 23 Nov 2021 14:11:27 +0900 Subject: [PATCH 2/4] Restructure how the headless storage is used / documented to hopefully make more sense --- osu.Game/Tests/Visual/OsuTestScene.cs | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/osu.Game/Tests/Visual/OsuTestScene.cs b/osu.Game/Tests/Visual/OsuTestScene.cs index 2bacfcebca..cb4af67839 100644 --- a/osu.Game/Tests/Visual/OsuTestScene.cs +++ b/osu.Game/Tests/Visual/OsuTestScene.cs @@ -81,8 +81,7 @@ namespace osu.Game.Tests.Visual protected override IReadOnlyDependencyContainer CreateChildDependencies(IReadOnlyDependencyContainer parent) { - if (!UseFreshStoragePerRun) - isolatedHostStorage = (parent.Get() as HeadlessGameHost)?.Storage; + isolatedHostStorage = (parent.Get() as HeadlessGameHost)?.Storage; Resources = parent.Get().Resources; @@ -149,8 +148,16 @@ namespace osu.Game.Tests.Visual } } - localStorage = - new Lazy(() => isolatedHostStorage ?? new TemporaryNativeStorage($"{GetType().Name}-{Guid.NewGuid()}")); + localStorage = new Lazy(() => + { + // When running headless, there is an opportunity to use the host storage rather than creating a second isolated one. + // This is because the host is recycled per TestScene execution in headless at an nunit level. + // Importantly, we can't use this optimisation when `UseFreshStoragePerRun` is true, as it doesn't reset per test method. + if (!UseFreshStoragePerRun && isolatedHostStorage != null) + return isolatedHostStorage; + + return new TemporaryNativeStorage($"{GetType().Name}-{Guid.NewGuid()}"); + }); } [Resolved] From 55f7d120e6325c0b003bcdb6373e31cae384149f Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Tue, 23 Nov 2021 14:11:44 +0900 Subject: [PATCH 3/4] Rename and reorder fields in `OsuTestScene` --- osu.Game/Tests/Visual/OsuTestScene.cs | 34 +++++++++++++-------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/osu.Game/Tests/Visual/OsuTestScene.cs b/osu.Game/Tests/Visual/OsuTestScene.cs index cb4af67839..ca7b7b27b0 100644 --- a/osu.Game/Tests/Visual/OsuTestScene.cs +++ b/osu.Game/Tests/Visual/OsuTestScene.cs @@ -43,13 +43,6 @@ namespace osu.Game.Tests.Visual protected new OsuScreenDependencies Dependencies { get; private set; } - private DrawableRulesetDependencies rulesetDependencies; - - private Lazy localStorage; - protected Storage LocalStorage => localStorage.Value; - - private Lazy contextFactory; - protected IResourceStore Resources; protected IAPIProvider API @@ -65,23 +58,32 @@ namespace osu.Game.Tests.Visual private DummyAPIAccess dummyAPI; - protected DatabaseContextFactory ContextFactory => contextFactory.Value; - /// /// Whether this test scene requires real-world API access. /// If true, this will bypass the local and use the provided one. /// protected virtual bool UseOnlineAPI => false; + protected DatabaseContextFactory ContextFactory => contextFactory.Value; + + private Lazy contextFactory; + + protected virtual bool UseFreshStoragePerRun => false; + /// - /// When running headless, there is an opportunity to use the host storage rather than creating a second isolated one. - /// This is because the host is recycled per TestScene execution in headless at an nunit level. + /// A storage to be used by test runs. Can be isolated by setting to true. /// - private Storage isolatedHostStorage; + protected Storage LocalStorage => localStorage.Value; + + private Lazy localStorage; + + private Storage headlessHostStorage; + + private DrawableRulesetDependencies rulesetDependencies; protected override IReadOnlyDependencyContainer CreateChildDependencies(IReadOnlyDependencyContainer parent) { - isolatedHostStorage = (parent.Get() as HeadlessGameHost)?.Storage; + headlessHostStorage = (parent.Get() as HeadlessGameHost)?.Storage; Resources = parent.Get().Resources; @@ -132,8 +134,6 @@ namespace osu.Game.Tests.Visual base.Content.Add(content = new DrawSizePreservingFillContainer()); } - protected virtual bool UseFreshStoragePerRun => false; - public virtual void RecycleLocalStorage(bool isDisposing) { if (localStorage?.IsValueCreated == true) @@ -153,8 +153,8 @@ namespace osu.Game.Tests.Visual // When running headless, there is an opportunity to use the host storage rather than creating a second isolated one. // This is because the host is recycled per TestScene execution in headless at an nunit level. // Importantly, we can't use this optimisation when `UseFreshStoragePerRun` is true, as it doesn't reset per test method. - if (!UseFreshStoragePerRun && isolatedHostStorage != null) - return isolatedHostStorage; + if (!UseFreshStoragePerRun && headlessHostStorage != null) + return headlessHostStorage; return new TemporaryNativeStorage($"{GetType().Name}-{Guid.NewGuid()}"); }); From 473f6b0347dac2494172f21d701e341ed7332954 Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Tue, 23 Nov 2021 14:14:53 +0900 Subject: [PATCH 4/4] Add more xmldoc --- osu.Game/Tests/Visual/OsuTestScene.cs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/osu.Game/Tests/Visual/OsuTestScene.cs b/osu.Game/Tests/Visual/OsuTestScene.cs index ca7b7b27b0..eedf266bbe 100644 --- a/osu.Game/Tests/Visual/OsuTestScene.cs +++ b/osu.Game/Tests/Visual/OsuTestScene.cs @@ -64,15 +64,32 @@ namespace osu.Game.Tests.Visual /// protected virtual bool UseOnlineAPI => false; + /// + /// A database context factory to be used by test runs. Can be isolated and reset by setting to true. + /// + /// + /// In interactive runs (ie. VisualTests) this will use the user's database if is not set to true. + /// protected DatabaseContextFactory ContextFactory => contextFactory.Value; private Lazy contextFactory; + /// + /// Whether a fresh storage should be initialised per test (method) run. + /// + /// + /// By default (ie. if not set to true): + /// - in interactive runs, the user's storage will be used + /// - in headless runs, a shared temporary storage will be used per test class. + /// protected virtual bool UseFreshStoragePerRun => false; /// /// A storage to be used by test runs. Can be isolated by setting to true. /// + /// + /// In interactive runs (ie. VisualTests) this will use the user's storage if is not set to true. + /// protected Storage LocalStorage => localStorage.Value; private Lazy localStorage;