11using System . Collections ;
22using System . Collections . Generic ;
33using System . Linq ;
4+ using System . Text ;
45using NUnit . Framework ;
56using Unity . Netcode . TestHelpers . Runtime ;
67using UnityEngine ;
78using UnityEngine . TestTools ;
8- using Object = UnityEngine . Object ;
99
1010namespace Unity . Netcode . RuntimeTests
1111{
@@ -85,15 +85,6 @@ public void SetOwnerWrite()
8585 m_SeconValue = new NetworkVariable < int > ( default , NetworkVariableReadPermission . Everyone , NetworkVariableWritePermission . Owner ) ;
8686 }
8787
88- public override void OnNetworkSpawn ( )
89- {
90- // Non-Authority will register each NetworkObject when it is spawned
91- if ( ( NetworkManager . DistributedAuthorityMode && ! IsOwner ) || ( ! NetworkManager . DistributedAuthorityMode && ! IsServer ) )
92- {
93- NetworkBehaviourUpdaterTests . ClientSideNotifyObjectSpawned ( gameObject ) ;
94- }
95- }
96-
9788 /// <summary>
9889 /// Server side only, sets the NetworkVariables being used to the ValueToSetNetVarTo
9990 /// that is pre-configured when the Network Prefab is created.
@@ -160,12 +151,14 @@ internal struct NetVarCombinationTypes
160151 internal class NetworkBehaviourUpdaterTests : NetcodeIntegrationTest
161152 {
162153 // Go ahead and create maximum number of clients (not all tests will use them)
163- protected override int NumberOfClients => m_NumberOfClients ;
154+ protected override int NumberOfClients => m_ClientCount ;
164155 public const int NetVarValueToSet = 1 ;
165- private static List < GameObject > s_ClientSpawnedNetworkObjects = new List < GameObject > ( ) ;
156+ private List < ulong > m_SpawnedObjects = new List < ulong > ( ) ;
166157 private GameObject m_PrefabToSpawn ;
167158 private NetVarCombinationTypes m_NetVarCombinationTypes ;
168- private int m_NumberOfClients = 0 ;
159+ private int m_ClientCount = 0 ;
160+
161+ private StringBuilder m_ErrorLog = new StringBuilder ( ) ;
169162
170163 public NetworkBehaviourUpdaterTests ( HostOrServer hostOrServer , int numberOfClients , NetVarContainer . NetVarsToCheck first , NetVarContainer . NetVarsToCheck second ) : base ( hostOrServer )
171164 {
@@ -174,25 +167,24 @@ public NetworkBehaviourUpdaterTests(HostOrServer hostOrServer, int numberOfClien
174167 FirstType = first ,
175168 SecondType = second
176169 } ;
177- m_NumberOfClients = numberOfClients ;
170+ // Adjust the client count if connecting to the service.
171+ m_ClientCount = UseCMBServiceEnviromentVariableSet ( ) ? numberOfClients + 1 : numberOfClients ;
178172 }
179173
180- protected override IEnumerator OnSetup ( )
174+ protected override bool UseCMBService ( )
181175 {
182- s_ClientSpawnedNetworkObjects . Clear ( ) ;
183- return base . OnSetup ( ) ;
176+ return true ;
184177 }
185178
186- /// <summary>
187- /// Clients will call this when NetworkObjects are spawned on their end
188- /// </summary>
189- /// <param name="objectSpaned">the GameObject of the NetworkObject spawned</param>
190- public static void ClientSideNotifyObjectSpawned ( GameObject objectSpaned )
179+ protected override void OnOneTimeTearDown ( )
191180 {
192- if ( ! s_ClientSpawnedNetworkObjects . Contains ( objectSpaned ) )
193- {
194- s_ClientSpawnedNetworkObjects . Add ( objectSpaned ) ;
195- }
181+ base . OnOneTimeTearDown ( ) ;
182+ }
183+
184+ protected override IEnumerator OnSetup ( )
185+ {
186+ m_SpawnedObjects . Clear ( ) ;
187+ return base . OnSetup ( ) ;
196188 }
197189
198190 protected override void OnServerAndClientsCreated ( )
@@ -221,6 +213,31 @@ protected override void OnServerAndClientsCreated()
221213 base . OnServerAndClientsCreated ( ) ;
222214 }
223215
216+ /// <summary>
217+ /// Determines if all clients have spawned clone instances.
218+ /// </summary>
219+ /// <remarks>
220+ /// <see cref="m_ErrorLog"/> will contain log entries of the
221+ /// <see cref="NetworkManager"/> instances and NetworkObjects
222+ /// that did not get spawned.
223+ /// </remarks>
224+ /// <returns>true(success) or false (failure)</returns>
225+ private bool AllClientsSpawnedObjects ( )
226+ {
227+ m_ErrorLog . Clear ( ) ;
228+ foreach ( var networkManager in m_NetworkManagers )
229+ {
230+ foreach ( var networkObjectId in m_SpawnedObjects )
231+ {
232+ if ( ! networkManager . SpawnManager . SpawnedObjects . ContainsKey ( networkObjectId ) )
233+ {
234+ m_ErrorLog . AppendLine ( $ "[{ networkManager . name } ] Has not spawned { nameof ( NetworkObject ) } -{ networkObjectId } .") ;
235+ }
236+ }
237+ }
238+ return m_ErrorLog . Length == 0 ;
239+ }
240+
224241 /// <summary>
225242 /// The updated BehaviourUpdaterAllTests was re-designed to replicate the same functionality being tested in the
226243 /// original version of this test with additional time out handling and a re-organization in the order of operations.
@@ -243,22 +260,19 @@ public IEnumerator BehaviourUpdaterAllTests([Values(1, 2)] int numToSpawn)
243260 // the appropriate number of NetworkObjects with the NetVarContainer behaviour
244261 var numberOfObjectsToSpawn = numToSpawn * NumberOfClients ;
245262
246- var authority = m_NetworkTopologyType == NetworkTopologyTypes . DistributedAuthority ? m_ClientNetworkManagers [ 0 ] : m_ServerNetworkManager ;
263+ var authority = GetAuthorityNetworkManager ( ) ;
247264
248265 // spawn the objects
249266 for ( int i = 0 ; i < numToSpawn ; i ++ )
250267 {
251- var spawnedObject = Object . Instantiate ( m_PrefabToSpawn ) ;
268+ var spawnedObject = SpawnObject ( m_PrefabToSpawn , authority ) ;
252269 spawnedPrefabs . Add ( spawnedObject ) ;
253- var networkSpawnedObject = spawnedObject . GetComponent < NetworkObject > ( ) ;
254- networkSpawnedObject . NetworkManagerOwner = authority ;
255- networkSpawnedObject . Spawn ( ) ;
270+ m_SpawnedObjects . Add ( spawnedObject . GetComponent < NetworkObject > ( ) . NetworkObjectId ) ;
256271 }
257272
258273 // Waits for all clients to spawn the NetworkObjects
259- yield return WaitForConditionOrTimeOut ( ( ) => numberOfObjectsToSpawn == s_ClientSpawnedNetworkObjects . Count ) ;
260- Assert . IsFalse ( s_GlobalTimeoutHelper . TimedOut , $ "Timed out waiting for clients to report spawning objects! " +
261- $ "Total reported client-side spawned objects { s_ClientSpawnedNetworkObjects . Count } ") ;
274+ yield return WaitForConditionOrTimeOut ( AllClientsSpawnedObjects ) ;
275+ AssertOnTimeout ( $ "Timed out waiting for clients to report spawning objects!\n { m_ErrorLog } ") ;
262276
263277
264278 // Once all clients have spawned the NetworkObjects, set the network variables for
@@ -287,12 +301,19 @@ public IEnumerator BehaviourUpdaterAllTests([Values(1, 2)] int numToSpawn)
287301
288302 // Get a list of all NetVarContainer components on the client-side spawned NetworkObjects
289303 var clientSideNetVarContainers = new List < NetVarContainer > ( ) ;
290- foreach ( var clientSpawnedObjects in s_ClientSpawnedNetworkObjects )
304+ foreach ( var networkManager in m_NetworkManagers )
291305 {
292- var netVarContainers = clientSpawnedObjects . GetComponents < NetVarContainer > ( ) ;
293- foreach ( var netvarContiner in netVarContainers )
306+ if ( networkManager == authority )
294307 {
295- clientSideNetVarContainers . Add ( netvarContiner ) ;
308+ continue ;
309+ }
310+ foreach ( var networkObjectId in m_SpawnedObjects )
311+ {
312+ var netVarContainers = networkManager . SpawnManager . SpawnedObjects [ networkObjectId ] . GetComponents < NetVarContainer > ( ) ;
313+ foreach ( var netvarContiner in netVarContainers )
314+ {
315+ clientSideNetVarContainers . Add ( netvarContiner ) ;
316+ }
296317 }
297318 }
298319
0 commit comments