NetworkServerTest: OnDataReceivedInvalidConnectionId simplified

This commit is contained in:
vis2k 2021-06-15 14:16:55 +08:00
parent e827c6c0f3
commit b08cc62fd4

View File

@ -373,36 +373,24 @@ public void SendClientToServerMessage()
[Test] [Test]
public void OnDataReceivedInvalidConnectionId() public void OnDataReceivedInvalidConnectionId()
{ {
// add one custom message handler // register a message handler
bool wasReceived = false; int called = 0;
NetworkConnection connectionReceived = null; NetworkServer.RegisterHandler<TestMessage1>((conn, msg) => ++called, false);
TestMessage1 messageReceived = new TestMessage1();
NetworkServer.RegisterHandler<TestMessage1>((conn, msg) =>
{
wasReceived = true;
connectionReceived = conn;
messageReceived = msg;
}, false);
// listen // listen
NetworkServer.Listen(1); NetworkServer.Listen(1);
Assert.That(NetworkServer.connections.Count, Is.EqualTo(0));
// serialize a test message into an arraysegment // serialize a test message into an arraysegment
TestMessage1 testMessage = new TestMessage1 { IntValue = 13, DoubleValue = 14, StringValue = "15" }; byte[] message = MessagePackingTest.PackToByteArray(new TestMessage1());
NetworkWriter writer = new NetworkWriter();
MessagePacking.Pack(testMessage, writer);
ArraySegment<byte> segment = writer.ToArraySegment();
// call transport.OnDataReceived with an invalid connectionId // call transport.OnDataReceived with an invalid connectionId
// an error log is expected. // an error log is expected.
LogAssert.ignoreFailingMessages = true; LogAssert.ignoreFailingMessages = true;
transport.OnServerDataReceived.Invoke(42, segment, 0); transport.OnServerDataReceived.Invoke(42, new ArraySegment<byte>(message), 0);
LogAssert.ignoreFailingMessages = false; LogAssert.ignoreFailingMessages = false;
// message handler should never be called // message handler should never be called
Assert.That(wasReceived, Is.False); Assert.That(called, Is.EqualTo(0));
Assert.That(connectionReceived, Is.Null);
} }
[Test] [Test]