@@ -162,10 +162,23 @@ protected NetworkManager GetAuthorityNetworkManager()
162162 {
163163 if ( m_UseCmbService )
164164 {
165+ // TODO: Because we start the NetworkManagers back-to-back, the service can make a NetworkManager
166+ // (other than the very first one) the session owner. This could lead to test instabilities and
167+ // we need to add additional code during the start process that will start the very 1st instance
168+ // and wait for it to be approved and assigned the session owner before we assume anything.
169+ // Otherwise, any pick from the m_NetworkManagers could result in that specific instance not being
170+ // the first session owner.
171+ // If we haven't even started any NetworkManager, then just return the first
172+ // instance until we resolve the above issue.
173+ if ( ! NetcodeIntegrationTestHelpers . IsStarted )
174+ {
175+ return m_NetworkManagers [ 0 ] ;
176+ }
177+
165178 foreach ( var client in m_NetworkManagers )
166179 {
167- // If client isn't approved we are still in setup and want to return the first client
168- // otherwise look for the session owner
180+ // See above notes.
181+ // TODO: Once the above is resolved, just check for client.LocalClient.IsSessionOwner
169182 if ( ! client . LocalClient . IsApproved || client . LocalClient . IsSessionOwner )
170183 {
171184 return client ;
@@ -203,7 +216,7 @@ protected NetworkManager GetNonAuthorityNetworkManager()
203216 /// Indicates whether the currently running tests are targeting the hosted CMB Service
204217 /// </summary>
205218 /// <remarks>Can only be true if <see cref="UseCMBService"/> returns true.</remarks>
206- protected bool m_UseCmbService ;
219+ protected bool m_UseCmbService { get ; private set ; }
207220
208221 /// <summary>
209222 /// Indicates whether a hosted CMB service is available.
@@ -419,6 +432,9 @@ protected virtual void OnInlineSetup()
419432 [ UnitySetUp ]
420433 public IEnumerator SetUp ( )
421434 {
435+ // In addition to setting the number of clients in the OneTimeSetup, we need to re-apply the number of clients for each unique test
436+ // in the event that a previous test stopped a client.
437+ m_NumberOfClients = NumberOfClients ;
422438 VerboseDebugLog . Clear ( ) ;
423439 VerboseDebug ( $ "Entering { nameof ( SetUp ) } ") ;
424440 NetcodeLogAssert = new NetcodeLogAssert ( ) ;
@@ -521,7 +537,7 @@ protected void CreateServerAndClients()
521537 {
522538 // If we are connecting to a CMB server and we have a zero client count,
523539 // then we must make m_NumberOfClients = 1 for the session owner.
524- if ( UseCMBService ( ) && NumberOfClients == 0 && m_NumberOfClients == 0 )
540+ if ( m_UseCmbService && NumberOfClients == 0 && m_NumberOfClients == 0 )
525541 {
526542 m_NumberOfClients = 1 ;
527543 }
@@ -816,7 +832,7 @@ protected void CreateServerAndClients(int numberOfClients)
816832 // In the event this is invoked within a derived integration test and
817833 // the number of clients is 0, then we need to have at least 1 client
818834 // to be the session owner.
819- if ( UseCMBService ( ) && numberOfClients == 0 )
835+ if ( m_UseCmbService && numberOfClients == 0 )
820836 {
821837 numberOfClients = 1 ;
822838 // If m_NumberOfCleints == 0, then we should increment it.
@@ -995,7 +1011,6 @@ protected virtual bool ShouldCheckForSpawnedPlayers()
9951011 return true ;
9961012 }
9971013
998-
9991014 /// <summary>
10001015 /// This starts the server and clients as long as <see cref="CanStartServerAndClients"/>
10011016 /// returns true.
@@ -1009,16 +1024,16 @@ protected IEnumerator StartServerAndClients()
10091024 // Start the instances and pass in our SceneManagerInitialization action that is invoked immediately after host-server
10101025 // is started and after each client is started.
10111026
1012- // When using the CMBService, we don't have a server, so get the appropriate authority network manager
1013- var authorityManager = GetAuthorityNetworkManager ( ) ;
1014-
10151027 VerboseDebug ( $ "Starting with useCmbService: { m_UseCmbService } ") ;
10161028 if ( ! NetcodeIntegrationTestHelpers . Start ( m_UseHost , ! m_UseCmbService , m_ServerNetworkManager , m_ClientNetworkManagers ) )
10171029 {
10181030 Debug . LogError ( "Failed to start instances" ) ;
10191031 Assert . Fail ( "Failed to start instances" ) ;
10201032 }
10211033
1034+ // When using the CMBService, we don't have a server, so get the appropriate authority network manager
1035+ var authorityManager = GetAuthorityNetworkManager ( ) ;
1036+
10221037 // When scene management is enabled, we need to re-apply the scenes populated list since we have overriden the ISceneManagerHandler
10231038 // imeplementation at this point. This assures any pre-loaded scenes will be automatically assigned to the server and force clients
10241039 // to load their own scenes.
@@ -1382,9 +1397,11 @@ private void DestroyNetworkManagers()
13821397 var networkManagers = Object . FindObjectsByType < NetworkManager > ( FindObjectsSortMode . None ) ;
13831398 foreach ( var networkManager in networkManagers )
13841399 {
1385-
13861400 Object . DestroyImmediate ( networkManager . gameObject ) ;
13871401 }
1402+ m_NetworkManagers = null ;
1403+ m_ClientNetworkManagers = null ;
1404+ m_ServerNetworkManager = null ;
13881405 }
13891406
13901407 /// <summary>
@@ -1949,7 +1966,6 @@ private void InitializeTestConfiguration(NetworkTopologyTypes networkTopologyTyp
19491966 if ( UseCMBService ( ) )
19501967 {
19511968 m_UseCmbService = m_DistributedAuthority && hostOrServer == HostOrServer . DAHost ;
1952-
19531969 }
19541970 }
19551971
0 commit comments