@@ -831,6 +831,8 @@ public IEnumerator SetUp()
831831 // In addition to setting the number of clients in the OneTimeSetup, we need to re-apply the number of clients for each unique test
832832 // in the event that a previous test stopped a client.
833833 m_NumberOfClients = NumberOfClients ;
834+ // Clear any time out state left by a previous test so it cannot fail this test's assertions
835+ s_GlobalTimeoutHelper . TimedOut = false ;
834836 VerboseDebugLog . Clear ( ) ;
835837 VerboseDebug ( $ "Entering { nameof ( SetUp ) } ") ;
836838 NetcodeLogAssert = new NetcodeLogAssert ( ) ;
@@ -1261,7 +1263,7 @@ protected void CreateAndStartNewClientWithTimeTravel()
12611263
12621264 // Wait for the new client to connect
12631265 var connected = WaitForClientsConnectedOrTimeOutWithTimeTravel ( ) ;
1264- AssertOnTimeout ( $ "{ nameof ( CreateAndStartNewClientWithTimeTravel ) } timed out waiting for all clients to be connected!\n { m_InternalErrorLog } ") ;
1266+ Assert . IsTrue ( connected , $ "{ nameof ( CreateAndStartNewClientWithTimeTravel ) } timed out waiting for all clients to be connected!\n { m_InternalErrorLog } ") ;
12651267
12661268 OnNewClientStartedAndConnected ( networkManager ) ;
12671269 if ( ! connected )
@@ -1644,9 +1646,9 @@ protected void StartServerAndClientsWithTimeTravel()
16441646 if ( ! m_BypassConnectionTimeout )
16451647 {
16461648 // Wait for all clients to connect
1647- WaitForClientsConnectedOrTimeOutWithTimeTravel ( ) ;
1649+ var connected = WaitForClientsConnectedOrTimeOutWithTimeTravel ( ) ;
16481650
1649- AssertOnTimeout ( $ "{ nameof ( StartServerAndClients ) } timed out waiting for all clients to be connected!\n { m_InternalErrorLog } ") ;
1651+ Assert . IsTrue ( connected , $ "{ nameof ( StartServerAndClients ) } timed out waiting for all clients to be connected!\n { m_InternalErrorLog } ") ;
16501652
16511653 if ( m_UseHost || authorityManager . IsHost )
16521654 {
@@ -1669,8 +1671,8 @@ protected void StartServerAndClientsWithTimeTravel()
16691671 {
16701672 foreach ( var networkManager in m_NetworkManagers )
16711673 {
1672- WaitForConditionOrTimeOutWithTimeTravel ( ( ) => AllPlayerObjectClonesSpawned ( networkManager ) ) ;
1673- AssertOnTimeout ( $ "{ nameof ( CreateAndStartNewClient ) } timed out waiting for all sessions to spawn Client-{ networkManager . LocalClientId } 's player object!") ;
1674+ var spawned = WaitForConditionOrTimeOutWithTimeTravel ( ( ) => AllPlayerObjectClonesSpawned ( networkManager ) ) ;
1675+ Assert . IsTrue ( spawned , $ "{ nameof ( CreateAndStartNewClient ) } timed out waiting for all sessions to spawn Client-{ networkManager . LocalClientId } 's player object!") ;
16741676 }
16751677 }
16761678
@@ -1771,7 +1773,7 @@ protected void ShutdownAndCleanUp()
17711773 }
17721774 catch ( Exception e )
17731775 {
1774- throw e ;
1776+ Debug . LogException ( e ) ;
17751777 }
17761778 finally
17771779 {
@@ -1909,6 +1911,9 @@ public IEnumerator TearDown()
19091911 {
19101912 ShutdownAndCleanUp ( ) ;
19111913 }
1914+
1915+ // Wait until all the scene from this test are unloaded before starting the next test.
1916+ yield return WaitForRemainingScenesUnloaded ( ) ;
19121917 }
19131918
19141919 if ( m_EnableTimeTravel )
@@ -2975,22 +2980,72 @@ protected void AssertOnTimeout(string timeOutErrorMessage, TimeoutHelper assigne
29752980 }
29762981
29772982
2983+ private const float k_SceneUnloadTimeout = 5.0f ;
2984+ private readonly List < AsyncOperation > m_PendingSceneUnloads = new List < AsyncOperation > ( ) ;
2985+
29782986 private void UnloadRemainingScenes ( )
29792987 {
29802988 // Unload any remaining scenes loaded but the test runner scene
29812989 // Note: Some tests only unload the server-side instance, and this
29822990 // just assures no currently loaded scenes will impact the next test
2991+ Scene testRunnerScene = default ;
29832992 for ( int i = 0 ; i < SceneManager . sceneCount ; i ++ )
29842993 {
29852994 var scene = SceneManager . GetSceneAt ( i ) ;
2986- if ( ! scene . IsValid ( ) || ! scene . isLoaded || scene . name . Contains ( NetcodeIntegrationTestHelpers . FirstPartOfTestRunnerSceneName ) || ! OnCanSceneCleanUpUnload ( scene ) )
2995+ if ( scene . name . Contains ( NetcodeIntegrationTestHelpers . FirstPartOfTestRunnerSceneName ) ) {
2996+ testRunnerScene = scene ;
2997+ continue ;
2998+ }
2999+ if ( ! scene . IsValid ( ) || ! scene . isLoaded || ! OnCanSceneCleanUpUnload ( scene ) )
29873000 {
29883001 continue ;
29893002 }
29903003
29913004 VerboseDebug ( $ "Unloading scene { scene . name } -{ scene . handle } ") ;
29923005 var asyncOperation = SceneManager . UnloadSceneAsync ( scene ) ;
3006+ if ( asyncOperation != null )
3007+ {
3008+ m_PendingSceneUnloads . Add ( asyncOperation ) ;
3009+ }
3010+ }
3011+
3012+ // Ensure the test runner scene is reset to be the active scene.
3013+ if ( testRunnerScene . IsValid ( ) )
3014+ {
3015+ if ( SceneManager . GetActiveScene ( ) != testRunnerScene )
3016+ {
3017+ SceneManager . SetActiveScene ( testRunnerScene ) ;
3018+ }
3019+
3020+ }
3021+ }
3022+
3023+ /// <summary>
3024+ /// Waits (in real time) for all scenes left over by the test to finish unloading.
3025+ /// Scenes that finish loading after tear down started are also unloaded.
3026+ /// </summary>
3027+ private IEnumerator WaitForRemainingScenesUnloaded ( )
3028+ {
3029+ var timeOut = Time . realtimeSinceStartup + k_SceneUnloadTimeout ;
3030+ while ( true )
3031+ {
3032+ // Pick up any scenes that finished loading after the clean up started
3033+ UnloadRemainingScenes ( ) ;
3034+
3035+ if ( m_PendingSceneUnloads . All ( c => c . isDone ) )
3036+ {
3037+ break ;
3038+ }
3039+
3040+ if ( Time . realtimeSinceStartup >= timeOut )
3041+ {
3042+ Debug . LogWarning ( $ "[{ nameof ( WaitForRemainingScenesUnloaded ) } ] Timed out waiting for scenes to unload!") ;
3043+ break ;
3044+ }
3045+ yield return null ;
29933046 }
3047+
3048+ m_PendingSceneUnloads . Clear ( ) ;
29943049 }
29953050
29963051 private StringBuilder m_WaitForLog = new StringBuilder ( ) ;
0 commit comments