2021-08-19 16:40:59 +08:00
|
|
|
// 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.
|
|
|
|
|
2022-06-17 15:37:17 +08:00
|
|
|
#nullable disable
|
|
|
|
|
2021-08-19 16:40:59 +08:00
|
|
|
using MessagePack;
|
|
|
|
using NUnit.Framework;
|
|
|
|
using osu.Game.Online;
|
|
|
|
using osu.Game.Online.Multiplayer;
|
|
|
|
using osu.Game.Online.Multiplayer.MatchTypes.TeamVersus;
|
|
|
|
|
|
|
|
namespace osu.Game.Tests.Online
|
|
|
|
{
|
|
|
|
[TestFixture]
|
|
|
|
public class TestMultiplayerMessagePackSerialization
|
|
|
|
{
|
|
|
|
[Test]
|
|
|
|
public void TestSerialiseRoom()
|
|
|
|
{
|
|
|
|
var room = new MultiplayerRoom(1)
|
|
|
|
{
|
|
|
|
MatchState = new TeamVersusRoomState()
|
|
|
|
};
|
|
|
|
|
2021-10-27 12:04:41 +08:00
|
|
|
byte[] serialized = MessagePackSerializer.Serialize(room);
|
2021-08-19 16:40:59 +08:00
|
|
|
|
|
|
|
var deserialized = MessagePackSerializer.Deserialize<MultiplayerRoom>(serialized);
|
|
|
|
|
|
|
|
Assert.IsTrue(deserialized.MatchState is TeamVersusRoomState);
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void TestSerialiseUserStateExpected()
|
|
|
|
{
|
|
|
|
var state = new TeamVersusUserState();
|
|
|
|
|
2021-10-27 12:04:41 +08:00
|
|
|
byte[] serialized = MessagePackSerializer.Serialize(typeof(MatchUserState), state);
|
2021-08-19 16:40:59 +08:00
|
|
|
var deserialized = MessagePackSerializer.Deserialize<MatchUserState>(serialized);
|
|
|
|
|
|
|
|
Assert.IsTrue(deserialized is TeamVersusUserState);
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
2021-12-10 13:04:31 +08:00
|
|
|
public void TestSerialiseUnionFailsWithSignalR()
|
2021-08-19 16:40:59 +08:00
|
|
|
{
|
|
|
|
var state = new TeamVersusUserState();
|
|
|
|
|
|
|
|
// SignalR serialises using the actual type, rather than a base specification.
|
2021-10-27 12:04:41 +08:00
|
|
|
byte[] serialized = MessagePackSerializer.Serialize(typeof(TeamVersusUserState), state);
|
2021-08-19 16:40:59 +08:00
|
|
|
|
|
|
|
// works with explicit type specified.
|
|
|
|
MessagePackSerializer.Deserialize<TeamVersusUserState>(serialized);
|
|
|
|
|
|
|
|
// fails with base (union) type.
|
|
|
|
Assert.Throws<MessagePackSerializationException>(() => MessagePackSerializer.Deserialize<MatchUserState>(serialized));
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void TestSerialiseUnionSucceedsWithWorkaround()
|
|
|
|
{
|
|
|
|
var state = new TeamVersusUserState();
|
|
|
|
|
|
|
|
// SignalR serialises using the actual type, rather than a base specification.
|
2021-10-27 12:04:41 +08:00
|
|
|
byte[] serialized = MessagePackSerializer.Serialize(typeof(TeamVersusUserState), state, SignalRUnionWorkaroundResolver.OPTIONS);
|
2021-08-19 16:40:59 +08:00
|
|
|
|
|
|
|
// works with explicit type specified.
|
|
|
|
MessagePackSerializer.Deserialize<TeamVersusUserState>(serialized);
|
|
|
|
|
|
|
|
// works with custom resolver.
|
|
|
|
var deserialized = MessagePackSerializer.Deserialize<MatchUserState>(serialized, SignalRUnionWorkaroundResolver.OPTIONS);
|
|
|
|
Assert.IsTrue(deserialized is TeamVersusUserState);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|