diff --git a/com.unity.multiplayer.mlapi/Tests/Runtime/MultiInstance/MultiInstanceHelpers.cs b/com.unity.multiplayer.mlapi/Tests/Runtime/MultiInstance/MultiInstanceHelpers.cs index bf0a78fee3..02cc898965 100644 --- a/com.unity.multiplayer.mlapi/Tests/Runtime/MultiInstance/MultiInstanceHelpers.cs +++ b/com.unity.multiplayer.mlapi/Tests/Runtime/MultiInstance/MultiInstanceHelpers.cs @@ -18,6 +18,7 @@ public static class MultiInstanceHelpers private static List s_NetworkManagerInstances = new List(); private static bool s_IsStarted; private static int s_ClientCount; + private static int s_OriginalTargetFrameRate = -1; /// /// Creates NetworkingManagers and configures them for use in a multi instance setting. @@ -25,7 +26,7 @@ public static class MultiInstanceHelpers /// The amount of clients /// The server NetworkManager /// The clients NetworkManagers - public static bool Create(int clientCount, out NetworkManager server, out NetworkManager[] clients) + public static bool Create(int clientCount, out NetworkManager server, out NetworkManager[] clients, int targetFrameRate = 60) { s_NetworkManagerInstances = new List(); @@ -49,6 +50,9 @@ public static bool Create(int clientCount, out NetworkManager server, out Networ }; } + s_OriginalTargetFrameRate = Application.targetFrameRate; + Application.targetFrameRate = targetFrameRate; + return true; } @@ -129,6 +133,8 @@ public static void Destroy() { Object.Destroy(s_CoroutineRunner); } + + Application.targetFrameRate = s_OriginalTargetFrameRate; } /// diff --git a/com.unity.multiplayer.mlapi/Tests/Runtime/MultiInstance/RPCTests.cs b/com.unity.multiplayer.mlapi/Tests/Runtime/MultiInstance/RPCTests.cs index ddf24ce674..cb30ca511a 100644 --- a/com.unity.multiplayer.mlapi/Tests/Runtime/MultiInstance/RPCTests.cs +++ b/com.unity.multiplayer.mlapi/Tests/Runtime/MultiInstance/RPCTests.cs @@ -1,10 +1,7 @@ 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; @@ -34,10 +31,6 @@ public void MyClientRpc() [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)) { @@ -134,9 +127,6 @@ public IEnumerator TestRPCs() 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(); } diff --git a/com.unity.multiplayer.mlapi/Tests/Runtime/NetworkSpawnManagerTests.cs b/com.unity.multiplayer.mlapi/Tests/Runtime/NetworkSpawnManagerTests.cs index bc868e3429..e88b4c7b5a 100644 --- a/com.unity.multiplayer.mlapi/Tests/Runtime/NetworkSpawnManagerTests.cs +++ b/com.unity.multiplayer.mlapi/Tests/Runtime/NetworkSpawnManagerTests.cs @@ -1,10 +1,8 @@ -using System; using System.Collections; using MLAPI.Exceptions; using NUnit.Framework; using UnityEngine; using UnityEngine.TestTools; -using Object = System.Object; namespace MLAPI.RuntimeTests { @@ -13,7 +11,6 @@ public class NetworkSpawnManagerTests private NetworkManager m_ServerNetworkManager; private NetworkManager[] m_ClientNetworkManagers; private GameObject m_PlayerPrefab; - private int m_OriginalTargetFrameRate; private ulong serverSideClientId => m_ServerNetworkManager.ServerClientId; private ulong clientSideClientId => m_ClientNetworkManagers[0].LocalClientId; @@ -22,16 +19,6 @@ public class NetworkSpawnManagerTests [UnitySetUp] public IEnumerator Setup() { - // Just always track the current target frame rate (will be re-applied upon TearDown) - m_OriginalTargetFrameRate = Application.targetFrameRate; - - // Since we use frame count as a metric, we need to assure it runs at a "common update rate" - // between platforms (i.e. Ubuntu seems to run at much higher FPS when set to -1) - if (Application.targetFrameRate < 0 || Application.targetFrameRate > 120) - { - Application.targetFrameRate = 120; - } - // Create multiple NetworkManager instances if (!MultiInstanceHelpers.Create(2, out NetworkManager server, out NetworkManager[] clients)) { @@ -191,11 +178,11 @@ public IEnumerator Teardown() { // Shutdown and clean up both of our NetworkManager instances MultiInstanceHelpers.Destroy(); - UnityEngine.Object.Destroy(m_PlayerPrefab); + Object.Destroy(m_PlayerPrefab); - // Set the application's target frame rate back to its original value - Application.targetFrameRate = m_OriginalTargetFrameRate; - yield return new WaitForSeconds(0); // wait for next frame so everything is destroyed, so following tests can execute from clean environment + // wait for next frame so everything is destroyed, so following tests can execute from clean environment + int nextFrameNumber = Time.frameCount + 1; + yield return new WaitUntil(() => Time.frameCount >= nextFrameNumber); } } } diff --git a/testproject/Assets/Tests/Runtime/MultiClientConnectionApproval.cs b/testproject/Assets/Tests/Runtime/MultiClientConnectionApproval.cs index d8866eb658..88f04048fd 100644 --- a/testproject/Assets/Tests/Runtime/MultiClientConnectionApproval.cs +++ b/testproject/Assets/Tests/Runtime/MultiClientConnectionApproval.cs @@ -20,22 +20,6 @@ public class MultiClientConnectionApproval private GameObject m_PlayerPrefab; private GameObject m_PlayerPrefabOverride; - private int m_OriginalTargetFrameRate; - - [SetUp] - public void SetUp() - { - // Just always track the current target frame rate (will be re-applied upon TearDown) - m_OriginalTargetFrameRate = Application.targetFrameRate; - - // Since we use frame count as a metric, we need to assure it runs at a "common update rate" - // between platforms (i.e. Ubuntu seems to run at much higher FPS when set to -1) - if (Application.targetFrameRate < 0 || Application.targetFrameRate > 120) - { - Application.targetFrameRate = 120; - } - } - /// /// Tests connection approval and connection approval failure /// @@ -44,7 +28,7 @@ public void SetUp() public IEnumerator ConnectionApproval() { m_ConnectionToken = "ThisIsTheRightPassword"; - return ConnectionApprovalHandler(3,1); + return ConnectionApprovalHandler(3, 1); } /// @@ -60,7 +44,7 @@ public IEnumerator ConnectionApprovalPrefabOverride() /// - /// Allows for several connection approval related configurations + /// Allows for several connection approval related configurations /// /// total number of clients (excluding the host) /// how many clients are expected to fail @@ -68,20 +52,14 @@ public IEnumerator ConnectionApprovalPrefabOverride() /// private IEnumerator ConnectionApprovalHandler(int numClients, int failureTestCount = 1, bool prefabOverride = false) { - Debug.Log($"Application.targetFrameRate = {Application.targetFrameRate}"); - if (Application.targetFrameRate == -1 || Application.targetFrameRate > 120) - { - Application.targetFrameRate = 120; - } - var startFrameCount = Time.frameCount; var startTime = Time.realtimeSinceStartup; m_SuccessfulConnections = 0; m_FailedConnections = 0; Assert.IsTrue(numClients >= failureTestCount); - - // Create Host and (numClients) clients + + // Create Host and (numClients) clients Assert.True(MultiInstanceHelpers.Create(numClients, out NetworkManager server, out NetworkManager[] clients)); // Create a default player GameObject to use @@ -131,7 +109,7 @@ private IEnumerator ConnectionApprovalHandler(int numClients, int failureTestCou client.NetworkConfig.ConnectionData = Encoding.ASCII.GetBytes(m_ConnectionToken); clientsAdjustedList.Add(client); } - + } // Start the instances @@ -141,7 +119,7 @@ private IEnumerator ConnectionApprovalHandler(int numClients, int failureTestCou Assert.Fail("Failed to start instances"); } - // [Client-Side] Wait for a connection to the server + // [Client-Side] Wait for a connection to the server yield return MultiInstanceHelpers.Run(MultiInstanceHelpers.WaitForClientsConnected(clientsAdjustedList.ToArray(), null, 512)); // [Host-Side] Check to make sure all clients are connected @@ -156,7 +134,7 @@ private IEnumerator ConnectionApprovalHandler(int numClients, int failureTestCou // If we are doing player prefab overrides, then check all of the players to make sure they spawned the appropriate NetworkObject if (prefabOverride) { - foreach(var networkClient in server.ConnectedClientsList) + foreach (var networkClient in server.ConnectedClientsList) { Assert.IsNotNull(networkClient.PlayerObject); Assert.AreEqual(networkClient.PlayerObject.GlobalObjectIdHash, m_PrefabOverrideGlobalObjectIdHash); @@ -171,7 +149,6 @@ private IEnumerator ConnectionApprovalHandler(int numClients, int failureTestCou server.ConnectionApprovalCallback -= ConnectionApprovalCallback; server.StopHost(); - Debug.Log($"Application.targetFrameRate = {Application.targetFrameRate}."); Debug.Log($"Total frames updated = {Time.frameCount - startFrameCount} within {Time.realtimeSinceStartup - startTime} seconds."); } @@ -186,7 +163,7 @@ private void ConnectionApprovalCallback(byte[] connectionData, ulong clientId, N string approvalToken = Encoding.ASCII.GetString(connectionData); var isApproved = approvalToken == m_ConnectionToken; - if(isApproved) + if (isApproved) { m_SuccessfulConnections++; } @@ -222,9 +199,6 @@ public void TearDown() // Shutdown and clean up both of our NetworkManager instances MultiInstanceHelpers.Destroy(); - - // Set the application's target frame rate back to its original value - Application.targetFrameRate = m_OriginalTargetFrameRate; } } } diff --git a/testproject/Assets/Tests/Runtime/RpcINetworkSerializable.cs b/testproject/Assets/Tests/Runtime/RpcINetworkSerializable.cs index d860fb3207..e386637519 100644 --- a/testproject/Assets/Tests/Runtime/RpcINetworkSerializable.cs +++ b/testproject/Assets/Tests/Runtime/RpcINetworkSerializable.cs @@ -15,8 +15,6 @@ public class RpcINetworkSerializable { private GameObject m_PlayerPrefab; - private int m_OriginalTargetFrameRate; - private UserSerializableClass m_UserSerializableClass; private List m_UserSerializableClassArray; @@ -25,20 +23,6 @@ public class RpcINetworkSerializable private bool m_IsSendingNull; private bool m_IsArrayEmpty; - [SetUp] - public void SetUp() - { - // Just always track the current target frame rate (will be re-applied upon TearDown) - m_OriginalTargetFrameRate = Application.targetFrameRate; - - // Since we use frame count as a metric, we need to assure it runs at a "common update rate" - // between platforms (i.e. Ubuntu seems to run at much higher FPS when set to -1) - if (Application.targetFrameRate < 0 || Application.targetFrameRate > 120) - { - Application.targetFrameRate = 120; - } - } - /// /// Tests that INetworkSerializable can be used through RPCs by a user /// @@ -50,7 +34,7 @@ public IEnumerator NetworkSerializableTest() var numClients = 1; var startTime = Time.realtimeSinceStartup; - // Create Host and (numClients) clients + // Create Host and (numClients) clients Assert.True(MultiInstanceHelpers.Create(numClients, out NetworkManager server, out NetworkManager[] clients)); // Create a default player GameObject to use @@ -76,7 +60,7 @@ public IEnumerator NetworkSerializableTest() Assert.Fail("Failed to start instances"); } - // [Client-Side] Wait for a connection to the server + // [Client-Side] Wait for a connection to the server yield return MultiInstanceHelpers.Run(MultiInstanceHelpers.WaitForClientsConnected(clients, null, 512)); // [Host-Side] Check to make sure all clients are connected @@ -174,7 +158,7 @@ public IEnumerator NetworkSerializableEmptyArrayTest() [UnityTest] public IEnumerator NetworkSerializableNULLArrayTest() { - return NetworkSerializableArrayTestHandler(0,true); + return NetworkSerializableArrayTestHandler(0, true); } /// @@ -197,7 +181,7 @@ public IEnumerator NetworkSerializableArrayTestHandler(int arraySize, bool sendN var numClients = 1; var startTime = Time.realtimeSinceStartup; - // Create Host and (numClients) clients + // Create Host and (numClients) clients Assert.True(MultiInstanceHelpers.Create(numClients, out NetworkManager server, out NetworkManager[] clients)); // Create a default player GameObject to use @@ -223,7 +207,7 @@ public IEnumerator NetworkSerializableArrayTestHandler(int arraySize, bool sendN Assert.Fail("Failed to start instances"); } - // [Client-Side] Wait for a connection to the server + // [Client-Side] Wait for a connection to the server yield return MultiInstanceHelpers.Run(MultiInstanceHelpers.WaitForClientsConnected(clients, null, 512)); // [Host-Side] Check to make sure all clients are connected @@ -245,7 +229,7 @@ public IEnumerator NetworkSerializableArrayTestHandler(int arraySize, bool sendN if (!m_IsSendingNull) { - // Create an array of userSerializableClass instances + // Create an array of userSerializableClass instances for (int i = 0; i < arraySize; i++) { var userSerializableClass = new UserSerializableClass(); @@ -297,7 +281,7 @@ private void ValidateUserSerializableClasses(UserSerializableClass[] userSeriali } else if (m_IsArrayEmpty) { - Assert.AreEqual(userSerializableClass.Length,0); + Assert.AreEqual(userSerializableClass.Length, 0); } else { @@ -343,9 +327,6 @@ public void TearDown() // Shutdown and clean up both of our NetworkManager instances MultiInstanceHelpers.Destroy(); - - // Set the application's target frame rate back to its original value - Application.targetFrameRate = m_OriginalTargetFrameRate; } } @@ -458,7 +439,7 @@ private void SendClientSerializedDataClientRpc(UserSerializableClass[] userSeria } /// - /// The test version of a custom user-defined class that implements INetworkSerializable + /// The test version of a custom user-defined class that implements INetworkSerializable /// public class UserSerializableClass : INetworkSerializable { diff --git a/testproject/Assets/Tests/Runtime/RpcTestsAutomated.cs b/testproject/Assets/Tests/Runtime/RpcTestsAutomated.cs index ae7277bb60..83e79db1ff 100644 --- a/testproject/Assets/Tests/Runtime/RpcTestsAutomated.cs +++ b/testproject/Assets/Tests/Runtime/RpcTestsAutomated.cs @@ -13,27 +13,9 @@ namespace TestProject.RuntimeTests public class RPCTestsAutomated { private bool m_TimedOut; - private int m_MaxFrames; - private GameObject m_PlayerPrefab; - private int m_OriginalTargetFrameRate; - - [SetUp] - public void SetUp() - { - // Just always track the current target frame rate (will be re-applied upon TearDown) - m_OriginalTargetFrameRate = Application.targetFrameRate; - - // Since we use frame count as a metric, we need to assure it runs at a "common update rate" - // between platforms (i.e. Ubuntu seems to run at much higher FPS when set to -1) - if (Application.targetFrameRate < 0 || Application.targetFrameRate > 120) - { - Application.targetFrameRate = 120; - } - } - /// /// Default Mode (Batched RPCs Enabled) /// @@ -57,7 +39,7 @@ public IEnumerator ManualRpcTestsAutomatedNoBatching() /// /// This just helps to simplify any further tests that can leverage from /// the RpcQueueManualTests' wide array of RPC testing under different - /// conditions. + /// conditions. /// Currently this allows for the adjustment of client count and whether /// RPC Batching is enabled or not. /// @@ -72,7 +54,7 @@ private IEnumerator AutomatedRpcTestsHandler(int numClients, bool useBatching = // Set RpcQueueManualTests into unit testing mode RpcQueueManualTests.UnitTesting = true; - // Create Host and (numClients) clients + // Create Host and (numClients) clients Assert.True(MultiInstanceHelpers.Create(numClients, out NetworkManager server, out NetworkManager[] clients)); // Create a default player GameObject to use @@ -110,7 +92,7 @@ private IEnumerator AutomatedRpcTestsHandler(int numClients, bool useBatching = clients[i].RpcQueueContainer.EnableBatchedRpcs(useBatching); } - // [Client-Side] Wait for a connection to the server + // [Client-Side] Wait for a connection to the server yield return MultiInstanceHelpers.Run(MultiInstanceHelpers.WaitForClientsConnected(clients, null, 512)); // [Host-Side] Check to make sure all clients are connected @@ -197,11 +179,9 @@ public void TearDown() Object.Destroy(m_PlayerPrefab); m_PlayerPrefab = null; } + // Shutdown and clean up both of our NetworkManager instances MultiInstanceHelpers.Destroy(); - - // Set the application's target frame rate back to its original value - Application.targetFrameRate = m_OriginalTargetFrameRate; } } }