Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System.Threading.Tasks;
using Moq;
using osu.Game.Online.Multiplayer;
using osu.Game.Online.Rooms;
using osu.Server.Spectator.Hubs.Multiplayer;
using Xunit;

Expand All @@ -19,19 +18,7 @@ public class MatchTypeRoomEventHookTests : MultiplayerTest
public async Task NewUserJoinedTriggersRulesetHook()
{
var hub = new Mock<IMultiplayerHubContext>();
var room = new ServerMultiplayerRoom(1, hub.Object, DatabaseFactory.Object)
{
Playlist =
{
new MultiplayerPlaylistItem
{
BeatmapID = 3333,
BeatmapChecksum = "3333"
},
}
};

await room.Initialise();
var room = await ServerMultiplayerRoom.InitialiseAsync(ROOM_ID, hub.Object, DatabaseFactory.Object);

Mock<IMatchController> controller = new Mock<IMatchController>();
await room.ChangeMatchType(controller.Object);
Expand All @@ -45,19 +32,7 @@ public async Task NewUserJoinedTriggersRulesetHook()
public async Task UserLeavesTriggersRulesetHook()
{
var hub = new Mock<IMultiplayerHubContext>();
var room = new ServerMultiplayerRoom(1, hub.Object, DatabaseFactory.Object)
{
Playlist =
{
new MultiplayerPlaylistItem
{
BeatmapID = 3333,
BeatmapChecksum = "3333"
},
}
};

await room.Initialise();
var room = await ServerMultiplayerRoom.InitialiseAsync(ROOM_ID, hub.Object, DatabaseFactory.Object);

var user = new MultiplayerRoomUser(1);

Expand All @@ -74,19 +49,7 @@ public async Task UserLeavesTriggersRulesetHook()
public async Task TypeChangeTriggersInitialJoins()
{
var hub = new Mock<IMultiplayerHubContext>();
var room = new ServerMultiplayerRoom(1, hub.Object, DatabaseFactory.Object)
{
Playlist =
{
new MultiplayerPlaylistItem
{
BeatmapID = 3333,
BeatmapChecksum = "3333"
},
}
};

await room.Initialise();
var room = await ServerMultiplayerRoom.InitialiseAsync(ROOM_ID, hub.Object, DatabaseFactory.Object);

// join a number of users initially to the room
for (int i = 0; i < 5; i++)
Expand Down
6 changes: 3 additions & 3 deletions osu.Server.Spectator.Tests/Multiplayer/MatchTypeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public async Task MatchRoomStateUpdatePropagatesToUsers()

room.MatchState = mockRoomState.Object;

await Hub.HubContext.NotifyMatchRoomStateChanged(room);
await HubContext.NotifyMatchRoomStateChanged(room);

Receiver.Verify(c => c.MatchRoomStateChanged(mockRoomState.Object), Times.Once);
}
Expand All @@ -50,7 +50,7 @@ public async Task MatchEventPropagatesToUsers()

var mockEvent = new Mock<MatchServerEvent>();

await Hub.HubContext.NotifyNewMatchEvent(room, mockEvent.Object);
await HubContext.NotifyNewMatchEvent(room, mockEvent.Object);

Receiver.Verify(c => c.MatchEvent(mockEvent.Object), Times.Once);
}
Expand All @@ -72,7 +72,7 @@ public async Task MatchUserStateUpdatePropagatesToUsers()

user.MatchState = mockRoomState.Object;

await Hub.HubContext.NotifyMatchUserStateChanged(room, user);
await HubContext.NotifyMatchUserStateChanged(room, user);

Receiver.Verify(c => c.MatchUserStateChanged(user.UserID, mockRoomState.Object), Times.Once);
}
Expand Down
15 changes: 13 additions & 2 deletions osu.Server.Spectator.Tests/Multiplayer/MultiplayerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public abstract class MultiplayerTest
protected const long ROOM_ID = 8888;
protected const long ROOM_ID_2 = 9999;

protected IMultiplayerHubContext HubContext { get; }
protected TestMultiplayerHub Hub { get; }
protected EntityStore<ServerMultiplayerRoom> Rooms { get; }
protected EntityStore<MultiplayerClientState> UserStates { get; }
Expand Down Expand Up @@ -138,15 +139,25 @@ protected MultiplayerTest()
LegacyIO.Setup(io => io.CreateRoomAsync(It.IsAny<int>(), It.IsAny<MultiplayerRoom>()))
.Returns<int, MultiplayerRoom>((_, room) => Task.FromResult(room.RoomID));

MultiplayerEventLogger eventLogger = new MultiplayerEventLogger(loggerFactoryMock.Object, DatabaseFactory.Object);

HubContext = new MultiplayerHubContext(
hubContext.Object,
Rooms,
UserStates,
loggerFactoryMock.Object,
DatabaseFactory.Object,
eventLogger);

Hub = new TestMultiplayerHub(
loggerFactoryMock.Object,
Rooms,
UserStates,
DatabaseFactory.Object,
new ChatFilters(DatabaseFactory.Object),
hubContext.Object,
HubContext,
LegacyIO.Object,
new MultiplayerEventLogger(loggerFactoryMock.Object, DatabaseFactory.Object),
eventLogger,
new Mock<IMatchmakingQueueBackgroundService>().Object);
Hub.Groups = Groups.Object;
Hub.Clients = Clients.Object;
Expand Down
19 changes: 0 additions & 19 deletions osu.Server.Spectator.Tests/Multiplayer/RoomParticipationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,25 +165,6 @@ public async Task UserJoinLeaveNotifiesOtherUsers()
Receiver.Verify(r => r.UserLeft(roomUser), Times.Exactly(2));
}

[Fact]
public async Task UserJoinPreRetrievalFailureCleansUpRoom()
{
Database.Setup(db => db.GetRealtimeRoomAsync(ROOM_ID))
.Callback<long>(InitialiseRoom)
.ReturnsAsync(() => new multiplayer_room
{
type = database_match_type.head_to_head,
ends_at = DateTimeOffset.Now.AddMinutes(5),
user_id = USER_ID,
});

SetUserContext(ContextUser2); // not the correct user to join the game first; triggers host mismatch failure.
await Assert.ThrowsAnyAsync<Exception>(() => Hub.JoinRoom(ROOM_ID));

await Assert.ThrowsAsync<KeyNotFoundException>(() => Rooms.GetForUse(ROOM_ID));
await Assert.ThrowsAsync<KeyNotFoundException>(() => UserStates.GetForUse(USER_ID));
}

[Fact]
public async Task UserJoinPreJoinFailureCleansUpRoom()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,7 @@ public class TeamVersusMatchControllerTests : MultiplayerTest
public async Task UserRequestsValidTeamChange(int team)
{
var hub = new Mock<IMultiplayerHubContext>();
var room = new ServerMultiplayerRoom(1, hub.Object, DatabaseFactory.Object)
{
Playlist =
{
new MultiplayerPlaylistItem
{
BeatmapID = 3333,
BeatmapChecksum = "3333"
},
}
};
await room.Initialise();
var room = await ServerMultiplayerRoom.InitialiseAsync(ROOM_ID, hub.Object, DatabaseFactory.Object);

var teamVersus = new TeamVersusMatchController(room, hub.Object, DatabaseFactory.Object);

Expand All @@ -56,18 +45,8 @@ public async Task UserRequestsValidTeamChange(int team)
public async Task UserRequestsInvalidTeamChange(int team)
{
var hub = new Mock<IMultiplayerHubContext>();
var room = new ServerMultiplayerRoom(1, hub.Object, DatabaseFactory.Object)
{
Playlist =
{
new MultiplayerPlaylistItem
{
BeatmapID = 3333,
BeatmapChecksum = "3333"
},
}
};
await room.Initialise();
var room = await ServerMultiplayerRoom.InitialiseAsync(ROOM_ID, hub.Object, DatabaseFactory.Object);

var teamVersus = new TeamVersusMatchController(room, hub.Object, DatabaseFactory.Object);

// change the match type
Expand All @@ -92,18 +71,7 @@ public async Task UserRequestsInvalidTeamChange(int team)
public async Task NewUsersAssignedToTeamWithFewerUsers()
{
var hub = new Mock<IMultiplayerHubContext>();
var room = new ServerMultiplayerRoom(1, hub.Object, DatabaseFactory.Object)
{
Playlist =
{
new MultiplayerPlaylistItem
{
BeatmapID = 3333,
BeatmapChecksum = "3333"
},
}
};
await room.Initialise();
var room = await ServerMultiplayerRoom.InitialiseAsync(ROOM_ID, hub.Object, DatabaseFactory.Object);

// change the match type
await room.ChangeMatchType(MatchType.TeamVersus);
Expand Down Expand Up @@ -133,18 +101,7 @@ public async Task NewUsersAssignedToTeamWithFewerUsers()
public async Task InitialUsersAssignedToTeamsEqually()
{
var hub = new Mock<IMultiplayerHubContext>();
var room = new ServerMultiplayerRoom(1, hub.Object, DatabaseFactory.Object)
{
Playlist =
{
new MultiplayerPlaylistItem
{
BeatmapID = 3333,
BeatmapChecksum = "3333"
},
}
};
await room.Initialise();
var room = await ServerMultiplayerRoom.InitialiseAsync(ROOM_ID, hub.Object, DatabaseFactory.Object);

// join a number of users initially to the room
for (int i = 0; i < 5; i++)
Expand All @@ -164,18 +121,7 @@ public async Task InitialUsersAssignedToTeamsEqually()
public async Task StateMaintainedBetweenRulesetSwitch()
{
var hub = new Mock<IMultiplayerHubContext>();
var room = new ServerMultiplayerRoom(1, hub.Object, DatabaseFactory.Object)
{
Playlist =
{
new MultiplayerPlaylistItem
{
BeatmapID = 3333,
BeatmapChecksum = "3333"
},
}
};
await room.Initialise();
var room = await ServerMultiplayerRoom.InitialiseAsync(ROOM_ID, hub.Object, DatabaseFactory.Object);

await room.ChangeMatchType(MatchType.TeamVersus);

Expand Down
5 changes: 1 addition & 4 deletions osu.Server.Spectator.Tests/Multiplayer/TestMultiplayerHub.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.

using Microsoft.AspNetCore.SignalR;
using Microsoft.Extensions.Logging;
using osu.Server.Spectator.Database;
using osu.Server.Spectator.Entities;
Expand All @@ -13,15 +12,13 @@ namespace osu.Server.Spectator.Tests.Multiplayer
{
public class TestMultiplayerHub : MultiplayerHub
{
public new MultiplayerHubContext HubContext => base.HubContext;

public TestMultiplayerHub(
ILoggerFactory loggerFactory,
EntityStore<ServerMultiplayerRoom> rooms,
EntityStore<MultiplayerClientState> users,
IDatabaseFactory databaseFactory,
ChatFilters chatFilters,
IHubContext<MultiplayerHub> hubContext,
IMultiplayerHubContext hubContext,
ISharedInterop sharedInterop,
MultiplayerEventLogger multiplayerEventLogger,
IMatchmakingQueueBackgroundService matchmakingQueueBackgroundService)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ public static IServiceCollection AddHubEntities(this IServiceCollection serviceC
.AddHostedService<IDailyChallengeUpdater>(ctx => ctx.GetRequiredService<IDailyChallengeUpdater>())
.AddSingleton<MultiplayerEventLogger>()
.AddSingleton<IMatchmakingQueueBackgroundService, MatchmakingQueueBackgroundService>()
.AddHostedService<IMatchmakingQueueBackgroundService>(ctx => ctx.GetRequiredService<IMatchmakingQueueBackgroundService>());
.AddHostedService<IMatchmakingQueueBackgroundService>(ctx => ctx.GetRequiredService<IMatchmakingQueueBackgroundService>())
.AddSingleton<IMultiplayerHubContext, MultiplayerHubContext>();
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using osu.Game.Online.API;
using osu.Game.Online.Multiplayer;
using osu.Game.Online.Rooms;
Expand Down Expand Up @@ -147,5 +149,9 @@ public interface IMultiplayerHubContext
Task NotifyMatchmakingItemSelected(ServerMultiplayerRoom room, int userId, long playlistItemId);

Task NotifyMatchmakingItemDeselected(ServerMultiplayerRoom room, int userId, long playlistItemId);

void Log(ServerMultiplayerRoom room, MultiplayerRoomUser? user, string message, LogLevel logLevel = LogLevel.Information);

void Error(MultiplayerRoomUser? user, string message, Exception exception);
}
}
Loading
Loading