@@ -18,14 +18,14 @@ namespace TestProject.RuntimeTests
1818 [ TestFixture ( NetworkTopologyTypes . ClientServer , HostOrServer . Server ) ]
1919 public class InScenePlacedNetworkObjectTests : IntegrationTestWithApproximation
2020 {
21- protected override int NumberOfClients => 3 ;
21+ protected override int NumberOfClients => 2 ;
2222
2323 private const string k_SceneToLoad = "InSceneNetworkObject" ;
2424 private const string k_InSceneUnder = "InSceneUnderGameObject" ;
2525 private const string k_InSceneUnderWithNT = "InSceneUnderGameObjectWithNT" ;
2626 private Scene m_ServerSideSceneLoaded ;
27- private bool m_CanStartServerAndClients ;
2827 private string m_SceneLoading = k_SceneToLoad ;
28+ private NetworkManager m_LateJoinClient ;
2929
3030 // TODO: [CmbServiceTests] Adapt to run with the service
3131 protected override bool UseCMBService ( )
@@ -38,9 +38,7 @@ public InScenePlacedNetworkObjectTests(NetworkTopologyTypes networkTopologyType,
3838 protected override IEnumerator OnSetup ( )
3939 {
4040 NetworkObjectTestComponent . Reset ( ) ;
41- m_EnableVerboseDebug = true ;
4241 NetworkObjectTestComponent . VerboseDebug = m_EnableVerboseDebug ;
43- m_CanStartServerAndClients = false ;
4442 return base . OnSetup ( ) ;
4543 }
4644
@@ -52,12 +50,14 @@ protected override IEnumerator OnSetup()
5250 /// <returns></returns>
5351 protected override IEnumerator OnTearDown ( )
5452 {
53+ m_LateJoinClient = null ;
5554 yield return CleanUpLoadedScene ( ) ;
5655 }
5756
58- protected override bool CanStartServerAndClients ( )
57+ protected override void OnNewClientCreated ( NetworkManager networkManager )
5958 {
60- return m_CanStartServerAndClients ;
59+ m_LateJoinClient = networkManager ;
60+ base . OnNewClientCreated ( networkManager ) ;
6161 }
6262
6363 public enum DespawnMode
@@ -81,30 +81,11 @@ public IEnumerator InSceneNetworkObjectSynchAndSpawn([Values] DespawnMode despaw
8181 Assert . Ignore ( $ "Test ignored as DeferDespawn is only valid with Distributed Authority mode.") ;
8282 }
8383
84- NetworkObjectTestComponent . VerboseDebug = false ;
85- // Because despawning a client will cause it to shutdown and clean everything in the
86- // scene hierarchy, we have to prevent one of the clients from spawning initially before
87- // we test synchronizing late joining clients with despawned in-scene placed NetworkObjects.
88- // So, we prevent the automatic starting of the server and clients, remove the client we
89- // will be targeting to join late from the m_ClientNetworkManagers array, start the server
90- // and the remaining client, despawn the in-scene NetworkObject, and then start and synchronize
91- // the clientToTest.
92- var clientToTest = m_ClientNetworkManagers [ 2 ] ;
93- var clients = m_ClientNetworkManagers . ToList ( ) ;
94- clients . Remove ( clientToTest ) ;
95- m_ClientNetworkManagers = clients . ToArray ( ) ;
96- m_CanStartServerAndClients = true ;
97- NetworkObjectTestComponent . Reset ( ) ;
98- yield return StartServerAndClients ( ) ;
99- clients . Add ( clientToTest ) ;
100- m_ClientNetworkManagers = clients . ToArray ( ) ;
101-
102-
10384 m_ServerNetworkManager . SceneManager . OnSceneEvent += Server_OnSceneEvent ;
10485 var status = m_ServerNetworkManager . SceneManager . LoadScene ( k_SceneToLoad , LoadSceneMode . Additive ) ;
10586 Assert . IsTrue ( status == SceneEventProgressStatus . Started , $ "When attempting to load scene { k_SceneToLoad } was returned the following progress status: { status } ") ;
106- // We removed a client from the initial spawn and server should spawn too
107- var clientCount = TotalClients - ( m_UseHost ? 1 : 0 ) ;
87+
88+ var clientCount = NumberOfClients + 1 ;
10889
10990 // This verifies the scene loaded and the in-scene placed NetworkObjects spawned.
11091 yield return WaitForConditionOrTimeOut ( ( ) => NetworkObjectTestComponent . SpawnedInstances . Count == clientCount ) ;
@@ -136,24 +117,24 @@ public IEnumerator InSceneNetworkObjectSynchAndSpawn([Values] DespawnMode despaw
136117
137118 // Now late join a client
138119 NetworkObjectTestComponent . OnInSceneObjectDespawned += OnInSceneObjectDespawned ;
139- NetcodeIntegrationTestHelpers . StartOneClient ( clientToTest ) ;
120+ yield return CreateAndStartNewClient ( ) ;
140121 // Spawned another client
141122 clientCount ++ ;
142123
143- yield return WaitForConditionOrTimeOut ( ( ) => ( clientToTest . IsConnectedClient && clientToTest . IsListening ) ) ;
144- AssertOnTimeout ( $ "Timed out waiting for { clientToTest . name } to reconnect !") ;
124+ yield return WaitForConditionOrTimeOut ( ( ) => ( m_LateJoinClient . IsConnectedClient && m_LateJoinClient . IsListening ) ) ;
125+ AssertOnTimeout ( $ "Timed out waiting for { m_LateJoinClient . name } to connect !") ;
145126
146127 yield return s_DefaultWaitForTick ;
147128
148129 // Make sure the late-joining client's in-scene placed NetworkObject received the despawn notification during synchronization
149- Assert . IsNotNull ( m_JoinedClientDespawnedNetworkObject , $ "{ clientToTest . name } did not despawn the in-scene placed NetworkObject when connecting and synchronizing!") ;
130+ Assert . IsNotNull ( m_JoinedClientDespawnedNetworkObject , $ "{ m_LateJoinClient . name } did not despawn the in-scene placed NetworkObject when connecting and synchronizing!") ;
150131
151132 // Update the newly joined client information
152133 ClientNetworkManagerPostStartInit ( ) ;
153134
154135 // We should still have no spawned in-scene placed NetworkObjects at this point
155136 yield return WaitForConditionOrTimeOut ( ( ) => NetworkObjectTestComponent . SpawnedInstances . Count == 0 ) ;
156- AssertOnTimeout ( $ "{ clientToTest . name } spawned in-scene placed NetworkObject!") ;
137+ AssertOnTimeout ( $ "{ m_LateJoinClient . name } spawned in-scene placed NetworkObject!") ;
157138
158139 // Now test that the despawned in-scene placed NetworkObject can be re-spawned (without having been registered as a NetworkPrefab)
159140 serverObject . Spawn ( ) ;
@@ -185,22 +166,6 @@ public IEnumerator InSceneNetworkObjectSynchAndSpawn([Values] DespawnMode despaw
185166 [ UnityTest ]
186167 public IEnumerator ParentedInSceneObjectLateJoiningClient ( )
187168 {
188- // Because despawning a client will cause it to shutdown and clean everything in the
189- // scene hierarchy, we have to prevent one of the clients from spawning initially before
190- // we test synchronizing late joining clients.
191- // So, we prevent the automatic starting of the server and clients, remove the client we
192- // will be targeting to join late from the m_ClientNetworkManagers array, start the server
193- // and the remaining client, despawn the in-scene NetworkObject, and then start and synchronize
194- // the clientToTest.
195- var clientToTest = m_ClientNetworkManagers [ 2 ] ;
196- var clients = m_ClientNetworkManagers . ToList ( ) ;
197- clients . Remove ( clientToTest ) ;
198- m_ClientNetworkManagers = clients . ToArray ( ) ;
199- m_CanStartServerAndClients = true ;
200- yield return StartServerAndClients ( ) ;
201- clients . Add ( clientToTest ) ;
202- m_ClientNetworkManagers = clients . ToArray ( ) ;
203-
204169 NetworkObjectTestComponent . ServerNetworkObjectInstance = null ;
205170
206171 m_ClientNetworkManagers [ 0 ] . SceneManager . OnSceneEvent += OnSceneEvent ;
@@ -232,9 +197,9 @@ public IEnumerator ParentedInSceneObjectLateJoiningClient()
232197 yield return WaitForConditionOrTimeOut ( ( ) => firstClientInSceneObjectInstance . transform . parent != null && firstClientInSceneObjectInstance . transform . parent == clientSidePlayer . transform ) ;
233198 AssertOnTimeout ( $ "Timed out waiting for the client-side id ({ m_ClientNetworkManagers [ 0 ] . LocalClientId } ) server player transform to be set on the client-side in-scene object!") ;
234199 // Now late join a client
235- NetcodeIntegrationTestHelpers . StartOneClient ( clientToTest ) ;
236- yield return WaitForConditionOrTimeOut ( ( ) => ( clientToTest . IsConnectedClient && clientToTest . IsListening ) ) ;
237- AssertOnTimeout ( $ "Timed out waiting for { clientToTest . name } to reconnect!") ;
200+ yield return CreateAndStartNewClient ( ) ;
201+ yield return WaitForConditionOrTimeOut ( ( ) => ( m_LateJoinClient . IsConnectedClient && m_LateJoinClient . IsListening ) ) ;
202+ AssertOnTimeout ( $ "Timed out waiting for { m_LateJoinClient . name } to reconnect!") ;
238203
239204 yield return s_DefaultWaitForTick ;
240205
@@ -245,7 +210,7 @@ public IEnumerator ParentedInSceneObjectLateJoiningClient()
245210 Assert . IsNotNull ( lateJoinClientInSceneObjectInstance , $ "Could not get the client-side registration of { nameof ( NetworkObjectTestComponent ) } for the late joining client!") ;
246211
247212 // Now get the late-joining client's instance for the server player
248- clientSidePlayer = m_PlayerNetworkObjects [ clientToTest . LocalClientId ] [ clientSidePlayer . OwnerClientId ] ;
213+ clientSidePlayer = m_PlayerNetworkObjects [ m_LateJoinClient . LocalClientId ] [ clientSidePlayer . OwnerClientId ] ;
249214
250215 // Validate the late joined client's in-scene NetworkObject is parented to the server-side player
251216 yield return WaitForConditionOrTimeOut ( ( ) => lateJoinClientInSceneObjectInstance . transform . parent != null && lateJoinClientInSceneObjectInstance . transform . parent == clientSidePlayer . transform ) ;
@@ -360,11 +325,8 @@ public IEnumerator EnableDisableInSceneObjectTests()
360325 // Enabled disabling the NetworkObject when it is despawned
361326 NetworkObjectTestComponent . DisableOnDespawn = true ;
362327 // Set the number of instances to expect
363- m_NumberOfInstancesCheck = NumberOfClients + ( m_UseHost ? 1 : 0 ) ;
328+ m_NumberOfInstancesCheck = TotalClients ;
364329
365- // Start the host and clients and load the in-scene object scene additively
366- m_CanStartServerAndClients = true ;
367- yield return StartServerAndClients ( ) ;
368330 m_ServerNetworkManager . SceneManager . OnLoadEventCompleted += SceneManager_OnLoadEventCompleted ;
369331 m_ServerNetworkManager . SceneManager . LoadScene ( k_SceneToLoad , LoadSceneMode . Additive ) ;
370332 yield return WaitForConditionOrTimeOut ( ( ) => m_AllClientsLoadedScene ) ;
@@ -499,28 +461,12 @@ public IEnumerator ParentedInSceneObjectUnderGameObject([Values(k_InSceneUnder,
499461 var useNetworkTransform = m_SceneLoading == k_InSceneUnderWithNT ;
500462
501463 m_SceneLoading = inSceneUnderToLoad ;
502- // Because despawning a client will cause it to shutdown and clean everything in the
503- // scene hierarchy, we have to prevent one of the clients from spawning initially before
504- // we test synchronizing late joining clients.
505- // So, we prevent the automatic starting of the server and clients, remove the client we
506- // will be targeting to join late from the m_ClientNetworkManagers array, start the server
507- // and the remaining client, despawn the in-scene NetworkObject, and then start and synchronize
508- // the clientToTest.
509- var clientToTest = m_ClientNetworkManagers [ 1 ] ;
510- var clients = m_ClientNetworkManagers . ToList ( ) ;
511464
512465 // Note: This test is a modified copy of ParentedInSceneObjectLateJoiningClient.
513466 // The 1st client is being ignored in this test and the focus is primarily on the late joining
514467 // 2nd client after adjustments have been made to the child NetworkBehaviour and if applicable
515468 // NetworkTransform.
516469
517- clients . Remove ( clientToTest ) ;
518- m_ClientNetworkManagers = clients . ToArray ( ) ;
519- m_CanStartServerAndClients = true ;
520- yield return StartServerAndClients ( ) ;
521- clients . Add ( clientToTest ) ;
522- m_ClientNetworkManagers = clients . ToArray ( ) ;
523-
524470 NetworkObjectTestComponent . ServerNetworkObjectInstance = null ;
525471
526472 m_ClientNetworkManagers [ 0 ] . SceneManager . OnSceneEvent += OnSceneEvent ;
@@ -545,9 +491,9 @@ public IEnumerator ParentedInSceneObjectUnderGameObject([Values(k_InSceneUnder,
545491 }
546492
547493 // Now late join a client
548- NetcodeIntegrationTestHelpers . StartOneClient ( clientToTest ) ;
549- yield return WaitForConditionOrTimeOut ( ( ) => ( clientToTest . IsConnectedClient && clientToTest . IsListening ) ) ;
550- AssertOnTimeout ( $ "Timed out waiting for { clientToTest . name } to reconnect!") ;
494+ yield return CreateAndStartNewClient ( ) ;
495+ yield return WaitForConditionOrTimeOut ( ( ) => ( m_LateJoinClient . IsConnectedClient && m_LateJoinClient . IsListening ) ) ;
496+ AssertOnTimeout ( $ "Timed out waiting for { m_LateJoinClient . name } to reconnect!") ;
551497
552498 yield return s_DefaultWaitForTick ;
553499
0 commit comments