From 77da1e12d5be37a1eacd09182d2e7124c2a2eb90 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20Dach?= Date: Thu, 23 Dec 2021 19:18:26 +0100 Subject: [PATCH] Add test implementation of the ruleset config cache --- .../Tests/Rulesets/TestRulesetConfigCache.cs | 19 +++++++++++++++++++ osu.Game/Tests/Visual/OsuTestScene.cs | 8 ++++++++ 2 files changed, 27 insertions(+) create mode 100644 osu.Game/Tests/Rulesets/TestRulesetConfigCache.cs diff --git a/osu.Game/Tests/Rulesets/TestRulesetConfigCache.cs b/osu.Game/Tests/Rulesets/TestRulesetConfigCache.cs new file mode 100644 index 0000000000..537bee6824 --- /dev/null +++ b/osu.Game/Tests/Rulesets/TestRulesetConfigCache.cs @@ -0,0 +1,19 @@ +// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. +// See the LICENCE file in the repository root for full licence text. + +using System.Collections.Concurrent; +using osu.Game.Rulesets; +using osu.Game.Rulesets.Configuration; + +namespace osu.Game.Tests.Rulesets +{ + /// + /// Test implementation of a , which ensures isolation between test scenes. + /// + public class TestRulesetConfigCache : IRulesetConfigCache + { + private readonly ConcurrentDictionary configCache = new ConcurrentDictionary(); + + public IRulesetConfigManager GetConfigFor(Ruleset ruleset) => configCache.GetOrAdd(ruleset.ShortName, _ => ruleset.CreateConfig(null)); + } +} diff --git a/osu.Game/Tests/Visual/OsuTestScene.cs b/osu.Game/Tests/Visual/OsuTestScene.cs index 1b2c1e72c4..df8c9f22db 100644 --- a/osu.Game/Tests/Visual/OsuTestScene.cs +++ b/osu.Game/Tests/Visual/OsuTestScene.cs @@ -31,6 +31,7 @@ using osu.Game.Rulesets.UI; using osu.Game.Screens; using osu.Game.Storyboards; using osu.Game.Tests.Beatmaps; +using osu.Game.Tests.Rulesets; namespace osu.Game.Tests.Visual { @@ -119,6 +120,13 @@ namespace osu.Game.Tests.Visual var baseDependencies = base.CreateChildDependencies(parent); + // to isolate ruleset configs in tests from the actual database and avoid state pollution problems, + // as well as problems due to the implementation details of the "real" implementation (the configs only being available at `LoadComplete()`), + // cache a test implementation of the ruleset config cache over the "real" one. + var isolatedBaseDependencies = new DependencyContainer(baseDependencies); + isolatedBaseDependencies.CacheAs(new TestRulesetConfigCache()); + baseDependencies = isolatedBaseDependencies; + var providedRuleset = CreateRuleset(); if (providedRuleset != null) baseDependencies = rulesetDependencies = new DrawableRulesetDependencies(providedRuleset, baseDependencies);