2021-08-19 08:40:59 +00: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.
|
|
|
|
|
|
|
|
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 04:04:41 +00:00
|
|
|
byte[] serialized = MessagePackSerializer.Serialize(room);
|
2021-08-19 08:40:59 +00:00
|
|
|
|
|
|
|
var deserialized = MessagePackSerializer.Deserialize<MultiplayerRoom>(serialized);
|
|
|
|
|
|
|
|
Assert.IsTrue(deserialized.MatchState is TeamVersusRoomState);
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void TestSerialiseUserStateExpected()
|
|
|
|
{
|
|
|
|
var state = new TeamVersusUserState();
|
|
|
|
|
2021-10-27 04:04:41 +00:00
|
|
|
byte[] serialized = MessagePackSerializer.Serialize(typeof(MatchUserState), state);
|
2021-08-19 08:40:59 +00:00
|
|
|
var deserialized = MessagePackSerializer.Deserialize<MatchUserState>(serialized);
|
|
|
|
|
|
|
|
Assert.IsTrue(deserialized is TeamVersusUserState);
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
2021-12-10 05:04:31 +00:00
|
|
|
public void TestSerialiseUnionFailsWithSignalR()
|
2021-08-19 08:40:59 +00:00
|
|
|
{
|
|
|
|
var state = new TeamVersusUserState();
|
|
|
|
|
|
|
|
// SignalR serialises using the actual type, rather than a base specification.
|
2021-10-27 04:04:41 +00:00
|
|
|
byte[] serialized = MessagePackSerializer.Serialize(typeof(TeamVersusUserState), state);
|
2021-08-19 08:40:59 +00: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 04:04:41 +00:00
|
|
|
byte[] serialized = MessagePackSerializer.Serialize(typeof(TeamVersusUserState), state, SignalRUnionWorkaroundResolver.OPTIONS);
|
2021-08-19 08:40:59 +00: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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|