|
| 1 | +using System.Collections; |
| 2 | +using NUnit.Framework; |
| 3 | +using TestProject.RuntimeTests.Support; |
| 4 | +using Unity.Netcode; |
| 5 | +using Unity.Netcode.RuntimeTests; |
| 6 | +using UnityEngine; |
| 7 | +using UnityEngine.TestTools; |
| 8 | + |
| 9 | +namespace TestProject.RuntimeTests |
| 10 | +{ |
| 11 | + public class NetworkVariableInitializationOnNetworkSpawnTest |
| 12 | + { |
| 13 | + private GameObject m_Prefab; |
| 14 | + |
| 15 | + [UnitySetUp] |
| 16 | + public IEnumerator SetUp() |
| 17 | + { |
| 18 | + // Make sure these static values are reset |
| 19 | + NetworkVariableInitOnNetworkSpawn.NetworkSpawnCalledOnClient = false; |
| 20 | + NetworkVariableInitOnNetworkSpawn.NetworkSpawnCalledOnServer = false; |
| 21 | + yield break; |
| 22 | + } |
| 23 | + |
| 24 | + [UnityTearDown] |
| 25 | + public IEnumerator Teardown() |
| 26 | + { |
| 27 | + // Shutdown and clean up both of our NetworkManager instances |
| 28 | + if (m_Prefab) |
| 29 | + { |
| 30 | + MultiInstanceHelpers.Destroy(); |
| 31 | + Object.Destroy(m_Prefab); |
| 32 | + m_Prefab = null; |
| 33 | + } |
| 34 | + NetworkVariableInitOnNetworkSpawn.NetworkSpawnCalledOnClient = false; |
| 35 | + NetworkVariableInitOnNetworkSpawn.NetworkSpawnCalledOnServer = false; |
| 36 | + yield break; |
| 37 | + } |
| 38 | + |
| 39 | + [UnityTest] |
| 40 | + [Description("When a network variable is initialized in OnNetworkSpawn on the server, the spawned object's NetworkVariable on the client is initialized with the same value.")] |
| 41 | + public IEnumerator WhenANetworkVariableIsInitializedInOnNetworkSpawnOnTheServer_TheSpawnedObjectsNetworkVariableOnTheClientIsInitializedWithTheSameValue() |
| 42 | + { |
| 43 | + const int numClients = 1; |
| 44 | + Assert.True(MultiInstanceHelpers.Create(numClients, out NetworkManager server, out NetworkManager[] clients)); |
| 45 | + m_Prefab = new GameObject("Object"); |
| 46 | + var networkObject = m_Prefab.AddComponent<NetworkObject>(); |
| 47 | + m_Prefab.AddComponent<NetworkVariableInitOnNetworkSpawn>(); |
| 48 | + |
| 49 | + // Make it a prefab |
| 50 | + MultiInstanceHelpers.MakeNetworkObjectTestPrefab(networkObject); |
| 51 | + |
| 52 | + var validNetworkPrefab = new NetworkPrefab(); |
| 53 | + validNetworkPrefab.Prefab = m_Prefab; |
| 54 | + server.NetworkConfig.NetworkPrefabs.Add(validNetworkPrefab); |
| 55 | + foreach (var client in clients) |
| 56 | + { |
| 57 | + client.NetworkConfig.NetworkPrefabs.Add(validNetworkPrefab); |
| 58 | + } |
| 59 | + |
| 60 | + // Start the instances |
| 61 | + if (!MultiInstanceHelpers.Start(true, server, clients)) |
| 62 | + { |
| 63 | + Debug.LogError("Failed to start instances"); |
| 64 | + Assert.Fail("Failed to start instances"); |
| 65 | + } |
| 66 | + |
| 67 | + // [Client-Side] Wait for a connection to the server |
| 68 | + yield return MultiInstanceHelpers.Run(MultiInstanceHelpers.WaitForClientsConnected(clients, null, 512)); |
| 69 | + |
| 70 | + // [Host-Side] Check to make sure all clients are connected |
| 71 | + yield return MultiInstanceHelpers.Run( |
| 72 | + MultiInstanceHelpers.WaitForClientsConnectedToServer(server, clients.Length + 1, null, 512)); |
| 73 | + |
| 74 | + var serverObject = Object.Instantiate(m_Prefab, Vector3.zero, Quaternion.identity); |
| 75 | + NetworkObject serverNetworkObject = serverObject.GetComponent<NetworkObject>(); |
| 76 | + serverNetworkObject.NetworkManagerOwner = server; |
| 77 | + serverNetworkObject.Spawn(); |
| 78 | + |
| 79 | + // Wait until all objects have spawned. |
| 80 | + const int expectedNetworkObjects = numClients + 2; // +2 = one for prefab, one for server. |
| 81 | + const int maxFrames = 240; |
| 82 | + var doubleCheckTime = Time.realtimeSinceStartup + 5.0f; |
| 83 | + while (Object.FindObjectsOfType<NetworkObject>().Length != expectedNetworkObjects) |
| 84 | + { |
| 85 | + if (Time.frameCount > maxFrames) |
| 86 | + { |
| 87 | + // This is here in the event a platform is running at a higher |
| 88 | + // frame rate than expected |
| 89 | + if (doubleCheckTime < Time.realtimeSinceStartup) |
| 90 | + { |
| 91 | + Assert.Fail("Did not successfully spawn all expected NetworkObjects"); |
| 92 | + break; |
| 93 | + } |
| 94 | + } |
| 95 | + var nextFrameNumber = Time.frameCount + 1; |
| 96 | + yield return new WaitUntil(() => Time.frameCount >= nextFrameNumber); |
| 97 | + } |
| 98 | + Assert.IsTrue(NetworkVariableInitOnNetworkSpawn.NetworkSpawnCalledOnServer); |
| 99 | + Assert.IsTrue(NetworkVariableInitOnNetworkSpawn.NetworkSpawnCalledOnClient); |
| 100 | + } |
| 101 | + } |
| 102 | +} |
0 commit comments