-
Notifications
You must be signed in to change notification settings - Fork 459
fix: MTTB-1273 Inconsistent SceneName in SceneEvent #3458
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
Merged
NoelStephensUnity
merged 13 commits into
develop-2.0.0
from
fix/mttb-1273/inconsistent-scene-name-in-scene-event
May 29, 2025
Merged
Changes from 8 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
571d565
fix: MTTB-1273 Inconsistent SceneName in SceneEvent
EmandM b78c6a6
update CHANGELOG.md
EmandM 30d7fb3
Merge branch 'develop-2.0.0' of https://github.com/Unity-Technologies…
EmandM 730e160
Get more information on test failures
EmandM 0895f55
Add code docs
EmandM 68c3f6c
Don't break conditional tests
EmandM b42f324
Clear the error builder
EmandM f041460
Fix SceneEventCallbackNotifications
EmandM d3cea31
Merge branch 'develop-2.0.0' into fix/mttb-1273/inconsistent-scene-na…
NoelStephensUnity a30d9be
Fix integration test helpers
EmandM f1b1c81
Fix the tests
EmandM cc9db63
Merge branch 'develop-2.0.0' of https://github.com/Unity-Technologies…
EmandM 713b078
Merge branch 'develop-2.0.0' into fix/mttb-1273/inconsistent-scene-na…
NoelStephensUnity File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
272 changes: 91 additions & 181 deletions
272
com.unity.netcode.gameobjects/Runtime/SceneManagement/NetworkSceneManager.cs
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
264 changes: 264 additions & 0 deletions
264
testproject/Assets/Tests/Runtime/NetworkSceneManager/OnSceneEventCallbackTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,264 @@ | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using NUnit.Framework; | ||
using Unity.Netcode; | ||
using Unity.Netcode.TestHelpers.Runtime; | ||
using UnityEngine.SceneManagement; | ||
using UnityEngine.TestTools; | ||
|
||
namespace TestProject.RuntimeTests | ||
{ | ||
[TestFixture(HostOrServer.Host)] | ||
[TestFixture(HostOrServer.Server)] | ||
[TestFixture(HostOrServer.DAHost)] | ||
public class OnSceneEventCallbackTests : NetcodeIntegrationTest | ||
{ | ||
protected override int NumberOfClients => 1; | ||
|
||
private const string k_SceneToLoad = "EmptyScene"; | ||
private const string k_PathToLoad = "Assets/Scenes/EmptyScene.unity"; | ||
|
||
public OnSceneEventCallbackTests(HostOrServer hostOrServer) : base(hostOrServer) | ||
{ | ||
|
||
} | ||
|
||
private struct ExpectedEvent | ||
{ | ||
public SceneEvent SceneEvent; | ||
public string SceneName; | ||
public string ScenePath; | ||
} | ||
|
||
private readonly Queue<ExpectedEvent> m_ExpectedEventQueue = new(); | ||
|
||
private static int s_NumEventsProcessed; | ||
private void OnSceneEvent(SceneEvent sceneEvent) | ||
{ | ||
VerboseDebug($"OnSceneEvent! Type: {sceneEvent.SceneEventType}."); | ||
if (m_ExpectedEventQueue.Count > 0) | ||
{ | ||
var expectedEvent = m_ExpectedEventQueue.Dequeue(); | ||
|
||
ValidateEventsAreEqual(expectedEvent.SceneEvent, sceneEvent); | ||
|
||
// Only LoadComplete events have an attached scene | ||
if (sceneEvent.SceneEventType == SceneEventType.LoadComplete) | ||
{ | ||
ValidateReceivedScene(expectedEvent, sceneEvent.Scene); | ||
} | ||
} | ||
else | ||
{ | ||
Assert.Fail($"Received unexpected event at index {s_NumEventsProcessed}: {sceneEvent.SceneEventType}"); | ||
} | ||
s_NumEventsProcessed++; | ||
} | ||
|
||
public enum ClientType | ||
{ | ||
Authority, | ||
NonAuthority, | ||
} | ||
|
||
public enum Action | ||
{ | ||
Load, | ||
Unload, | ||
} | ||
|
||
[UnityTest] | ||
public IEnumerator LoadAndUnloadCallbacks([Values] ClientType clientType, [Values] Action action) | ||
{ | ||
yield return RunSceneEventCallbackTest(clientType, action, k_SceneToLoad); | ||
} | ||
|
||
[UnityTest] | ||
public IEnumerator LoadSceneFromPath([Values] ClientType clientType) | ||
{ | ||
yield return RunSceneEventCallbackTest(clientType, Action.Load, k_PathToLoad); | ||
} | ||
|
||
private IEnumerator RunSceneEventCallbackTest(ClientType clientType, Action action, string loadCall) | ||
{ | ||
if (m_UseCmbService) | ||
{ | ||
yield return s_DefaultWaitForTick; | ||
} | ||
|
||
var authority = GetAuthorityNetworkManager(); | ||
var nonAuthority = GetNonAuthorityNetworkManager(); | ||
var managerToTest = clientType == ClientType.Authority ? authority : nonAuthority; | ||
|
||
|
||
var expectedCompletedClients = new List<ulong> { nonAuthority.LocalClientId }; | ||
// the authority ID is not inside ClientsThatCompleted when running as a server | ||
if (m_UseHost) | ||
{ | ||
expectedCompletedClients.Insert(0, authority.LocalClientId); | ||
} | ||
|
||
Scene loadedScene = default; | ||
if (action == Action.Unload) | ||
{ | ||
// Load the scene initially | ||
authority.SceneManager.LoadScene(k_SceneToLoad, LoadSceneMode.Additive); | ||
|
||
yield return WaitForConditionOrAssert(ValidateSceneIsLoaded, $"[Setup] Timed out waiting for client to load the scene {k_SceneToLoad}!"); | ||
|
||
// Wait for any pending messages to be processed | ||
yield return null; | ||
|
||
// Get a reference to the scene to test | ||
loadedScene = SceneManager.GetSceneByName(k_SceneToLoad); | ||
} | ||
|
||
s_NumEventsProcessed = 0; | ||
m_ExpectedEventQueue.Clear(); | ||
m_ExpectedEventQueue.Enqueue(new ExpectedEvent() | ||
{ | ||
SceneEvent = new SceneEvent() | ||
{ | ||
SceneEventType = action == Action.Load ? SceneEventType.Load : SceneEventType.Unload, | ||
LoadSceneMode = LoadSceneMode.Additive, | ||
SceneName = k_SceneToLoad, | ||
ScenePath = k_PathToLoad, | ||
ClientId = managerToTest.LocalClientId, | ||
}, | ||
}); | ||
m_ExpectedEventQueue.Enqueue(new ExpectedEvent() | ||
{ | ||
SceneEvent = new SceneEvent() | ||
{ | ||
SceneEventType = action == Action.Load ? SceneEventType.LoadComplete : SceneEventType.UnloadComplete, | ||
LoadSceneMode = LoadSceneMode.Additive, | ||
SceneName = k_SceneToLoad, | ||
ScenePath = k_PathToLoad, | ||
ClientId = managerToTest.LocalClientId, | ||
}, | ||
SceneName = action == Action.Load ? k_SceneToLoad : null, | ||
ScenePath = action == Action.Load ? k_PathToLoad : null | ||
}); | ||
|
||
if (clientType == ClientType.Authority) | ||
{ | ||
m_ExpectedEventQueue.Enqueue(new ExpectedEvent() | ||
{ | ||
SceneEvent = new SceneEvent() | ||
{ | ||
SceneEventType = action == Action.Load ? SceneEventType.LoadComplete : SceneEventType.UnloadComplete, | ||
LoadSceneMode = LoadSceneMode.Additive, | ||
SceneName = k_SceneToLoad, | ||
ScenePath = k_PathToLoad, | ||
ClientId = nonAuthority.LocalClientId, | ||
} | ||
}); | ||
} | ||
|
||
m_ExpectedEventQueue.Enqueue(new ExpectedEvent() | ||
{ | ||
SceneEvent = new SceneEvent() | ||
{ | ||
SceneEventType = action == Action.Load ? SceneEventType.LoadEventCompleted : SceneEventType.UnloadEventCompleted, | ||
LoadSceneMode = LoadSceneMode.Additive, | ||
SceneName = k_SceneToLoad, | ||
ScenePath = k_PathToLoad, | ||
ClientId = authority.LocalClientId, | ||
ClientsThatCompleted = expectedCompletedClients, | ||
ClientsThatTimedOut = new List<ulong>() | ||
} | ||
}); | ||
|
||
////////////////////////////////////////// | ||
// Testing event notifications | ||
managerToTest.SceneManager.OnSceneEvent += OnSceneEvent; | ||
|
||
if (action == Action.Load) | ||
{ | ||
Assert.That(authority.SceneManager.LoadScene(loadCall, LoadSceneMode.Additive) == SceneEventProgressStatus.Started); | ||
|
||
yield return WaitForConditionOrAssert(ValidateSceneIsLoaded, $"[Test] Timed out waiting for client to load the scene {k_SceneToLoad}!"); | ||
} | ||
else | ||
{ | ||
Assert.That(loadedScene.name, Is.EqualTo(k_SceneToLoad), "scene was not loaded!"); | ||
Assert.That(authority.SceneManager.UnloadScene(loadedScene) == SceneEventProgressStatus.Started); | ||
|
||
yield return WaitForConditionOrAssert(ValidateSceneIsUnloaded, $"[Test] Timed out waiting for client to unload the scene {k_SceneToLoad}!"); | ||
} | ||
|
||
// Wait for all messages to process | ||
yield return null; | ||
|
||
if (m_ExpectedEventQueue.Count > 0) | ||
{ | ||
Assert.Fail($"Failed to invoke all expected OnSceneEvent callbacks. {m_ExpectedEventQueue.Count} callbacks missing. First missing event is {m_ExpectedEventQueue.Dequeue().SceneEvent.SceneEventType}"); | ||
} | ||
|
||
managerToTest.SceneManager.OnSceneEvent -= OnSceneEvent; | ||
} | ||
|
||
private bool ValidateSceneIsLoaded(StringBuilder errorBuilder) | ||
{ | ||
foreach (var manager in m_NetworkManagers) | ||
{ | ||
// default will have isLoaded as false so we can get the scene or default and test on isLoaded | ||
var loadedScene = manager.SceneManager.ScenesLoaded.Values.FirstOrDefault(scene => scene.name == k_SceneToLoad); | ||
if (!loadedScene.isLoaded) | ||
{ | ||
errorBuilder.AppendLine($"[ValidateIsLoaded] Scene {loadedScene.name} exists but is not loaded!"); | ||
return false; | ||
} | ||
|
||
if (manager.SceneManager.SceneEventProgressTracking.Count > 0) | ||
{ | ||
errorBuilder.AppendLine($"[ValidateIsLoaded] NetworkManager {manager.name} still has progress tracking events."); | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
private bool ValidateSceneIsUnloaded() | ||
{ | ||
foreach (var manager in m_NetworkManagers) | ||
{ | ||
if (manager.SceneManager.ScenesLoaded.Values.Any(scene => scene.name == k_SceneToLoad)) | ||
{ | ||
return false; | ||
} | ||
|
||
if (manager.SceneManager.SceneEventProgressTracking.Count > 0) | ||
{ | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
private static void ValidateEventsAreEqual(SceneEvent expectedEvent, SceneEvent sceneEvent) | ||
{ | ||
AssertField(expectedEvent.SceneEventType, sceneEvent.SceneEventType, nameof(sceneEvent.SceneEventType), sceneEvent.SceneEventType); | ||
AssertField(expectedEvent.LoadSceneMode, sceneEvent.LoadSceneMode, nameof(sceneEvent.LoadSceneMode), sceneEvent.SceneEventType); | ||
AssertField(expectedEvent.SceneName, sceneEvent.SceneName, nameof(sceneEvent.SceneName), sceneEvent.SceneEventType); | ||
AssertField(expectedEvent.ClientId, sceneEvent.ClientId, nameof(sceneEvent.ClientId), sceneEvent.SceneEventType); | ||
AssertField(expectedEvent.ClientsThatCompleted, sceneEvent.ClientsThatCompleted, nameof(sceneEvent.SceneEventType), sceneEvent.SceneEventType); | ||
AssertField(expectedEvent.ClientsThatTimedOut, sceneEvent.ClientsThatTimedOut, nameof(sceneEvent.ClientsThatTimedOut), sceneEvent.SceneEventType); | ||
} | ||
|
||
// The LoadCompleted event includes the scene being loaded | ||
private static void ValidateReceivedScene(ExpectedEvent expectedEvent, Scene scene) | ||
{ | ||
AssertField(expectedEvent.SceneName, scene.name, "Scene.name", SceneEventType.LoadComplete); | ||
AssertField(expectedEvent.ScenePath, scene.path, "Scene.path", SceneEventType.LoadComplete); | ||
} | ||
|
||
private static void AssertField<T>(T expected, T actual, string fieldName, SceneEventType type) | ||
{ | ||
Assert.AreEqual(expected, actual, $"Failed on event {s_NumEventsProcessed} - {type}: Expected {fieldName} to be {expected}. Found {actual}"); | ||
} | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
testproject/Assets/Tests/Runtime/NetworkSceneManager/OnSceneEventCallbackTests.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Nice clean up and fix.
👍