-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathCommandTests.cs
More file actions
67 lines (52 loc) · 2.11 KB
/
CommandTests.cs
File metadata and controls
67 lines (52 loc) · 2.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
using System;
using System.Linq;
using System.Runtime.InteropServices;
using MultiplayerMod.Multiplayer.Commands;
using MultiplayerMod.Network;
using MultiplayerMod.Platform.Common;
using MultiplayerMod.Platform.Common.Network.Messaging;
using NUnit.Framework;
namespace MultiplayerMod.Test.Network;
[TestFixture]
[Parallelizable]
public class CommandTests {
[Serializable]
private class Command : MultiplayerCommand {
public int Value { set; get; }
public override void Execute(MultiplayerCommandContext context) { }
}
[Serializable]
private class DataCommand : MultiplayerCommand {
public byte[] Data = new byte[Configuration.MaxMessageSize * 2];
public override void Execute(MultiplayerCommandContext context) { }
}
[Test]
public void TestSerializationDeserialization() {
var command = new Command { Value = 42 };
using var serialized = NetworkSerializer.Serialize(new NetworkMessage(command, MultiplayerCommandOptions.None));
var data = new byte[serialized.Size];
Marshal.Copy(serialized.Pointer, data, 0, (int) serialized.Size);
var dataHandle = GCHandle.Alloc(data, GCHandleType.Pinned);
var messageHandle = new NetworkMessageHandle(dataHandle.AddrOfPinnedObject(), (uint) data.Length);
var message = (NetworkMessage) NetworkSerializer.Deserialize(messageHandle);
Assert.AreNotSame(command, message.Command);
Assert.AreEqual(((Command) message.Command).Value, 42);
}
[Test]
public void TestNetworkMessageFragmentation() {
var factory = new NetworkMessageFactory();
var processor = new NetworkMessageProcessor();
var command = new DataCommand();
var fragmentsCount = 0;
var message = factory.Create(command, MultiplayerCommandOptions.None)
.Select(
fragment => {
fragmentsCount++;
return processor.Process(0, fragment);
}
)
.FirstOrDefault(it => it != null);
Assert.AreEqual(4, fragmentsCount);
Assert.NotNull(message);
}
}