// Copyright (c) ppy Pty Ltd . 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() }; var serialized = MessagePackSerializer.Serialize(room); var deserialized = MessagePackSerializer.Deserialize(serialized); Assert.IsTrue(deserialized.MatchState is TeamVersusRoomState); } [Test] public void TestSerialiseUserStateExpected() { var state = new TeamVersusUserState(); var serialized = MessagePackSerializer.Serialize(typeof(MatchUserState), state); var deserialized = MessagePackSerializer.Deserialize(serialized); Assert.IsTrue(deserialized is TeamVersusUserState); } [Test] public void TestSerialiseUnionFailsWithSingalR() { var state = new TeamVersusUserState(); // SignalR serialises using the actual type, rather than a base specification. var serialized = MessagePackSerializer.Serialize(typeof(TeamVersusUserState), state); // works with explicit type specified. MessagePackSerializer.Deserialize(serialized); // fails with base (union) type. Assert.Throws(() => MessagePackSerializer.Deserialize(serialized)); } [Test] public void TestSerialiseUnionSucceedsWithWorkaround() { var state = new TeamVersusUserState(); // SignalR serialises using the actual type, rather than a base specification. var serialized = MessagePackSerializer.Serialize(typeof(TeamVersusUserState), state, SignalRUnionWorkaroundResolver.OPTIONS); // works with explicit type specified. MessagePackSerializer.Deserialize(serialized); // works with custom resolver. var deserialized = MessagePackSerializer.Deserialize(serialized, SignalRUnionWorkaroundResolver.OPTIONS); Assert.IsTrue(deserialized is TeamVersusUserState); } } }