Move UserLookupCache inside dependencies

This commit is contained in:
smoogipoo 2021-06-25 17:37:02 +09:00
parent d6ab08c958
commit 7aefbe3da1
11 changed files with 46 additions and 69 deletions

View File

@ -2,8 +2,6 @@
// See the LICENCE file in the repository root for full licence text. // See the LICENCE file in the repository root for full licence text.
using System.Linq; using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using NUnit.Framework; using NUnit.Framework;
using osu.Framework.Allocation; using osu.Framework.Allocation;
using osu.Framework.Graphics; using osu.Framework.Graphics;
@ -232,14 +230,5 @@ namespace osu.Game.Tests.Visual.Gameplay
AddStep("load screen", () => LoadScreen(spectatorScreen = new SoloSpectator(streamingUser))); AddStep("load screen", () => LoadScreen(spectatorScreen = new SoloSpectator(streamingUser)));
AddUntilStep("wait for screen load", () => spectatorScreen.LoadState == LoadState.Loaded); AddUntilStep("wait for screen load", () => spectatorScreen.LoadState == LoadState.Loaded);
} }
internal class TestUserLookupCache : UserLookupCache
{
protected override Task<User> ComputeValueAsync(int lookup, CancellationToken token = default) => Task.FromResult(new User
{
Id = lookup,
Username = $"User {lookup}"
});
}
} }
} }

View File

@ -3,19 +3,15 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using NUnit.Framework; using NUnit.Framework;
using osu.Framework.Testing; using osu.Framework.Testing;
using osu.Framework.Timing; using osu.Framework.Timing;
using osu.Game.Database;
using osu.Game.Online.Spectator; using osu.Game.Online.Spectator;
using osu.Game.Rulesets.Osu.Scoring; using osu.Game.Rulesets.Osu.Scoring;
using osu.Game.Screens.OnlinePlay.Multiplayer.Spectate; using osu.Game.Screens.OnlinePlay.Multiplayer.Spectate;
using osu.Game.Screens.Play.HUD; using osu.Game.Screens.Play.HUD;
using osu.Game.Tests.Visual.OnlinePlay; using osu.Game.Tests.Visual.OnlinePlay;
using osu.Game.Tests.Visual.Spectator; using osu.Game.Tests.Visual.Spectator;
using osu.Game.Users;
namespace osu.Game.Tests.Visual.Multiplayer namespace osu.Game.Tests.Visual.Multiplayer
{ {
@ -128,24 +124,10 @@ namespace osu.Game.Tests.Visual.Multiplayer
protected class TestDependencies : MultiplayerRoomTestDependencies protected class TestDependencies : MultiplayerRoomTestDependencies
{ {
public readonly TestSpectatorClient SpectatorClient = new TestSpectatorClient(); public readonly TestSpectatorClient SpectatorClient = new TestSpectatorClient();
public readonly UserLookupCache LookupCache = new TestUserLookupCache();
public TestDependencies() public TestDependencies()
{ {
CacheAs<SpectatorClient>(SpectatorClient); CacheAs<SpectatorClient>(SpectatorClient);
CacheAs(LookupCache);
}
}
private class TestUserLookupCache : UserLookupCache
{
protected override Task<User> ComputeValueAsync(int lookup, CancellationToken token = default)
{
return Task.FromResult(new User
{
Id = lookup,
Username = $"User {lookup}"
});
} }
} }
} }

View File

@ -3,21 +3,17 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using NUnit.Framework; using NUnit.Framework;
using osu.Framework.Allocation; using osu.Framework.Allocation;
using osu.Framework.Graphics; using osu.Framework.Graphics;
using osu.Framework.Testing; using osu.Framework.Testing;
using osu.Game.Beatmaps; using osu.Game.Beatmaps;
using osu.Game.Database;
using osu.Game.Online.Spectator; using osu.Game.Online.Spectator;
using osu.Game.Screens.OnlinePlay.Multiplayer.Spectate; using osu.Game.Screens.OnlinePlay.Multiplayer.Spectate;
using osu.Game.Screens.Play; using osu.Game.Screens.Play;
using osu.Game.Tests.Beatmaps.IO; using osu.Game.Tests.Beatmaps.IO;
using osu.Game.Tests.Visual.OnlinePlay; using osu.Game.Tests.Visual.OnlinePlay;
using osu.Game.Tests.Visual.Spectator; using osu.Game.Tests.Visual.Spectator;
using osu.Game.Users;
namespace osu.Game.Tests.Visual.Multiplayer namespace osu.Game.Tests.Visual.Multiplayer
{ {
@ -289,24 +285,10 @@ namespace osu.Game.Tests.Visual.Multiplayer
protected class TestDependencies : MultiplayerRoomTestDependencies protected class TestDependencies : MultiplayerRoomTestDependencies
{ {
public readonly TestSpectatorClient SpectatorClient = new TestSpectatorClient(); public readonly TestSpectatorClient SpectatorClient = new TestSpectatorClient();
public readonly UserLookupCache LookupCache = new TestUserLookupCache();
public TestDependencies() public TestDependencies()
{ {
CacheAs<SpectatorClient>(SpectatorClient); CacheAs<SpectatorClient>(SpectatorClient);
CacheAs(LookupCache);
}
}
internal class TestUserLookupCache : UserLookupCache
{
protected override Task<User> ComputeValueAsync(int lookup, CancellationToken token = default)
{
return Task.FromResult(new User
{
Id = lookup,
Username = $"User {lookup}"
});
} }
} }
} }

View File

@ -6,11 +6,11 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using NUnit.Framework; using NUnit.Framework;
using osu.Framework.Allocation; using osu.Framework.Allocation;
using osu.Framework.Extensions.ObjectExtensions;
using osu.Framework.Graphics; using osu.Framework.Graphics;
using osu.Framework.Testing; using osu.Framework.Testing;
using osu.Framework.Utils; using osu.Framework.Utils;
using osu.Game.Configuration; using osu.Game.Configuration;
using osu.Game.Database;
using osu.Game.Online.API; using osu.Game.Online.API;
using osu.Game.Online.Spectator; using osu.Game.Online.Spectator;
using osu.Game.Replays.Legacy; using osu.Game.Replays.Legacy;
@ -18,7 +18,6 @@ using osu.Game.Rulesets.Osu.Scoring;
using osu.Game.Rulesets.Scoring; using osu.Game.Rulesets.Scoring;
using osu.Game.Scoring; using osu.Game.Scoring;
using osu.Game.Screens.Play.HUD; using osu.Game.Screens.Play.HUD;
using osu.Game.Tests.Visual.Online;
using osu.Game.Tests.Visual.OnlinePlay; using osu.Game.Tests.Visual.OnlinePlay;
using osu.Game.Tests.Visual.Spectator; using osu.Game.Tests.Visual.Spectator;
@ -26,12 +25,10 @@ namespace osu.Game.Tests.Visual.Multiplayer
{ {
public class TestSceneMultiplayerGameplayLeaderboard : MultiplayerTestScene public class TestSceneMultiplayerGameplayLeaderboard : MultiplayerTestScene
{ {
private const int users = 16; private static IEnumerable<int> users => Enumerable.Range(0, 16);
public TestMultiplayerSpectatorClient SpectatorClient => RoomDependencies?.SpectatorClient; public TestMultiplayerSpectatorClient SpectatorClient => RoomDependencies?.SpectatorClient;
public UserLookupCache LookupCache => RoomDependencies?.LookupCache;
protected new TestDependencies RoomDependencies => (TestDependencies)base.RoomDependencies; protected new TestDependencies RoomDependencies => (TestDependencies)base.RoomDependencies;
private MultiplayerGameplayLeaderboard leaderboard; private MultiplayerGameplayLeaderboard leaderboard;
@ -57,14 +54,11 @@ namespace osu.Game.Tests.Visual.Multiplayer
var playable = Beatmap.Value.GetPlayableBeatmap(Ruleset.Value); var playable = Beatmap.Value.GetPlayableBeatmap(Ruleset.Value);
for (int i = 0; i < users; i++) foreach (var user in users)
SpectatorClient.StartPlay(i, Beatmap.Value.BeatmapInfo.OnlineBeatmapID ?? 0); SpectatorClient.StartPlay(user, Beatmap.Value.BeatmapInfo.OnlineBeatmapID ?? 0);
SpectatorClient.Schedule(() => // Todo: This is REALLY bad.
{ Client.CurrentMatchPlayingUserIds.AddRange(users);
Client.CurrentMatchPlayingUserIds.Clear();
Client.CurrentMatchPlayingUserIds.AddRange(SpectatorClient.PlayingUsers);
});
Children = new Drawable[] Children = new Drawable[]
{ {
@ -73,7 +67,7 @@ namespace osu.Game.Tests.Visual.Multiplayer
scoreProcessor.ApplyBeatmap(playable); scoreProcessor.ApplyBeatmap(playable);
LoadComponentAsync(leaderboard = new MultiplayerGameplayLeaderboard(scoreProcessor, SpectatorClient.PlayingUsers.ToArray()) LoadComponentAsync(leaderboard = new MultiplayerGameplayLeaderboard(scoreProcessor, users.ToArray())
{ {
Anchor = Anchor.Centre, Anchor = Anchor.Centre,
Origin = Anchor.Centre, Origin = Anchor.Centre,
@ -94,7 +88,8 @@ namespace osu.Game.Tests.Visual.Multiplayer
[Test] [Test]
public void TestUserQuit() public void TestUserQuit()
{ {
AddRepeatStep("mark user quit", () => Client.CurrentMatchPlayingUserIds.RemoveAt(0), users); foreach (var user in users)
AddStep($"mark user {user} quit", () => Client.RemoveUser(LookupCache.GetUserAsync(user).Result.AsNonNull()));
} }
[Test] [Test]
@ -110,12 +105,10 @@ namespace osu.Game.Tests.Visual.Multiplayer
protected class TestDependencies : MultiplayerRoomTestDependencies protected class TestDependencies : MultiplayerRoomTestDependencies
{ {
public readonly TestMultiplayerSpectatorClient SpectatorClient = new TestMultiplayerSpectatorClient(); public readonly TestMultiplayerSpectatorClient SpectatorClient = new TestMultiplayerSpectatorClient();
public readonly UserLookupCache LookupCache = new TestSceneCurrentlyPlayingDisplay.TestUserLookupCache();
public TestDependencies() public TestDependencies()
{ {
CacheAs<SpectatorClient>(SpectatorClient); CacheAs<SpectatorClient>(SpectatorClient);
CacheAs(LookupCache);
} }
} }

View File

@ -1,13 +1,14 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence. // 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. // See the LICENCE file in the repository root for full licence text.
using osu.Game.Database;
using osu.Game.Online.Multiplayer; using osu.Game.Online.Multiplayer;
using osu.Game.Screens.OnlinePlay; using osu.Game.Screens.OnlinePlay;
using osu.Game.Tests.Visual.OnlinePlay; using osu.Game.Tests.Visual.OnlinePlay;
namespace osu.Game.Tests.Visual.Multiplayer namespace osu.Game.Tests.Visual.Multiplayer
{ {
public interface IMultiplayerRoomTestDependencies : IRoomTestDependencies public interface IMultiplayerTestDependencies : IOnlinePlayTestDependencies
{ {
/// <summary> /// <summary>
/// The cached <see cref="MultiplayerClient"/>. /// The cached <see cref="MultiplayerClient"/>.
@ -18,5 +19,10 @@ namespace osu.Game.Tests.Visual.Multiplayer
/// The cached <see cref="IRoomManager"/>. /// The cached <see cref="IRoomManager"/>.
/// </summary> /// </summary>
new TestMultiplayerRoomManager RoomManager { get; } new TestMultiplayerRoomManager RoomManager { get; }
/// <summary>
/// The cached <see cref="UserLookupCache"/>.
/// </summary>
TestUserLookupCache LookupCache { get; }
} }
} }

View File

@ -1,21 +1,26 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence. // 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. // See the LICENCE file in the repository root for full licence text.
using osu.Game.Database;
using osu.Game.Online.Multiplayer; using osu.Game.Online.Multiplayer;
using osu.Game.Screens.OnlinePlay; using osu.Game.Screens.OnlinePlay;
using osu.Game.Tests.Visual.OnlinePlay; using osu.Game.Tests.Visual.OnlinePlay;
namespace osu.Game.Tests.Visual.Multiplayer namespace osu.Game.Tests.Visual.Multiplayer
{ {
public class MultiplayerRoomTestDependencies : RoomTestDependencies, IMultiplayerRoomTestDependencies public class MultiplayerRoomTestDependencies : RoomTestDependencies, IMultiplayerTestDependencies
{ {
public TestMultiplayerClient Client { get; } public TestMultiplayerClient Client { get; }
public TestUserLookupCache LookupCache { get; }
public new TestMultiplayerRoomManager RoomManager => (TestMultiplayerRoomManager)base.RoomManager; public new TestMultiplayerRoomManager RoomManager => (TestMultiplayerRoomManager)base.RoomManager;
public MultiplayerRoomTestDependencies() public MultiplayerRoomTestDependencies()
{ {
Client = new TestMultiplayerClient(RoomManager); Client = new TestMultiplayerClient(RoomManager);
LookupCache = new TestUserLookupCache();
CacheAs<MultiplayerClient>(Client); CacheAs<MultiplayerClient>(Client);
CacheAs<UserLookupCache>(LookupCache);
} }
protected override IRoomManager CreateRoomManager() => new TestMultiplayerRoomManager(); protected override IRoomManager CreateRoomManager() => new TestMultiplayerRoomManager();

View File

@ -8,13 +8,14 @@ using osu.Game.Tests.Visual.OnlinePlay;
namespace osu.Game.Tests.Visual.Multiplayer namespace osu.Game.Tests.Visual.Multiplayer
{ {
public abstract class MultiplayerTestScene : OnlinePlayTestScene public abstract class MultiplayerTestScene : OnlinePlayTestScene, IMultiplayerTestDependencies
{ {
public const int PLAYER_1_ID = 55; public const int PLAYER_1_ID = 55;
public const int PLAYER_2_ID = 56; public const int PLAYER_2_ID = 56;
public TestMultiplayerClient Client => RoomDependencies.Client; public TestMultiplayerClient Client => RoomDependencies.Client;
public new TestMultiplayerRoomManager RoomManager => RoomDependencies.RoomManager; public new TestMultiplayerRoomManager RoomManager => RoomDependencies.RoomManager;
public TestUserLookupCache LookupCache => RoomDependencies?.LookupCache;
protected new MultiplayerRoomTestDependencies RoomDependencies => (MultiplayerRoomTestDependencies)base.RoomDependencies; protected new MultiplayerRoomTestDependencies RoomDependencies => (MultiplayerRoomTestDependencies)base.RoomDependencies;

View File

@ -8,7 +8,7 @@ using osu.Game.Screens.OnlinePlay.Lounge.Components;
namespace osu.Game.Tests.Visual.OnlinePlay namespace osu.Game.Tests.Visual.OnlinePlay
{ {
public interface IRoomTestDependencies public interface IOnlinePlayTestDependencies
{ {
/// <summary> /// <summary>
/// The cached <see cref="Room"/>. /// The cached <see cref="Room"/>.

View File

@ -16,7 +16,7 @@ namespace osu.Game.Tests.Visual.OnlinePlay
/// <summary> /// <summary>
/// A <see cref="ScreenTestScene"/> providing all the dependencies cached by <see cref="OnlinePlayScreen"/> for testing <see cref="OnlinePlaySubScreen"/>s. /// A <see cref="ScreenTestScene"/> providing all the dependencies cached by <see cref="OnlinePlayScreen"/> for testing <see cref="OnlinePlaySubScreen"/>s.
/// </summary> /// </summary>
public abstract class OnlinePlayTestScene : ScreenTestScene, IRoomTestDependencies public abstract class OnlinePlayTestScene : ScreenTestScene, IOnlinePlayTestDependencies
{ {
public Bindable<Room> SelectedRoom => RoomDependencies?.SelectedRoom; public Bindable<Room> SelectedRoom => RoomDependencies?.SelectedRoom;
public IRoomManager RoomManager => RoomDependencies?.RoomManager; public IRoomManager RoomManager => RoomDependencies?.RoomManager;

View File

@ -15,7 +15,7 @@ namespace osu.Game.Tests.Visual.OnlinePlay
/// <summary> /// <summary>
/// Contains dependencies for testing online-play rooms. /// Contains dependencies for testing online-play rooms.
/// </summary> /// </summary>
public class RoomTestDependencies : IReadOnlyDependencyContainer, IRoomTestDependencies public class RoomTestDependencies : IReadOnlyDependencyContainer, IOnlinePlayTestDependencies
{ {
public Bindable<Room> SelectedRoom { get; } public Bindable<Room> SelectedRoom { get; }
public IRoomManager RoomManager { get; } public IRoomManager RoomManager { get; }

View File

@ -0,0 +1,19 @@
// 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.Threading;
using System.Threading.Tasks;
using osu.Game.Database;
using osu.Game.Users;
namespace osu.Game.Tests.Visual
{
public class TestUserLookupCache : UserLookupCache
{
protected override Task<User> ComputeValueAsync(int lookup, CancellationToken token = default) => Task.FromResult(new User
{
Id = lookup,
Username = $"User {lookup}"
});
}
}