NetworkBehaviourTests: SendRPCInternal

This commit is contained in:
vis2k 2020-03-06 09:44:44 +01:00
parent 644626294b
commit 7656a833c9

View File

@ -33,6 +33,26 @@ public void CallSendCommandInternal()
}
}
// we need to inherit from networkbehaviour to test protected functions
public class NetworkBehaviourSendRPCInternalComponent : NetworkBehaviour
{
// counter to make sure that it's called exactly once
public int called;
// weaver generates this from [Command]
// but for tests we need to add it manually
public static void RPCGenerated(NetworkBehaviour comp, NetworkReader reader)
{
++((NetworkBehaviourSendRPCInternalComponent)comp).called;
}
// SendCommandInternal is protected. let's expose it so we can test it.
public void CallSendRPCInternal()
{
SendRPCInternal(GetType(), nameof(RPCGenerated), new NetworkWriter(), 0);
}
}
public class NetworkBehaviourTests
{
GameObject gameObject;
@ -52,6 +72,7 @@ public void SetUp()
[TearDown]
public void TearDown()
{
NetworkServer.RemoveLocalConnection();
GameObject.DestroyImmediate(gameObject);
}
@ -246,6 +267,91 @@ public void InvokeCommand()
// clean up
NetworkBehaviour.ClearDelegates();
}
[Test]
public void SendRPCInternal()
{
// add rpc component
NetworkBehaviourSendRPCInternalComponent comp = gameObject.AddComponent<NetworkBehaviourSendRPCInternalComponent>();
Assert.That(comp.called, Is.EqualTo(0));
// transport is needed by server and client.
// it needs to be on a gameobject because client.connect enables it,
// which throws a NRE if not on a gameobject
GameObject transportGO = new GameObject();
Transport.activeTransport = transportGO.AddComponent<MemoryTransport>();
// calling rpc before server is active shouldn't work
LogAssert.Expect(LogType.Error, "RPC Function " + nameof(NetworkBehaviourSendRPCInternalComponent.RPCGenerated) + " called on Client.");
comp.CallSendRPCInternal();
Assert.That(comp.called, Is.EqualTo(0));
// we need to start a server and connect a client in order to be
// able to send commands
// message handlers
NetworkServer.RegisterHandler<ConnectMessage>((conn, msg) => {}, false);
NetworkServer.RegisterHandler<DisconnectMessage>((conn, msg) => {}, false);
NetworkServer.RegisterHandler<ErrorMessage>((conn, msg) => {}, false);
NetworkServer.RegisterHandler<SpawnMessage>((conn, msg) => {}, false);
NetworkServer.Listen(1);
Assert.That(NetworkServer.active, Is.True);
// connect host client
NetworkClient.ConnectHost();
Assert.That(NetworkClient.active, Is.True);
// get the host connection which already has client->server and
// server->client set up
ULocalConnectionToServer connectionToServer = (ULocalConnectionToServer)NetworkClient.connection;
// set host connection as ready and authenticated
connectionToServer.isReady = true;
connectionToServer.isAuthenticated = true;
connectionToServer.connectionToClient.isReady = true;
connectionToServer.connectionToClient.isAuthenticated = true;
connectionToServer.connectionToClient.identity = identity;
// calling rpc before isServer is true shouldn't work
LogAssert.Expect(LogType.Warning, "ClientRpc " + nameof(NetworkBehaviourSendRPCInternalComponent.RPCGenerated) + " called on un-spawned object: " + gameObject.name);
comp.CallSendRPCInternal();
Assert.That(comp.called, Is.EqualTo(0));
// we need an observer because sendrpc sends to ready observers
identity.OnStartServer(); // creates observers
identity.observers[connectionToServer.connectionToClient.connectionId] = connectionToServer.connectionToClient;
identity.netId = 42;
// isServer needs to be true, otherwise we can't call rpcs
Assert.That(comp.isServer, Is.True);
// register the command delegate, otherwise it's not found
NetworkBehaviour.RegisterRpcDelegate(typeof(NetworkBehaviourSendRPCInternalComponent),
nameof(NetworkBehaviourSendRPCInternalComponent.RPCGenerated),
NetworkBehaviourSendRPCInternalComponent.RPCGenerated);
// identity needs to be in spawned dict, otherwise rpc handler
// won't find it
NetworkIdentity.spawned[identity.netId] = identity;
// call rpc
comp.CallSendRPCInternal();
// update client's connection so that pending messages are processed
connectionToServer.Update();
// rpc should have been called now
Assert.That(comp.called, Is.EqualTo(1));
// clean up
NetworkBehaviour.ClearDelegates();
ClientScene.Shutdown(); // clear clientscene.readyconnection
NetworkServer.RemoveLocalConnection();
NetworkClient.Shutdown();
NetworkServer.Shutdown();
Transport.activeTransport = null;
GameObject.DestroyImmediate(transportGO);
}
}
// we need to inherit from networkbehaviour to test protected functions