|
1 | 1 | using System; |
2 | 2 | using System.Collections; |
| 3 | +using System.Collections.Generic; |
3 | 4 | using System.Linq; |
4 | 5 | using NUnit.Framework; |
5 | 6 | using Unity.Netcode.TestHelpers.Runtime; |
|
8 | 9 |
|
9 | 10 | namespace Unity.Netcode.RuntimeTests |
10 | 11 | { |
11 | | - internal class NetworkPrefabHandlerWithDataTests |
| 12 | + internal class NetworkPrefabHandlerWithDataTests : NetcodeIntegrationTest |
12 | 13 | { |
13 | | - private const int k_ClientCount = 4; |
| 14 | + protected override int NumberOfClients => 4; |
14 | 15 | private const string k_TestPrefabObjectName = "NetworkPrefabTestObject"; |
15 | | - private uint m_ObjectId = 1; |
| 16 | + private GameObject m_Prefab; |
| 17 | + private PrefabInstanceHandlerWithData[] m_ClientHandlers; |
16 | 18 |
|
17 | | - private GameObject _prefab; |
18 | | - private NetworkManager server; |
19 | | - private NetworkManager[] clients; |
20 | | - |
21 | | - private PrefabInstanceHandlerWithData[] clientHandlers; |
22 | | - |
23 | | - [SetUp] |
24 | | - public void Setup() |
| 19 | + protected override void OnServerAndClientsCreated() |
25 | 20 | { |
26 | | - NetcodeIntegrationTestHelpers.Create(k_ClientCount, out server, out clients); |
27 | | - _prefab = CreateNetworkPrefab(); |
| 21 | + // Creates a network object prefab and registers it to all clients. |
| 22 | + m_Prefab = CreateNetworkObjectPrefab(k_TestPrefabObjectName).gameObject; |
| 23 | + var authority = GetAuthorityNetworkManager(); |
28 | 24 |
|
29 | | - RegisterPrefab(server, out _); |
30 | | - |
31 | | - clientHandlers = new PrefabInstanceHandlerWithData[clients.Length]; |
32 | | - for (int i = 0; i < clients.Length; i++) |
| 25 | + m_ClientHandlers = new PrefabInstanceHandlerWithData[NumberOfClients]; |
| 26 | + var idx = 0; |
| 27 | + foreach (var manager in m_NetworkManagers) |
33 | 28 | { |
34 | | - RegisterPrefab(clients[i], out clientHandlers[i]); |
| 29 | + RegisterPrefabHandler(manager, out var handler); |
| 30 | + if (manager != authority) |
| 31 | + { |
| 32 | + m_ClientHandlers[idx] = handler; |
| 33 | + idx++; |
| 34 | + } |
35 | 35 | } |
36 | 36 | } |
37 | 37 |
|
38 | | - [TearDown] |
39 | | - public void Teardown() |
| 38 | + private PrefabInstanceHandlerWithData m_LateJoinPrefabHandler; |
| 39 | + protected override void OnNewClientCreated(NetworkManager networkManager) |
40 | 40 | { |
41 | | - foreach (var client in clients) |
42 | | - { |
43 | | - client.PrefabHandler.RemoveHandler(_prefab); |
44 | | - client.NetworkConfig.Prefabs.Remove(_prefab); |
45 | | - client.Shutdown(); |
46 | | - } |
47 | | - |
48 | | - server.PrefabHandler.RemoveHandler(_prefab); |
49 | | - server.NetworkConfig.Prefabs.Remove(_prefab); |
50 | | - server.Shutdown(); |
| 41 | + // This will register all prefabs from the authority to the newly created client. |
| 42 | + base.OnNewClientCreated(networkManager); |
51 | 43 |
|
52 | | - UnityEngine.Object.DestroyImmediate(_prefab); |
53 | | - NetcodeIntegrationTestHelpers.Destroy(); |
| 44 | + RegisterPrefabHandler(networkManager, out var lateJoinPrefabHandler); |
| 45 | + m_LateJoinPrefabHandler = lateJoinPrefabHandler; |
54 | 46 | } |
55 | 47 |
|
56 | 48 | [UnityTest] |
57 | 49 | public IEnumerator InstantiationPayload_SyncsCorrectly() |
58 | 50 | { |
59 | | - yield return StartAndWaitForClients(); |
60 | 51 | var data = new NetworkSerializableTest { Value = 12, Value2 = 3.14f }; |
| 52 | + |
61 | 53 | SpawnPrefabWithData(data); |
62 | | - yield return WaitForAllClientsToSync(data); |
| 54 | + |
| 55 | + yield return WaitForConditionOrTimeOut(() => AllHandlersSynchronized(data)); |
| 56 | + AssertOnTimeout("Not all handlers synchronized"); |
63 | 57 | } |
64 | 58 |
|
65 | 59 | [UnityTest] |
66 | 60 | public IEnumerator InstantiationPayload_LateJoinersReceiveData() |
67 | 61 | { |
68 | | - yield return StartAndWaitForClients(); |
69 | 62 | var data = new NetworkSerializableTest { Value = 42, Value2 = 2.71f }; |
70 | 63 | SpawnPrefabWithData(data); |
71 | 64 |
|
72 | | - // Disconnect and destroy one client to simulate late join |
73 | | - var lateJoiner = clients[0]; |
74 | | - lateJoiner.Shutdown(); |
75 | | - yield return null; |
| 65 | + yield return WaitForConditionOrTimeOut(() => AllHandlersSynchronized(data)); |
| 66 | + AssertOnTimeout("Not all handlers synchronized"); |
76 | 67 |
|
77 | | - var lateJoinerIndex = 0; |
78 | | - clients[lateJoinerIndex] = NetcodeIntegrationTestHelpers.CreateNewClient(k_ClientCount); |
79 | | - RegisterPrefab(clients[lateJoinerIndex], out clientHandlers[lateJoinerIndex]); |
80 | | - |
81 | | - NetcodeIntegrationTestHelpers.StartOneClient(clients[lateJoinerIndex]); |
82 | | - yield return NetcodeIntegrationTestHelpers.WaitForClientConnected(clients[lateJoinerIndex]); |
| 68 | + // Late join a client |
| 69 | + yield return CreateAndStartNewClient(); |
83 | 70 |
|
84 | 71 | // Confirm late joiner got correct data |
85 | | - var timeoutHelper = new TimeoutHelper(); |
86 | | - yield return NetcodeIntegrationTest.WaitForConditionOrTimeOut(() => clientHandlers[lateJoinerIndex].instantiationData.IsSynchronizedWith(data)); |
87 | | - Assert.False(timeoutHelper.TimedOut, "Late joiner did not synchronize properly with instantiation data."); |
| 72 | + yield return WaitForConditionOrTimeOut(() => m_LateJoinPrefabHandler.InstantiationData.IsSynchronizedWith(data)); |
| 73 | + AssertOnTimeout("Late joiner received incorrect data"); |
88 | 74 | } |
89 | 75 |
|
90 | | - private GameObject CreateNetworkPrefab() |
91 | | - { |
92 | | - var guid = NetworkManagerHelper.AddGameNetworkObject($"{k_TestPrefabObjectName}{m_ObjectId++}"); |
93 | | - var networkObject = NetworkManagerHelper.InstantiatedNetworkObjects[guid]; |
94 | | - NetcodeIntegrationTestHelpers.MakeNetworkObjectTestPrefab(networkObject); |
95 | | - return networkObject.gameObject; |
96 | | - } |
97 | 76 |
|
98 | | - private void RegisterPrefab(NetworkManager manager, out PrefabInstanceHandlerWithData handler) |
| 77 | + private void RegisterPrefabHandler(NetworkManager manager, out PrefabInstanceHandlerWithData handler) |
99 | 78 | { |
100 | | - var networkPrefab = new NetworkPrefab { Prefab = _prefab }; |
101 | | - manager.NetworkConfig.Prefabs.Add(networkPrefab); |
102 | | - |
103 | | - handler = new PrefabInstanceHandlerWithData(_prefab); |
104 | | - manager.PrefabHandler.AddHandler(_prefab, handler); |
| 79 | + handler = new PrefabInstanceHandlerWithData(m_Prefab); |
| 80 | + manager.PrefabHandler.AddHandler(m_Prefab, handler); |
105 | 81 | } |
106 | 82 |
|
107 | | - private NetworkObject SpawnPrefabWithData(NetworkSerializableTest data) |
| 83 | + private void SpawnPrefabWithData(NetworkSerializableTest data) |
108 | 84 | { |
109 | | - var instance = GameObject.Instantiate(_prefab).GetComponent<NetworkObject>(); |
110 | | - server.PrefabHandler.SetInstantiationData(instance, data); |
| 85 | + var instance = UnityEngine.Object.Instantiate(m_Prefab).GetComponent<NetworkObject>(); |
| 86 | + GetAuthorityNetworkManager().PrefabHandler.SetInstantiationData(instance, data); |
111 | 87 | instance.Spawn(); |
112 | | - return instance; |
113 | | - } |
114 | | - |
115 | | - private IEnumerator StartAndWaitForClients() |
116 | | - { |
117 | | - if (!NetcodeIntegrationTestHelpers.Start(true, server, clients)) |
118 | | - Assert.Fail("Failed to start instances"); |
119 | | - |
120 | | - yield return NetcodeIntegrationTestHelpers.WaitForClientsConnected(clients, null, 512); |
121 | | - yield return NetcodeIntegrationTestHelpers.WaitForClientsConnectedToServer(server, clients.Length + 1, null, 512); |
122 | 88 | } |
123 | 89 |
|
124 | | - private IEnumerator WaitForAllClientsToSync(NetworkSerializableTest expectedData) |
| 90 | + private bool AllHandlersSynchronized(NetworkSerializableTest expectedData) |
125 | 91 | { |
126 | | - var timeoutHelper = new TimeoutHelper(); |
127 | | - yield return NetcodeIntegrationTest.WaitForConditionOrTimeOut(() => clientHandlers.All(h => h.instantiationData.IsSynchronizedWith(expectedData))); |
128 | | - Assert.False(timeoutHelper.TimedOut, "Data did not synchronize correctly to all clients."); |
| 92 | + return m_ClientHandlers.All(handler => handler.InstantiationData.IsSynchronizedWith(expectedData)); |
129 | 93 | } |
130 | 94 |
|
131 | 95 | private class PrefabInstanceHandlerWithData : NetworkPrefabInstanceHandlerWithData<NetworkSerializableTest> |
132 | 96 | { |
133 | | - public GameObject Prefab; |
134 | | - public NetworkSerializableTest instantiationData; |
| 97 | + private readonly GameObject m_Prefab; |
| 98 | + public NetworkSerializableTest InstantiationData; |
135 | 99 |
|
136 | 100 | public PrefabInstanceHandlerWithData(GameObject prefab) |
137 | 101 | { |
138 | | - Prefab = prefab; |
| 102 | + m_Prefab = prefab; |
139 | 103 | } |
140 | 104 |
|
141 | 105 | public override NetworkObject Instantiate(ulong ownerClientId, Vector3 position, Quaternion rotation, NetworkSerializableTest data) |
142 | 106 | { |
143 | | - instantiationData = data; |
144 | | - return GameObject.Instantiate(Prefab, position, rotation).GetComponent<NetworkObject>(); |
| 107 | + InstantiationData = data; |
| 108 | + return UnityEngine.Object.Instantiate(m_Prefab, position, rotation).GetComponent<NetworkObject>(); |
145 | 109 | } |
146 | 110 |
|
147 | 111 | public override void Destroy(NetworkObject networkObject) |
148 | 112 | { |
149 | | - GameObject.DestroyImmediate(networkObject.gameObject); |
| 113 | + UnityEngine.Object.DestroyImmediate(networkObject.gameObject); |
150 | 114 | } |
151 | 115 | } |
152 | 116 |
|
|
0 commit comments