From 0f362ec1cf5192a1340dd6157d0d3c37b82a8b3b Mon Sep 17 00:00:00 2001 From: vis2k Date: Tue, 3 Mar 2020 13:06:20 +0100 Subject: [PATCH] NetworkIdentityTests: HandleRPC --- .../Tests/Editor/NetworkIdentityTests.cs | 39 +++++++++++++++++++ .../Mirror/Tests/Editor/NetworkServerTest.cs | 12 ++++++ 2 files changed, 51 insertions(+) diff --git a/Assets/Mirror/Tests/Editor/NetworkIdentityTests.cs b/Assets/Mirror/Tests/Editor/NetworkIdentityTests.cs index bbce55902..665b21a0b 100644 --- a/Assets/Mirror/Tests/Editor/NetworkIdentityTests.cs +++ b/Assets/Mirror/Tests/Editor/NetworkIdentityTests.cs @@ -1250,6 +1250,45 @@ public void HandleCommand() GameObject.DestroyImmediate(gameObject); } + [Test] + public void HandleRpc() + { + // create a networkidentity with an rpc component + GameObject gameObject = new GameObject(); + NetworkIdentity identity = gameObject.AddComponent(); + RpcTestNetworkBehaviour comp0 = gameObject.AddComponent(); + Assert.That(comp0.called, Is.EqualTo(0)); + + // register the command delegate, otherwise it's not found + NetworkBehaviour.RegisterRpcDelegate(typeof(RpcTestNetworkBehaviour), nameof(RpcTestNetworkBehaviour.RpcGenerated), RpcTestNetworkBehaviour.RpcGenerated); + + // identity needs to be in spawned dict, otherwise command handler + // won't find it + NetworkIdentity.spawned[identity.netId] = identity; + + // serialize an RpcMessage message into an arraysegment + RpcMessage message = new RpcMessage { + componentIndex = 0, + functionHash = NetworkBehaviour.GetMethodHash(typeof(RpcTestNetworkBehaviour), nameof(RpcTestNetworkBehaviour.RpcGenerated)), + netId = identity.netId, + payload = new ArraySegment(new byte[0]) + }; + NetworkWriter writer = new NetworkWriter(); + MessagePacker.Pack(message, writer); + ArraySegment segment = writer.ToArraySegment(); + + // call HandleRpc + identity.HandleRPC(message.componentIndex, message.functionHash, new NetworkReader(segment)); + + // was the rpc called in the component? + Assert.That(comp0.called, Is.EqualTo(1)); + + // clean up + NetworkIdentity.spawned.Clear(); + NetworkBehaviour.ClearDelegates(); + GameObject.DestroyImmediate(gameObject); + } + [Test] public void ServerUpdate() { diff --git a/Assets/Mirror/Tests/Editor/NetworkServerTest.cs b/Assets/Mirror/Tests/Editor/NetworkServerTest.cs index 13abf237d..0cde451dc 100644 --- a/Assets/Mirror/Tests/Editor/NetworkServerTest.cs +++ b/Assets/Mirror/Tests/Editor/NetworkServerTest.cs @@ -19,6 +19,18 @@ public static void CommandGenerated(NetworkBehaviour comp, NetworkReader reader) } } + public class RpcTestNetworkBehaviour : NetworkBehaviour + { + // counter to make sure that it's called exactly once + public int called; + // weaver generates this from [Rpc] + // but for tests we need to add it manually + public static void RpcGenerated(NetworkBehaviour comp, NetworkReader reader) + { + ++((RpcTestNetworkBehaviour)comp).called; + } + } + public class OnStartClientTestNetworkBehaviour : NetworkBehaviour { // counter to make sure that it's called exactly once