-
Notifications
You must be signed in to change notification settings - Fork 461
test: Added RPC test #822
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
test: Added RPC test #822
Changes from all commits
03db191
e476147
c8b478f
afa4a63
6509976
7b6758b
0a98121
7090b89
93bb348
70ad5df
cd52c70
a69dcf9
d45efa9
ca18d9f
4cb908d
0395e1d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,144 @@ | ||
| using System; | ||
| using System.Collections; | ||
| using System.Diagnostics; | ||
| using System.Linq; | ||
| using MLAPI.Messaging; | ||
| using NUnit.Framework; | ||
| using UnityEditor; | ||
| using UnityEngine; | ||
| using UnityEngine.TestTools; | ||
| using Debug = UnityEngine.Debug; | ||
|
|
||
| namespace MLAPI.RuntimeTests | ||
| { | ||
| public class RPCTests | ||
| { | ||
| public class RPCTestNetworkBehaviour : NetworkBehaviour | ||
| { | ||
| public event Action OnServer_RPC; | ||
| public event Action OnClient_RPC; | ||
|
|
||
| [ServerRpc] | ||
| public void MyServerRpc() | ||
| { | ||
| OnServer_RPC(); | ||
| } | ||
|
|
||
| [ClientRpc] | ||
| public void MyClientRpc() | ||
| { | ||
| OnClient_RPC(); | ||
| } | ||
| } | ||
|
|
||
| [UnityTest] | ||
| public IEnumerator TestRPCs() | ||
| { | ||
| // Set target frameRate to work around ubuntu timings | ||
| int targetFrameRate = Application.targetFrameRate; | ||
| Application.targetFrameRate = 120; | ||
|
|
||
| // Create multiple NetworkManager instances | ||
| if (!MultiInstanceHelpers.Create(1, out NetworkManager server, out NetworkManager[] clients)) | ||
| { | ||
| Debug.LogError("Failed to create instances"); | ||
| Assert.Fail("Failed to create instances"); | ||
| } | ||
|
|
||
| /* | ||
| * Normally we would only allow player prefabs to be set to a prefab. Not runtime created objects. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems like this comment would go with the function definition of MakeNetworkedObjectTestPrefab |
||
| * In order to prevent having a Resource folder full of a TON of prefabs that we have to maintain, | ||
| * MultiInstanceHelper has a helper function that lets you mark a runtime created object to be | ||
| * treated as a prefab by the MLAPI. That's how we can get away with creating the player prefab | ||
| * at runtime without it being treated as a SceneObject or causing other conflicts with the MLAPI. | ||
| */ | ||
|
|
||
| // Create playerPrefab | ||
| var playerPrefab = new GameObject("Player"); | ||
| NetworkObject networkObject = playerPrefab.AddComponent<NetworkObject>(); | ||
| playerPrefab.AddComponent<RPCTestNetworkBehaviour>(); | ||
|
|
||
| // Make it a prefab | ||
| MultiInstanceHelpers.MakeNetworkedObjectTestPrefab(networkObject); | ||
|
|
||
| // Set the player prefab | ||
| server.NetworkConfig.PlayerPrefab = playerPrefab; | ||
|
|
||
| for (int i = 0; i < clients.Length; i++) | ||
| { | ||
| clients[i].NetworkConfig.PlayerPrefab = playerPrefab; | ||
| } | ||
|
|
||
| // Start the instances | ||
| if (!MultiInstanceHelpers.Start(true, server, clients)) | ||
| { | ||
| Debug.LogError("Failed to start instances"); | ||
| Assert.Fail("Failed to start instances"); | ||
| } | ||
|
|
||
|
|
||
| // Wait for connection on client side | ||
| yield return MultiInstanceHelpers.Run(MultiInstanceHelpers.WaitForClientsConnected(clients)); | ||
|
|
||
| // Wait for connection on server side | ||
| yield return MultiInstanceHelpers.Run(MultiInstanceHelpers.WaitForClientConnectedToServer(server)); | ||
|
|
||
| // This is the *SERVER VERSION* of the *CLIENT PLAYER* | ||
| var serverClientPlayerResult = new MultiInstanceHelpers.CoroutineResultWrapper<NetworkObject>(); | ||
| yield return MultiInstanceHelpers.Run(MultiInstanceHelpers.GetNetworkObjectByRepresentation((x => x.IsPlayerObject && x.OwnerClientId == clients[0].LocalClientId), server, serverClientPlayerResult)); | ||
|
|
||
| // This is the *CLIENT VERSION* of the *CLIENT PLAYER* | ||
| var clientClientPlayerResult = new MultiInstanceHelpers.CoroutineResultWrapper<NetworkObject>(); | ||
| yield return MultiInstanceHelpers.Run(MultiInstanceHelpers.GetNetworkObjectByRepresentation((x => x.IsPlayerObject && x.OwnerClientId == clients[0].LocalClientId), clients[0], clientClientPlayerResult)); | ||
|
|
||
| // Setup state | ||
| bool hasReceivedServerRPC = false; | ||
| bool hasReceivedClientRPCRemotely = false; | ||
| bool hasReceivedClientRPCLocally = false; | ||
|
|
||
| clientClientPlayerResult.Result.GetComponent<RPCTestNetworkBehaviour>().OnClient_RPC += () => | ||
| { | ||
| Debug.Log("ClientRPC received on client object"); | ||
| hasReceivedClientRPCRemotely = true; | ||
| }; | ||
|
|
||
| clientClientPlayerResult.Result.GetComponent<RPCTestNetworkBehaviour>().OnServer_RPC += () => | ||
| { | ||
| // The RPC invoked locally. (Weaver failure?) | ||
| Assert.Fail("ServerRPC invoked locally. Weaver failure?"); | ||
| }; | ||
|
|
||
| serverClientPlayerResult.Result.GetComponent<RPCTestNetworkBehaviour>().OnServer_RPC += () => | ||
| { | ||
| Debug.Log("ServerRPC received on server object"); | ||
| hasReceivedServerRPC = true; | ||
| }; | ||
|
|
||
| serverClientPlayerResult.Result.GetComponent<RPCTestNetworkBehaviour>().OnClient_RPC += () => | ||
| { | ||
| // The RPC invoked locally. (Weaver failure?) | ||
| Debug.Log("ClientRPC received on server object"); | ||
| hasReceivedClientRPCLocally = true; | ||
| }; | ||
|
|
||
| // Send ServerRPC | ||
| clientClientPlayerResult.Result.GetComponent<RPCTestNetworkBehaviour>().MyServerRpc(); | ||
|
|
||
| // Send ClientRPC | ||
| serverClientPlayerResult.Result.GetComponent<RPCTestNetworkBehaviour>().MyClientRpc(); | ||
|
|
||
| // Wait for RPCs to be received | ||
| yield return MultiInstanceHelpers.Run(MultiInstanceHelpers.WaitForCondition(() => hasReceivedServerRPC && hasReceivedClientRPCLocally && hasReceivedClientRPCRemotely)); | ||
|
|
||
| Assert.True(hasReceivedServerRPC, "ServerRPC was not received"); | ||
| Assert.True(hasReceivedClientRPCLocally, "ClientRPC was not locally received on the server"); | ||
| Assert.True(hasReceivedClientRPCRemotely, "ClientRPC was not remotely received on the client"); | ||
|
|
||
| // Release frame rate | ||
| Application.targetFrameRate = targetFrameRate; | ||
|
|
||
| // Cleanup | ||
| MultiInstanceHelpers.Destroy(); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It might be useful to move this into a TearDown attribute decorated method so in the event there is a failure the rest of the unit tests that use MultiInstanceHelpers will not error out due to a SIPTransport already started error. |
||
| } | ||
| } | ||
| } | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
really cool. I tried reusing this in NetworkTransform tests and it works great. It feels like a lot of this should be put in a "Setup()" UnitySetUp method though. It'd make it easier to reuse with multiple test cases. And by doing this, you'll notice you have a Create, but no Destroy method for your helper. (this could be done in a followup PR for sure)