From 1abfa3ff1f5e5fc06dacba928000aa746acaa9e8 Mon Sep 17 00:00:00 2001 From: Luke Stampfli Date: Wed, 26 May 2021 22:24:43 +0200 Subject: [PATCH 01/10] feat!: rename NetworkStart to OnNetworkSpawn, add OnNetworkDespawn --- .../Prototyping/NetworkTransform.cs | 2 +- .../Runtime/Core/NetworkBehaviour.cs | 23 +++++++++----- .../Runtime/Core/NetworkManager.cs | 4 +-- .../Runtime/Core/NetworkObject.cs | 30 ++++++------------ .../Messaging/InternalMessageHandler.cs | 2 +- .../Runtime/Spawning/NetworkSpawnManager.cs | 31 +++++++++---------- ...nableDisableSceneNetworkObjectComponent.cs | 4 +-- testproject/Assets/Scripts/PlayerMovement.cs | 4 +-- .../HybridScripts/RpcQueueManualTests.cs | 2 +- .../Scripts/ConnectionApprovalComponent.cs | 2 +- .../Tests/Manual/Scripts/NetworkPrefabPool.cs | 2 +- .../NetworkSceneManagerCallbackTests.cs | 2 +- .../Tests/Manual/Scripts/StatsDisplay.cs | 2 +- .../Manual/Scripts/SwitchSceneHandler.cs | 4 +-- 14 files changed, 54 insertions(+), 60 deletions(-) diff --git a/com.unity.multiplayer.mlapi/Prototyping/NetworkTransform.cs b/com.unity.multiplayer.mlapi/Prototyping/NetworkTransform.cs index 2e7ab08182..7b413ec809 100644 --- a/com.unity.multiplayer.mlapi/Prototyping/NetworkTransform.cs +++ b/com.unity.multiplayer.mlapi/Prototyping/NetworkTransform.cs @@ -188,7 +188,7 @@ private void Awake() m_Transform = transform; } - public override void NetworkStart() + public override void OnNetworkSpawn() { void SetupVar(NetworkVariable v, T initialValue, ref T oldVal) { diff --git a/com.unity.multiplayer.mlapi/Runtime/Core/NetworkBehaviour.cs b/com.unity.multiplayer.mlapi/Runtime/Core/NetworkBehaviour.cs index f1638fb95d..9c4247afe8 100644 --- a/com.unity.multiplayer.mlapi/Runtime/Core/NetworkBehaviour.cs +++ b/com.unity.multiplayer.mlapi/Runtime/Core/NetworkBehaviour.cs @@ -341,9 +341,6 @@ protected NetworkBehaviour GetNetworkBehaviour(ushort behaviourId) /// public ulong OwnerClientId => NetworkObject.OwnerClientId; - internal bool NetworkStartInvoked = false; - internal bool InternalNetworkStartInvoked = false; - /// /// Stores the network tick at the NetworkBehaviourUpdate time /// This allows sending NetworkVariables not more often than once per network tick, regardless of the update rate @@ -353,22 +350,32 @@ protected NetworkBehaviour GetNetworkBehaviour(ushort behaviourId) /// /// Gets called when message handlers are ready to be registered and the network is setup /// - public virtual void NetworkStart() { } + public virtual void OnNetworkSpawn() { } /// - /// Gets called when message handlers are ready to be registered and the network is setup. Provides a Payload if it was provided + /// Gets called when the gets spawned, message handlers are ready to be registered and the network is setup. Provides a Payload if it was provided /// /// The stream containing the spawn payload - public virtual void NetworkStart(Stream stream) + public virtual void OnNetworkSpawn(Stream stream) { - NetworkStart(); + OnNetworkSpawn(); } - internal void InternalNetworkStart() + /// + /// Gets called when the gets de-spawned. Is called both on the server and clients. + /// + public virtual void OnNetworkDespawn() {} + + internal void InternalOnNetworkSpawn() { InitializeVariables(); } + internal void InternalOnNetworkDespawn() + { + + } + /// /// Gets called when the local client gains ownership of this object /// diff --git a/com.unity.multiplayer.mlapi/Runtime/Core/NetworkManager.cs b/com.unity.multiplayer.mlapi/Runtime/Core/NetworkManager.cs index 0eac75309d..2b22c010cb 100644 --- a/com.unity.multiplayer.mlapi/Runtime/Core/NetworkManager.cs +++ b/com.unity.multiplayer.mlapi/Runtime/Core/NetworkManager.cs @@ -1496,7 +1496,7 @@ internal void OnClientDisconnectFromServer(ulong clientId) if (PrefabHandler.ContainsHandler(ConnectedClients[clientId].PlayerObject.GlobalObjectIdHash)) { PrefabHandler.HandleNetworkPrefabDestroy(ConnectedClients[clientId].PlayerObject); - SpawnManager.OnDestroyObject(ConnectedClients[clientId].PlayerObject.NetworkObjectId, false); + SpawnManager.OnDespawnObject(ConnectedClients[clientId].PlayerObject.NetworkObjectId, false); } else { @@ -1514,7 +1514,7 @@ internal void OnClientDisconnectFromServer(ulong clientId) if (PrefabHandler.ContainsHandler(ConnectedClients[clientId].OwnedObjects[i].GlobalObjectIdHash)) { PrefabHandler.HandleNetworkPrefabDestroy(ConnectedClients[clientId].OwnedObjects[i]); - SpawnManager.OnDestroyObject(ConnectedClients[clientId].OwnedObjects[i].NetworkObjectId, false); + SpawnManager.OnDespawnObject(ConnectedClients[clientId].OwnedObjects[i].NetworkObjectId, false); } else { diff --git a/com.unity.multiplayer.mlapi/Runtime/Core/NetworkObject.cs b/com.unity.multiplayer.mlapi/Runtime/Core/NetworkObject.cs index d1e62c92f0..4db53ee1ca 100644 --- a/com.unity.multiplayer.mlapi/Runtime/Core/NetworkObject.cs +++ b/com.unity.multiplayer.mlapi/Runtime/Core/NetworkObject.cs @@ -397,7 +397,7 @@ private void OnDestroy() { if (NetworkManager != null && NetworkManager.SpawnManager != null && NetworkManager.SpawnManager.SpawnedObjects.ContainsKey(NetworkObjectId)) { - NetworkManager.SpawnManager.OnDestroyObject(NetworkObjectId, false); + NetworkManager.SpawnManager.OnDespawnObject(NetworkObjectId, false); } } @@ -504,33 +504,21 @@ internal void InvokeBehaviourOnGainedOwnership() } } - internal void ResetNetworkStartInvoked() + internal void InvokeBehaviourNetworkSpawn(Stream stream) { - if (ChildNetworkBehaviours != null) + for (int i = 0; i < ChildNetworkBehaviours.Count; i++) { - for (int i = 0; i < ChildNetworkBehaviours.Count; i++) - { - ChildNetworkBehaviours[i].NetworkStartInvoked = false; - } + ChildNetworkBehaviours[i].InternalOnNetworkSpawn(); + ChildNetworkBehaviours[i].OnNetworkSpawn(stream); } } - internal void InvokeBehaviourNetworkSpawn(Stream stream) + internal void InvokeBehaviourNetworkDespawn() { for (int i = 0; i < ChildNetworkBehaviours.Count; i++) { - //We check if we are it's NetworkObject owner incase a NetworkObject exists as a child of our NetworkObject - if (!ChildNetworkBehaviours[i].NetworkStartInvoked) - { - if (!ChildNetworkBehaviours[i].InternalNetworkStartInvoked) - { - ChildNetworkBehaviours[i].InternalNetworkStart(); - ChildNetworkBehaviours[i].InternalNetworkStartInvoked = true; - } - - ChildNetworkBehaviours[i].NetworkStart(stream); - ChildNetworkBehaviours[i].NetworkStartInvoked = true; - } + ChildNetworkBehaviours[i].InternalOnNetworkDespawn(); + ChildNetworkBehaviours[i].OnNetworkDespawn(); } } @@ -698,7 +686,7 @@ internal void SerializeSceneObject(NetworkWriter writer, ulong targetClientId) // If our current buffer position is greater than our positionBeforeNetworkVariableData then we wrote NetworkVariable data // Part 1: This will include the total NetworkVariable data size, if there was NetworkVariable data written, to the stream // in order to be able to skip past this entry on the deserialization side in the event this NetworkObject fails to be - // constructed (See Part 2 below in the DeserializeSceneObject method) + // constructed (See Part 2 below in the DeserializeSceneObject method) if (buffer.Position > positionBeforeNetworkVariableData) { // Store our current stream buffer position diff --git a/com.unity.multiplayer.mlapi/Runtime/Messaging/InternalMessageHandler.cs b/com.unity.multiplayer.mlapi/Runtime/Messaging/InternalMessageHandler.cs index 8dccf5bc95..e8e4a54c0a 100644 --- a/com.unity.multiplayer.mlapi/Runtime/Messaging/InternalMessageHandler.cs +++ b/com.unity.multiplayer.mlapi/Runtime/Messaging/InternalMessageHandler.cs @@ -231,7 +231,7 @@ public void HandleDestroyObject(ulong clientId, Stream stream) using (var reader = PooledNetworkReader.Get(stream)) { ulong networkId = reader.ReadUInt64Packed(); - NetworkManager.SpawnManager.OnDestroyObject(networkId, true); + NetworkManager.SpawnManager.OnDespawnObject(networkId, true); } #if DEVELOPMENT_BUILD || UNITY_EDITOR s_HandleDestroyObject.End(); diff --git a/com.unity.multiplayer.mlapi/Runtime/Spawning/NetworkSpawnManager.cs b/com.unity.multiplayer.mlapi/Runtime/Spawning/NetworkSpawnManager.cs index 8a19ba9e1f..466c2bc739 100644 --- a/com.unity.multiplayer.mlapi/Runtime/Spawning/NetworkSpawnManager.cs +++ b/com.unity.multiplayer.mlapi/Runtime/Spawning/NetworkSpawnManager.cs @@ -332,8 +332,6 @@ internal void SpawnNetworkObjectLocally(NetworkObject networkObject, ulong netwo } } - networkObject.ResetNetworkStartInvoked(); - if (readPayload) { using (var payloadBuffer = PooledNetworkBuffer.Get()) @@ -452,7 +450,7 @@ internal void DespawnObject(NetworkObject networkObject, bool destroyObject = fa throw new NotServerException("Only server can despawn objects"); } - OnDestroyObject(networkObject.NetworkObjectId, destroyObject); + OnDespawnObject(networkObject.NetworkObjectId, destroyObject); } // Makes scene objects ready to be reused @@ -490,7 +488,7 @@ internal void ServerDestroySpawnedSceneObjects() if (NetworkManager.PrefabHandler != null && NetworkManager.PrefabHandler.ContainsHandler(sobj)) { NetworkManager.PrefabHandler.HandleNetworkPrefabDestroy(sobj); - OnDestroyObject(sobj.NetworkObjectId, false); + OnDespawnObject(sobj.NetworkObjectId, false); } else { @@ -514,7 +512,7 @@ internal void DestroyNonSceneObjects() { NetworkManager.PrefabHandler.HandleNetworkPrefabDestroy(networkObjects[i]); - OnDestroyObject(networkObjects[i].NetworkObjectId, false); + OnDespawnObject(networkObjects[i].NetworkObjectId, false); } else { @@ -538,7 +536,7 @@ internal void DestroySceneObjects() if (NetworkManager.PrefabHandler.ContainsHandler(networkObjects[i])) { NetworkManager.PrefabHandler.HandleNetworkPrefabDestroy(networkObjects[i]); - OnDestroyObject(networkObjects[i].NetworkObjectId, false); + OnDespawnObject(networkObjects[i].NetworkObjectId, false); } else { @@ -599,7 +597,7 @@ internal void ClientCollectSoftSyncSceneObjectSweep(NetworkObject[] networkObjec } } - internal void OnDestroyObject(ulong networkId, bool destroyGameObject) + internal void OnDespawnObject(ulong networkId, bool destroyGameObject) { if (NetworkManager == null) { @@ -607,13 +605,13 @@ internal void OnDestroyObject(ulong networkId, bool destroyGameObject) } //Removal of spawned object - if (!SpawnedObjects.TryGetValue(networkId, out NetworkObject sobj)) + if (!SpawnedObjects.TryGetValue(networkId, out NetworkObject networkObject)) { Debug.LogWarning($"Trying to destroy object {networkId} but it doesn't seem to exist anymore!"); return; } - if (!sobj.IsOwnedByServer && !sobj.IsPlayerObject && NetworkManager.Singleton.ConnectedClients.TryGetValue(sobj.OwnerClientId, out NetworkClient networkClient)) + if (!networkObject.IsOwnedByServer && !networkObject.IsPlayerObject && NetworkManager.Singleton.ConnectedClients.TryGetValue(networkObject.OwnerClientId, out NetworkClient networkClient)) { //Someone owns it. for (int i = networkClient.OwnedObjects.Count - 1; i > -1; i--) @@ -625,7 +623,8 @@ internal void OnDestroyObject(ulong networkId, bool destroyGameObject) } } - sobj.IsSpawned = false; + networkObject.IsSpawned = false; + networkObject.InvokeBehaviourNetworkDespawn(); if (NetworkManager != null && NetworkManager.IsServer) { @@ -641,7 +640,7 @@ internal void OnDestroyObject(ulong networkId, bool destroyGameObject) var rpcQueueContainer = NetworkManager.RpcQueueContainer; if (rpcQueueContainer != null) { - if (sobj != null) + if (networkObject != null) { // As long as we have any remaining clients, then notify of the object being destroy. if (NetworkManager.ConnectedClientsList.Count > 0) @@ -668,14 +667,14 @@ internal void OnDestroyObject(ulong networkId, bool destroyGameObject) } } - var gobj = sobj.gameObject; + var gobj = networkObject.gameObject; if (destroyGameObject && gobj != null) { - if (NetworkManager.PrefabHandler.ContainsHandler(sobj)) + if (NetworkManager.PrefabHandler.ContainsHandler(networkObject)) { - NetworkManager.PrefabHandler.HandleNetworkPrefabDestroy(sobj); - OnDestroyObject(networkId, false); + NetworkManager.PrefabHandler.HandleNetworkPrefabDestroy(networkObject); + OnDespawnObject(networkId, false); } else { @@ -688,7 +687,7 @@ internal void OnDestroyObject(ulong networkId, bool destroyGameObject) // of the function if (SpawnedObjects.Remove(networkId)) { - SpawnedObjectsList.Remove(sobj); + SpawnedObjectsList.Remove(networkObject); } } } diff --git a/testproject/Assets/Samples/EnableDisableNetworkObject/EnableDisableSceneNetworkObjectComponent.cs b/testproject/Assets/Samples/EnableDisableNetworkObject/EnableDisableSceneNetworkObjectComponent.cs index fe25a6632b..25671c3ef0 100644 --- a/testproject/Assets/Samples/EnableDisableNetworkObject/EnableDisableSceneNetworkObjectComponent.cs +++ b/testproject/Assets/Samples/EnableDisableNetworkObject/EnableDisableSceneNetworkObjectComponent.cs @@ -27,7 +27,7 @@ private void Start() } } - public override void NetworkStart() + public override void OnNetworkSpawn() { //For this example, the server controls whether the mesh is visible and can collide or not if(IsServer && IsHost) @@ -37,7 +37,7 @@ public override void NetworkStart() m_ActivateObjectButton.gameObject.SetActive(true); } } - base.NetworkStart(); + base.OnNetworkSpawn(); } public void ButtonActivateToggle() diff --git a/testproject/Assets/Scripts/PlayerMovement.cs b/testproject/Assets/Scripts/PlayerMovement.cs index 0e48e6537a..898733d2ab 100644 --- a/testproject/Assets/Scripts/PlayerMovement.cs +++ b/testproject/Assets/Scripts/PlayerMovement.cs @@ -32,9 +32,9 @@ private void Start() } } - public override void NetworkStart() + public override void OnNetworkSpawn() { - base.NetworkStart(); + base.OnNetworkSpawn(); Players[OwnerClientId] = this; // todo should really have a NetworkStop for unregistering this... } diff --git a/testproject/Assets/Tests/Manual/HybridScripts/RpcQueueManualTests.cs b/testproject/Assets/Tests/Manual/HybridScripts/RpcQueueManualTests.cs index 1f20e88408..a18bb25a5a 100644 --- a/testproject/Assets/Tests/Manual/HybridScripts/RpcQueueManualTests.cs +++ b/testproject/Assets/Tests/Manual/HybridScripts/RpcQueueManualTests.cs @@ -319,7 +319,7 @@ private void InitializeNetworkManager() /// /// Invoked upon the attached NetworkObject component being initialized by MLAPI /// - public override void NetworkStart() + public override void OnNetworkSpawn() { if (IsServer) { diff --git a/testproject/Assets/Tests/Manual/Scripts/ConnectionApprovalComponent.cs b/testproject/Assets/Tests/Manual/Scripts/ConnectionApprovalComponent.cs index 7257a3c5ed..4e0d21e8ae 100644 --- a/testproject/Assets/Tests/Manual/Scripts/ConnectionApprovalComponent.cs +++ b/testproject/Assets/Tests/Manual/Scripts/ConnectionApprovalComponent.cs @@ -60,7 +60,7 @@ private void Start() } } - public override void NetworkStart() + public override void OnNetworkSpawn() { if (m_SimulateFailure) { diff --git a/testproject/Assets/Tests/Manual/Scripts/NetworkPrefabPool.cs b/testproject/Assets/Tests/Manual/Scripts/NetworkPrefabPool.cs index ea0bcb1317..522b8c7d53 100644 --- a/testproject/Assets/Tests/Manual/Scripts/NetworkPrefabPool.cs +++ b/testproject/Assets/Tests/Manual/Scripts/NetworkPrefabPool.cs @@ -158,7 +158,7 @@ private void OnSceneSwitchBegin() /// /// Override NetworkBehaviour.NetworkStart /// - public override void NetworkStart() + public override void OnNetworkSpawn() { InitializeObjectPool(); if (IsServer) diff --git a/testproject/Assets/Tests/Manual/Scripts/NetworkSceneManagerCallbackTests.cs b/testproject/Assets/Tests/Manual/Scripts/NetworkSceneManagerCallbackTests.cs index 75911c1d4e..d3b9f05cef 100644 --- a/testproject/Assets/Tests/Manual/Scripts/NetworkSceneManagerCallbackTests.cs +++ b/testproject/Assets/Tests/Manual/Scripts/NetworkSceneManagerCallbackTests.cs @@ -11,7 +11,7 @@ public void StartHost() NetworkManager.StartHost(); } - public override void NetworkStart() + public override void OnNetworkSpawn() { if (IsServer) { diff --git a/testproject/Assets/Tests/Manual/Scripts/StatsDisplay.cs b/testproject/Assets/Tests/Manual/Scripts/StatsDisplay.cs index bda4d770a9..296ccb766b 100644 --- a/testproject/Assets/Tests/Manual/Scripts/StatsDisplay.cs +++ b/testproject/Assets/Tests/Manual/Scripts/StatsDisplay.cs @@ -41,7 +41,7 @@ private void Start() } } - public override void NetworkStart() + public override void OnNetworkSpawn() { if (NetworkManager.IsServer) { diff --git a/testproject/Assets/Tests/Manual/Scripts/SwitchSceneHandler.cs b/testproject/Assets/Tests/Manual/Scripts/SwitchSceneHandler.cs index 8e0c609ba9..a51418472f 100644 --- a/testproject/Assets/Tests/Manual/Scripts/SwitchSceneHandler.cs +++ b/testproject/Assets/Tests/Manual/Scripts/SwitchSceneHandler.cs @@ -55,7 +55,7 @@ private IEnumerator CheckForVisibility() yield return null; } - public override void NetworkStart() + public override void OnNetworkSpawn() { if (NetworkManager.Singleton && NetworkManager.Singleton.IsListening && NetworkManager.Singleton.IsServer) { @@ -68,7 +68,7 @@ public override void NetworkStart() { m_SwitchSceneButtonObject.SetActive(false); } - base.NetworkStart(); + base.OnNetworkSpawn(); } private SceneSwitchProgress m_CurrentSceneSwitchProgress; From a50902b0efe5a7f867f4a4e2552434924a09198a Mon Sep 17 00:00:00 2001 From: Luke Stampfli Date: Fri, 28 May 2021 15:45:43 +0200 Subject: [PATCH 02/10] test: spawn/despawn function tests --- .../Tests/Runtime/NetworkObject.meta | 8 + .../NetworkObjectOnSpawnTests.cs | 214 ++++++++++++++++++ .../NetworkObjectOnSpawnTests.cs.meta | 11 + .../NetworkObjectSceneSerializationTests.cs | 0 ...tworkObjectSceneSerializationTests.cs.meta | 0 5 files changed, 233 insertions(+) create mode 100644 com.unity.multiplayer.mlapi/Tests/Runtime/NetworkObject.meta create mode 100644 com.unity.multiplayer.mlapi/Tests/Runtime/NetworkObject/NetworkObjectOnSpawnTests.cs create mode 100644 com.unity.multiplayer.mlapi/Tests/Runtime/NetworkObject/NetworkObjectOnSpawnTests.cs.meta rename com.unity.multiplayer.mlapi/Tests/Runtime/{ => NetworkObject}/NetworkObjectSceneSerializationTests.cs (100%) rename com.unity.multiplayer.mlapi/Tests/Runtime/{ => NetworkObject}/NetworkObjectSceneSerializationTests.cs.meta (100%) diff --git a/com.unity.multiplayer.mlapi/Tests/Runtime/NetworkObject.meta b/com.unity.multiplayer.mlapi/Tests/Runtime/NetworkObject.meta new file mode 100644 index 0000000000..3b96bc20f2 --- /dev/null +++ b/com.unity.multiplayer.mlapi/Tests/Runtime/NetworkObject.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 3a1f0974a98eaa1498ac39be872eeed4 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/com.unity.multiplayer.mlapi/Tests/Runtime/NetworkObject/NetworkObjectOnSpawnTests.cs b/com.unity.multiplayer.mlapi/Tests/Runtime/NetworkObject/NetworkObjectOnSpawnTests.cs new file mode 100644 index 0000000000..8d19afd725 --- /dev/null +++ b/com.unity.multiplayer.mlapi/Tests/Runtime/NetworkObject/NetworkObjectOnSpawnTests.cs @@ -0,0 +1,214 @@ +using System.Collections; +using System.Collections.Generic; +using MLAPI; +using MLAPI.RuntimeTests; +using NUnit.Framework; +using UnityEngine; +using UnityEngine.TestTools; + +namespace MLAPI.RuntimeTests +{ + public class NetworkObjectOnSpawnTests + { + private GameObject m_Prefab; + + /// + /// Tests that instantiating a and destroying without spawning it + /// does not run or . + /// + /// + [UnityTest] + public IEnumerator InstantiateDestroySpawnNotCalled() + { + var gameObject = new GameObject("InstantiateDestroySpawnNotCalled_Object"); + var networkObject = gameObject.AddComponent(); + var fail = gameObject.AddComponent(); + + yield return null; + + // instantiate + var instance = GameObject.Instantiate(gameObject); + yield return null; + + // destroy + GameObject.Destroy(instance); + yield return null; + } + + private class FailWhenSpawned : NetworkBehaviour + { + public override void OnNetworkSpawn() + { + Assert.Fail("Spawn should not be called on not spawned object"); + } + + public override void OnNetworkDespawn() + { + Assert.Fail("Depawn should not be called on not spawned object"); + } + } + + /// + /// Test that callbacks are run for playerobject spawn, despawn, regular spawn, destroy on server. + /// + /// + [UnityTest] + public IEnumerator TestOnNetworkSpawnCallbacks() + { + // Create Host and (numClients) clients + Assert.True(MultiInstanceHelpers.Create(2, out NetworkManager server, out NetworkManager[] clients)); + + // Create a default player GameObject to use + m_Prefab = new GameObject("TestObject"); + var networkObject = m_Prefab.AddComponent(); + + // add test component + m_Prefab.AddComponent(); + + // Make it a prefab + MultiInstanceHelpers.MakeNetworkedObjectTestPrefab(networkObject); + + // Set the player prefab + server.NetworkConfig.PlayerPrefab = m_Prefab; + + // Set all of the client's player prefab + for (int i = 0; i < clients.Length; i++) + { + clients[i].NetworkConfig.PlayerPrefab = m_Prefab; + } + + // Start the instances + if (!MultiInstanceHelpers.Start(true, server, clients)) + { + Debug.LogError("Failed to start instances"); + Assert.Fail("Failed to start instances"); + } + + // [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 + yield return MultiInstanceHelpers.Run(MultiInstanceHelpers.WaitForClientsConnectedToServer(server, clients.Length + 1, null, 512)); + + // [Host-Side] Get the Host owned instance + var serverClientPlayerResult = new MultiInstanceHelpers.CoroutineResultWrapper(); + yield return MultiInstanceHelpers.Run(MultiInstanceHelpers.GetNetworkObjectByRepresentation((x => x.IsPlayerObject && x.OwnerClientId == clients[0].LocalClientId), server, serverClientPlayerResult)); + + var serverInstance = serverClientPlayerResult.Result.GetComponent(); + + var clientInstances = new List(); + foreach (var client in clients) + { + var clientClientPlayerResult = new MultiInstanceHelpers.CoroutineResultWrapper(); + yield return MultiInstanceHelpers.Run(MultiInstanceHelpers.GetNetworkObjectByRepresentation((x => x.IsPlayerObject && x.OwnerClientId == clients[0].LocalClientId), client, clientClientPlayerResult)); + var clientRpcTests = clientClientPlayerResult.Result.GetComponent(); + Assert.IsNotNull(clientRpcTests); + clientInstances.Add(clientRpcTests); + } + + // -------------- step 1 check player spawn despawn + + // check spawned on server + Assert.AreEqual(1, serverInstance.OnNetworkSpawnCalledCount); + + // safety check despawned + Assert.AreEqual(0, serverInstance.OnNetworkDespawnCalledCount); + + // check spawned on client + foreach (var clientInstance in clientInstances) + { + Assert.AreEqual(1, clientInstance.OnNetworkSpawnCalledCount); + + // safety check despawned + Assert.AreEqual(0, clientInstance.OnNetworkDespawnCalledCount); + } + + // despawn on server + serverInstance.GetComponent().Despawn(); + + // check despawned on server + Assert.AreEqual(1, serverInstance.OnNetworkDespawnCalledCount); + + // wait long enough for player object to be despawned + for (int i = 0; i < 5; i++) + { + yield return null; + } + + // check despawned on clients + foreach (var clientInstance in clientInstances) + { + Assert.AreEqual(1, clientInstance.OnNetworkDespawnCalledCount); + } + + //----------- step 2 check spawn again and destroy + + serverInstance.GetComponent().Spawn(); + + // wait long enough for player object to be spawned + for (int i = 0; i < 5; i++) + { + yield return null; + } + + // check spawned again on server this is 2 becaue we are reusing the object which was already spawned once. + Assert.AreEqual(2, serverInstance.OnNetworkSpawnCalledCount); + + // check spawned on client + foreach (var clientInstance in clientInstances) + { + Assert.AreEqual(1, clientInstance.OnNetworkSpawnCalledCount); + } + + // destroy the server object + GameObject.Destroy(serverInstance.gameObject); + + // wait one frame for destroy to kick in + yield return null; + + // check whether despawned was called again on server instance + Assert.AreEqual(2, serverInstance.OnNetworkDespawnCalledCount); + + // wait long enough for player object to be despawned on client + for (int i = 0; i < 5; i++) + { + yield return null; + } + + // check despawned on clients + foreach (var clientInstance in clientInstances) + { + Assert.AreEqual(1, clientInstance.OnNetworkDespawnCalledCount); + } + } + + private class TrackOnSpawnFunctions : NetworkBehaviour + { + public int OnNetworkSpawnCalledCount { get; private set; } + public int OnNetworkDespawnCalledCount { get; private set; } + + public override void OnNetworkSpawn() + { + OnNetworkSpawnCalledCount++; + } + + public override void OnNetworkDespawn() + { + OnNetworkDespawnCalledCount++; + } + } + + [TearDown] + public void TearDown() + { + if (m_Prefab != null) + { + Object.Destroy(m_Prefab); + m_Prefab = null; + } + + // Shutdown and clean up both of our NetworkManager instances + MultiInstanceHelpers.Destroy(); + } + } +} diff --git a/com.unity.multiplayer.mlapi/Tests/Runtime/NetworkObject/NetworkObjectOnSpawnTests.cs.meta b/com.unity.multiplayer.mlapi/Tests/Runtime/NetworkObject/NetworkObjectOnSpawnTests.cs.meta new file mode 100644 index 0000000000..407ad63b8f --- /dev/null +++ b/com.unity.multiplayer.mlapi/Tests/Runtime/NetworkObject/NetworkObjectOnSpawnTests.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: dbb32425abaa21a44aeacf00f37ca4e2 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/com.unity.multiplayer.mlapi/Tests/Runtime/NetworkObjectSceneSerializationTests.cs b/com.unity.multiplayer.mlapi/Tests/Runtime/NetworkObject/NetworkObjectSceneSerializationTests.cs similarity index 100% rename from com.unity.multiplayer.mlapi/Tests/Runtime/NetworkObjectSceneSerializationTests.cs rename to com.unity.multiplayer.mlapi/Tests/Runtime/NetworkObject/NetworkObjectSceneSerializationTests.cs diff --git a/com.unity.multiplayer.mlapi/Tests/Runtime/NetworkObjectSceneSerializationTests.cs.meta b/com.unity.multiplayer.mlapi/Tests/Runtime/NetworkObject/NetworkObjectSceneSerializationTests.cs.meta similarity index 100% rename from com.unity.multiplayer.mlapi/Tests/Runtime/NetworkObjectSceneSerializationTests.cs.meta rename to com.unity.multiplayer.mlapi/Tests/Runtime/NetworkObject/NetworkObjectSceneSerializationTests.cs.meta From 0eb957d98abbb3b406a421495fec22a4d20a124a Mon Sep 17 00:00:00 2001 From: Luke Stampfli Date: Fri, 28 May 2021 17:38:27 +0200 Subject: [PATCH 03/10] fix: set target framerate for test --- .../NetworkObjectOnSpawnTests.cs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/com.unity.multiplayer.mlapi/Tests/Runtime/NetworkObject/NetworkObjectOnSpawnTests.cs b/com.unity.multiplayer.mlapi/Tests/Runtime/NetworkObject/NetworkObjectOnSpawnTests.cs index 8d19afd725..73752cb65f 100644 --- a/com.unity.multiplayer.mlapi/Tests/Runtime/NetworkObject/NetworkObjectOnSpawnTests.cs +++ b/com.unity.multiplayer.mlapi/Tests/Runtime/NetworkObject/NetworkObjectOnSpawnTests.cs @@ -12,6 +12,8 @@ public class NetworkObjectOnSpawnTests { private GameObject m_Prefab; + private int m_OriginalTargetFrameRate; + /// /// Tests that instantiating a and destroying without spawning it /// does not run or . @@ -48,6 +50,20 @@ public override void OnNetworkDespawn() } } + [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; + } + } + /// /// Test that callbacks are run for playerobject spawn, despawn, regular spawn, destroy on server. /// @@ -209,6 +225,9 @@ 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; } } } From 332d6cd09564d20f0aff17a8c97f6eabca41b38d Mon Sep 17 00:00:00 2001 From: Luke Stampfli Date: Fri, 28 May 2021 17:45:37 +0200 Subject: [PATCH 04/10] refactor: format --- com.unity.multiplayer.mlapi/Runtime/Core/NetworkBehaviour.cs | 2 +- .../Runtime/NetworkVariable/NetworkVariable.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/com.unity.multiplayer.mlapi/Runtime/Core/NetworkBehaviour.cs b/com.unity.multiplayer.mlapi/Runtime/Core/NetworkBehaviour.cs index 9c4247afe8..dd196d1911 100644 --- a/com.unity.multiplayer.mlapi/Runtime/Core/NetworkBehaviour.cs +++ b/com.unity.multiplayer.mlapi/Runtime/Core/NetworkBehaviour.cs @@ -364,7 +364,7 @@ public virtual void OnNetworkSpawn(Stream stream) /// /// Gets called when the gets de-spawned. Is called both on the server and clients. /// - public virtual void OnNetworkDespawn() {} + public virtual void OnNetworkDespawn() { } internal void InternalOnNetworkSpawn() { diff --git a/com.unity.multiplayer.mlapi/Runtime/NetworkVariable/NetworkVariable.cs b/com.unity.multiplayer.mlapi/Runtime/NetworkVariable/NetworkVariable.cs index 42016b0e77..d8fcd4b3fc 100644 --- a/com.unity.multiplayer.mlapi/Runtime/NetworkVariable/NetworkVariable.cs +++ b/com.unity.multiplayer.mlapi/Runtime/NetworkVariable/NetworkVariable.cs @@ -204,7 +204,7 @@ public void ReadField(Stream stream) /// public void WriteField(Stream stream) { - using (var writer = PooledNetworkWriter.Get(stream)) + using (var writer = PooledNetworkWriter.Get(stream)) { writer.WriteObjectPacked(m_InternalValue); //BOX } From af5460e897134b622de90555a78a7dc0db502bda Mon Sep 17 00:00:00 2001 From: Luke Stampfli Date: Tue, 8 Jun 2021 12:06:17 +0200 Subject: [PATCH 05/10] refactor: Use object instead of gameobject qualifier --- .../Runtime/NetworkObject/NetworkObjectOnSpawnTests.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/com.unity.multiplayer.mlapi/Tests/Runtime/NetworkObject/NetworkObjectOnSpawnTests.cs b/com.unity.multiplayer.mlapi/Tests/Runtime/NetworkObject/NetworkObjectOnSpawnTests.cs index 73752cb65f..4c92f26f4f 100644 --- a/com.unity.multiplayer.mlapi/Tests/Runtime/NetworkObject/NetworkObjectOnSpawnTests.cs +++ b/com.unity.multiplayer.mlapi/Tests/Runtime/NetworkObject/NetworkObjectOnSpawnTests.cs @@ -29,11 +29,11 @@ public IEnumerator InstantiateDestroySpawnNotCalled() yield return null; // instantiate - var instance = GameObject.Instantiate(gameObject); + var instance = Object.Instantiate(gameObject); yield return null; // destroy - GameObject.Destroy(instance); + Object.Destroy(instance); yield return null; } @@ -177,7 +177,7 @@ public IEnumerator TestOnNetworkSpawnCallbacks() } // destroy the server object - GameObject.Destroy(serverInstance.gameObject); + Object.Destroy(serverInstance.gameObject); // wait one frame for destroy to kick in yield return null; From beaa11f9e75dc41b8bbf7c57eb6c7ade8487e0d1 Mon Sep 17 00:00:00 2001 From: Luke Stampfli Date: Wed, 9 Jun 2021 14:00:05 +0200 Subject: [PATCH 06/10] refactor: use waituntil instead of for loop in enumerator --- .../NetworkObjectOnSpawnTests.cs | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/com.unity.multiplayer.mlapi/Tests/Runtime/NetworkObject/NetworkObjectOnSpawnTests.cs b/com.unity.multiplayer.mlapi/Tests/Runtime/NetworkObject/NetworkObjectOnSpawnTests.cs index 4c92f26f4f..e24efd6b7a 100644 --- a/com.unity.multiplayer.mlapi/Tests/Runtime/NetworkObject/NetworkObjectOnSpawnTests.cs +++ b/com.unity.multiplayer.mlapi/Tests/Runtime/NetworkObject/NetworkObjectOnSpawnTests.cs @@ -146,10 +146,8 @@ public IEnumerator TestOnNetworkSpawnCallbacks() Assert.AreEqual(1, serverInstance.OnNetworkDespawnCalledCount); // wait long enough for player object to be despawned - for (int i = 0; i < 5; i++) - { - yield return null; - } + int nextFrameNumber = Time.frameCount + 2; + yield return new WaitUntil(() => Time.frameCount >= nextFrameNumber); // check despawned on clients foreach (var clientInstance in clientInstances) @@ -162,10 +160,9 @@ public IEnumerator TestOnNetworkSpawnCallbacks() serverInstance.GetComponent().Spawn(); // wait long enough for player object to be spawned - for (int i = 0; i < 5; i++) - { - yield return null; - } + nextFrameNumber = Time.frameCount + 2; + yield return new WaitUntil(() => Time.frameCount >= nextFrameNumber); + // check spawned again on server this is 2 becaue we are reusing the object which was already spawned once. Assert.AreEqual(2, serverInstance.OnNetworkSpawnCalledCount); @@ -186,10 +183,8 @@ public IEnumerator TestOnNetworkSpawnCallbacks() Assert.AreEqual(2, serverInstance.OnNetworkDespawnCalledCount); // wait long enough for player object to be despawned on client - for (int i = 0; i < 5; i++) - { - yield return null; - } + nextFrameNumber = Time.frameCount + 2; + yield return new WaitUntil(() => Time.frameCount >= nextFrameNumber); // check despawned on clients foreach (var clientInstance in clientInstances) From 9e4035b171f9d8d311b4adfe39a06d6851cfd197 Mon Sep 17 00:00:00 2001 From: Luke Stampfli Date: Wed, 9 Jun 2021 14:03:38 +0200 Subject: [PATCH 07/10] refactor: remove unecessary error logs in multiinstance tests. --- .../Tests/Runtime/MultiInstance/RPCTests.cs | 1 - .../NetworkObject/NetworkObjectOnSpawnTests.cs | 1 - .../Tests/Runtime/NetworkSpawnManagerTests.cs | 1 - .../Tests/Runtime/MultiClientConnectionApproval.cs | 11 +++++------ .../Tests/Runtime/RpcINetworkSerializable.cs | 14 ++++++-------- .../Assets/Tests/Runtime/RpcTestsAutomated.cs | 7 +++---- 6 files changed, 14 insertions(+), 21 deletions(-) diff --git a/com.unity.multiplayer.mlapi/Tests/Runtime/MultiInstance/RPCTests.cs b/com.unity.multiplayer.mlapi/Tests/Runtime/MultiInstance/RPCTests.cs index ddf24ce674..9f74d6b741 100644 --- a/com.unity.multiplayer.mlapi/Tests/Runtime/MultiInstance/RPCTests.cs +++ b/com.unity.multiplayer.mlapi/Tests/Runtime/MultiInstance/RPCTests.cs @@ -72,7 +72,6 @@ public IEnumerator TestRPCs() // Start the instances if (!MultiInstanceHelpers.Start(true, server, clients)) { - Debug.LogError("Failed to start instances"); Assert.Fail("Failed to start instances"); } diff --git a/com.unity.multiplayer.mlapi/Tests/Runtime/NetworkObject/NetworkObjectOnSpawnTests.cs b/com.unity.multiplayer.mlapi/Tests/Runtime/NetworkObject/NetworkObjectOnSpawnTests.cs index e24efd6b7a..2564908a89 100644 --- a/com.unity.multiplayer.mlapi/Tests/Runtime/NetworkObject/NetworkObjectOnSpawnTests.cs +++ b/com.unity.multiplayer.mlapi/Tests/Runtime/NetworkObject/NetworkObjectOnSpawnTests.cs @@ -96,7 +96,6 @@ public IEnumerator TestOnNetworkSpawnCallbacks() // Start the instances if (!MultiInstanceHelpers.Start(true, server, clients)) { - Debug.LogError("Failed to start instances"); Assert.Fail("Failed to start instances"); } diff --git a/com.unity.multiplayer.mlapi/Tests/Runtime/NetworkSpawnManagerTests.cs b/com.unity.multiplayer.mlapi/Tests/Runtime/NetworkSpawnManagerTests.cs index bc868e3429..5e70647fdc 100644 --- a/com.unity.multiplayer.mlapi/Tests/Runtime/NetworkSpawnManagerTests.cs +++ b/com.unity.multiplayer.mlapi/Tests/Runtime/NetworkSpawnManagerTests.cs @@ -60,7 +60,6 @@ public IEnumerator Setup() // Start the instances if (!MultiInstanceHelpers.Start(true, server, clients)) { - Debug.LogError("Failed to start instances"); Assert.Fail("Failed to start instances"); } diff --git a/testproject/Assets/Tests/Runtime/MultiClientConnectionApproval.cs b/testproject/Assets/Tests/Runtime/MultiClientConnectionApproval.cs index d8866eb658..63c0d762d9 100644 --- a/testproject/Assets/Tests/Runtime/MultiClientConnectionApproval.cs +++ b/testproject/Assets/Tests/Runtime/MultiClientConnectionApproval.cs @@ -60,7 +60,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 @@ -80,8 +80,8 @@ private IEnumerator ConnectionApprovalHandler(int numClients, int failureTestCou 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,17 +131,16 @@ private IEnumerator ConnectionApprovalHandler(int numClients, int failureTestCou client.NetworkConfig.ConnectionData = Encoding.ASCII.GetBytes(m_ConnectionToken); clientsAdjustedList.Add(client); } - + } // Start the instances if (!MultiInstanceHelpers.Start(true, server, clients)) { - Debug.LogError("Failed to start instances"); 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 diff --git a/testproject/Assets/Tests/Runtime/RpcINetworkSerializable.cs b/testproject/Assets/Tests/Runtime/RpcINetworkSerializable.cs index d860fb3207..1a181c3ebb 100644 --- a/testproject/Assets/Tests/Runtime/RpcINetworkSerializable.cs +++ b/testproject/Assets/Tests/Runtime/RpcINetworkSerializable.cs @@ -50,7 +50,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 @@ -72,11 +72,10 @@ public IEnumerator NetworkSerializableTest() // Start the instances if (!MultiInstanceHelpers.Start(true, server, clients)) { - Debug.LogError("Failed to start instances"); 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 @@ -197,7 +196,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 @@ -219,11 +218,10 @@ public IEnumerator NetworkSerializableArrayTestHandler(int arraySize, bool sendN // Start the instances if (!MultiInstanceHelpers.Start(true, server, clients)) { - Debug.LogError("Failed to start instances"); 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 +243,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(); @@ -458,7 +456,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..465a6ce8fc 100644 --- a/testproject/Assets/Tests/Runtime/RpcTestsAutomated.cs +++ b/testproject/Assets/Tests/Runtime/RpcTestsAutomated.cs @@ -57,7 +57,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 +72,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 @@ -98,7 +98,6 @@ private IEnumerator AutomatedRpcTestsHandler(int numClients, bool useBatching = // Start the instances if (!MultiInstanceHelpers.Start(true, server, clients)) { - Debug.LogError("Failed to start instances"); Assert.Fail("Failed to start instances"); } @@ -110,7 +109,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 From 8e8940f38345be6bb9dca082fede1818bb054d7a Mon Sep 17 00:00:00 2001 From: Luke Stampfli Date: Wed, 9 Jun 2021 14:22:37 +0200 Subject: [PATCH 08/10] fix: Fix random movement --- testproject/Assets/Tests/Manual/Scripts/RandomMovement.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/testproject/Assets/Tests/Manual/Scripts/RandomMovement.cs b/testproject/Assets/Tests/Manual/Scripts/RandomMovement.cs index e149c7dd84..166456d1ba 100644 --- a/testproject/Assets/Tests/Manual/Scripts/RandomMovement.cs +++ b/testproject/Assets/Tests/Manual/Scripts/RandomMovement.cs @@ -12,7 +12,7 @@ public class RandomMovement : NetworkBehaviour, IPlayerMovement private Rigidbody m_Rigidbody; - public override void NetworkStart() + public override void OnNetworkSpawn() { m_Rigidbody = GetComponent(); if (NetworkObject != null && m_Rigidbody != null) From 0c243f46e96ab7fee0d6c2ab12faac1089e8c831 Mon Sep 17 00:00:00 2001 From: Luke Stampfli Date: Mon, 14 Jun 2021 12:04:09 +0200 Subject: [PATCH 09/10] test: remove no longer necessary target framerate code from tests --- .../NetworkObjectOnSpawnTests.cs | 22 ------------------- 1 file changed, 22 deletions(-) diff --git a/com.unity.multiplayer.mlapi/Tests/Runtime/NetworkObject/NetworkObjectOnSpawnTests.cs b/com.unity.multiplayer.mlapi/Tests/Runtime/NetworkObject/NetworkObjectOnSpawnTests.cs index 2564908a89..3b1556b56e 100644 --- a/com.unity.multiplayer.mlapi/Tests/Runtime/NetworkObject/NetworkObjectOnSpawnTests.cs +++ b/com.unity.multiplayer.mlapi/Tests/Runtime/NetworkObject/NetworkObjectOnSpawnTests.cs @@ -12,8 +12,6 @@ public class NetworkObjectOnSpawnTests { private GameObject m_Prefab; - private int m_OriginalTargetFrameRate; - /// /// Tests that instantiating a and destroying without spawning it /// does not run or . @@ -50,20 +48,6 @@ public override void OnNetworkDespawn() } } - [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; - } - } - /// /// Test that callbacks are run for playerobject spawn, despawn, regular spawn, destroy on server. /// @@ -216,12 +200,6 @@ public void TearDown() Object.Destroy(m_Prefab); m_Prefab = 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; } } } From dce66c88dee4ec14500d34f021039c26f68c592c Mon Sep 17 00:00:00 2001 From: Luke Stampfli Date: Mon, 14 Jun 2021 12:29:08 +0200 Subject: [PATCH 10/10] fix: destroy network instance helpers in test --- .../Tests/Runtime/NetworkObject/NetworkObjectOnSpawnTests.cs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/com.unity.multiplayer.mlapi/Tests/Runtime/NetworkObject/NetworkObjectOnSpawnTests.cs b/com.unity.multiplayer.mlapi/Tests/Runtime/NetworkObject/NetworkObjectOnSpawnTests.cs index 3b1556b56e..0bf6720dad 100644 --- a/com.unity.multiplayer.mlapi/Tests/Runtime/NetworkObject/NetworkObjectOnSpawnTests.cs +++ b/com.unity.multiplayer.mlapi/Tests/Runtime/NetworkObject/NetworkObjectOnSpawnTests.cs @@ -174,6 +174,9 @@ public IEnumerator TestOnNetworkSpawnCallbacks() { Assert.AreEqual(1, clientInstance.OnNetworkDespawnCalledCount); } + + // Shutdown and clean up both of our NetworkManager instances + MultiInstanceHelpers.Destroy(); } private class TrackOnSpawnFunctions : NetworkBehaviour