Repository navigation
fix: [Backport] Ensure all loaded scenes are registered on session owner promotion #4217
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop-2.0.0
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,130 @@ | ||
| using System.Collections; | ||
| using System.Text; | ||
| using NUnit.Framework; | ||
| using Unity.Netcode; | ||
| using Unity.Netcode.TestHelpers.Runtime; | ||
| using UnityEngine.SceneManagement; | ||
| using UnityEngine.TestTools; | ||
|
|
||
| namespace TestProject.RuntimeTests | ||
| { | ||
| /// <summary> | ||
| /// Validates that a client promoted to session owner registers the scenes it loaded outside of | ||
| /// <see cref="NetworkSceneManager"/> so it can synchronize newly joining clients. | ||
| /// Runs against the DAHost by default and against the live CMB service when USE_CMB_SERVICE is set. | ||
| /// </summary> | ||
| internal class SessionOwnerPromotionSceneTests : NetcodeIntegrationTest | ||
| { | ||
| private const string k_LocallyLoadedScene = "EmptyScene"; | ||
| protected override int NumberOfClients => 2; | ||
|
|
||
| private Scene m_OriginalActiveScene; | ||
| private Scene m_LocallyLoadedScene; | ||
|
|
||
| public SessionOwnerPromotionSceneTests() : base(HostOrServer.DAHost) { } | ||
|
|
||
| protected override void OnOneTimeSetup() | ||
| { | ||
| m_OriginalActiveScene = SceneManager.GetActiveScene(); | ||
| base.OnOneTimeSetup(); | ||
| } | ||
|
|
||
| protected override IEnumerator OnTearDown() | ||
| { | ||
| SceneManager.SetActiveScene(m_OriginalActiveScene); | ||
| if (m_LocallyLoadedScene.IsValid() && m_LocallyLoadedScene.isLoaded) | ||
| { | ||
| var scene = m_LocallyLoadedScene; | ||
| SceneManager.UnloadSceneAsync(scene); | ||
| yield return WaitForConditionOrTimeOut(() => !scene.isLoaded); | ||
| } | ||
| yield return base.OnTearDown(); | ||
| } | ||
|
|
||
| private NetworkManager m_InitialSessionOwner; | ||
|
|
||
| /// <summary> | ||
| /// Avoids <see cref="NetcodeIntegrationTest.GetAuthorityNetworkManager"/> since it fails the test when | ||
| /// invoked before a new session owner has been promoted. | ||
| /// </summary> | ||
| private bool IsNewSessionOwnerPromoted(StringBuilder errorLog) | ||
| { | ||
| // Have to do our own loop looking for a session owner as GetAuthorityNetworkManager will fail the test if no session owner is found. | ||
| // It is expected that no session owner will be found for a frame or two during session owner promotion. | ||
| NetworkManager sessionOwner = null; | ||
| foreach (var networkManager in m_NetworkManagers) | ||
| { | ||
| if (networkManager.LocalClient.IsSessionOwner) | ||
| { | ||
| sessionOwner = networkManager; | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| if (sessionOwner == null || sessionOwner == m_InitialSessionOwner) | ||
| { | ||
| errorLog.AppendLine("No new session owner has been promoted."); | ||
| return false; | ||
| } | ||
|
|
||
| var allClientsSynchronized = true; | ||
| foreach (var networkManager in m_NetworkManagers) | ||
| { | ||
| if (networkManager.CurrentSessionOwner != sessionOwner.LocalClientId) | ||
| { | ||
| errorLog.AppendLine($"[Client-{networkManager.LocalClientId}] Incorrect {nameof(NetworkManager.CurrentSessionOwner)}! Expected: {sessionOwner.LocalClientId}. Has: {networkManager.CurrentSessionOwner}."); | ||
| allClientsSynchronized = false; | ||
| } | ||
| } | ||
| return allClientsSynchronized; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Loads and activates a scene without using <see cref="NetworkSceneManager"/>, promotes a new session owner, | ||
| /// and then verifies the promoted session owner can synchronize late joining clients. | ||
| /// </summary> | ||
| [UnityTest] | ||
| public IEnumerator PromotedSessionOwnerSynchronizesLocallyLoadedActiveScene() | ||
| { | ||
| // Load and activate a scene outside of NetworkSceneManager after the session has started, | ||
| // so no NetworkManager instance has a network scene handle registered for it. | ||
| var loadOperation = SceneManager.LoadSceneAsync(k_LocallyLoadedScene, LoadSceneMode.Additive); | ||
| Assert.NotNull(loadOperation, "Failed to load test scene!"); | ||
|
|
||
| yield return WaitForConditionOrTimeOut(() => loadOperation.isDone); | ||
| AssertOnTimeout($"Timed out waiting for {k_LocallyLoadedScene} to load!"); | ||
|
|
||
| // Set the newly loaded scene as the active scene | ||
| // Now all clients have this unregistered scene as their active scene. | ||
| m_LocallyLoadedScene = SceneManager.GetSceneByName(k_LocallyLoadedScene); | ||
| SceneManager.SetActiveScene(m_LocallyLoadedScene); | ||
|
|
||
| // Now all clients have this unregistered scene as their active scene. | ||
| m_InitialSessionOwner = GetAuthorityNetworkManager(); | ||
|
|
||
| // Sanity check: Double-check the newly loaded scene is unregistered on all clients | ||
| foreach (var networkManager in m_NetworkManagers) | ||
| { | ||
| var isSceneRegistered = networkManager.SceneManager.ClientSceneHandleToServerSceneHandle.ContainsKey(m_LocallyLoadedScene.handle); | ||
| Assert.That(isSceneRegistered, Is.False, $"[Client-{networkManager.LocalClientId}] Scene loaded by the UnityEngine SceneManager should not have been registered!"); | ||
| } | ||
|
|
||
| if (m_UseCmbService) | ||
| { | ||
| // the CMB service promotes a client when the current session owner disconnects | ||
| yield return StopOneClient(m_InitialSessionOwner); | ||
| } | ||
| else | ||
| { | ||
| // The DAHost can manually promote another client as the session owner. | ||
| m_InitialSessionOwner.PromoteSessionOwner(GetNonAuthorityNetworkManager().LocalClientId); | ||
| } | ||
|
|
||
| yield return WaitForConditionOrTimeOut(IsNewSessionOwnerPromoted); | ||
| AssertOnTimeout("Session owner promotion failed!"); | ||
|
|
||
| // Connect a late-joining client to verify the new session owner handles the previously unregistered scene. | ||
| yield return CreateAndStartNewClient(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This regression test can pass even if the promoted owner never registers 🤖 Helpful? 👍/👎 |
||
| } | ||
| } | ||
| } | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Late joining can still fail on a promoted CMB owner when a locally loaded additive scene's handle collides with an existing shared handle. With
ServerSceneHandleToClientSceneHandle[100] = 200, another local scene can have handle100, because Unity scene handles are client-local.InitializeScenesLoadedpasses100as both handles, soUpdateServerClientSceneHandlereturns before adding the missing reverse mapping.AddSceneToClientSynchronizationthen indexes that missing entry and throwsKeyNotFoundException. Reuse existing client-to-server mappings during initialization. Assign an unused shared handle when registering a new scene.🤖 Helpful? 👍/👎