-
Notifications
You must be signed in to change notification settings - Fork 450
fix: inscene networkobjects get destroyed upon client networkmanager shutting down without connecting #1809
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
0xFA11
merged 10 commits into
develop
from
fix/nccbug-119-inscene-networkobjects-get-destroyed-upon-networkmanager-shuttingdown-without-connecting
Mar 18, 2022
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
32132b8
fix
NoelStephensUnity 743aa5c
test
NoelStephensUnity 94071a6
update
NoelStephensUnity 4f64084
style
NoelStephensUnity a1a1703
test refactor
NoelStephensUnity 7650520
Merge branch 'develop' into fix/nccbug-119-inscene-networkobjects-get…
NoelStephensUnity d5cea7c
Merge branch 'develop' into fix/nccbug-119-inscene-networkobjects-get…
NoelStephensUnity 65d47a2
Merge branch 'develop' into fix/nccbug-119-inscene-networkobjects-get…
0xFA11 669bfea
minor tweak
0xFA11 e4114a0
Merge develop into fix/nccbug-119-inscene-networkobjects-get-destroye…
netcode-ci-service 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
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
138 changes: 138 additions & 0 deletions
138
testproject/Assets/Tests/Runtime/SceneObjectsNotDestroyedOnShutdownTest.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,138 @@ | ||
|
||
using System; | ||
using System.Collections; | ||
using System.Linq; | ||
using NUnit.Framework; | ||
using UnityEngine; | ||
using UnityEngine.TestTools; | ||
using UnityEngine.SceneManagement; | ||
using Unity.Netcode; | ||
using Unity.Netcode.TestHelpers.Runtime; | ||
using Object = UnityEngine.Object; | ||
|
||
namespace TestProject.RuntimeTests | ||
{ | ||
public class SceneObjectsNotDestroyedOnShutdownTest | ||
{ | ||
private const string k_TestScene = "InSceneNetworkObject"; | ||
private const string k_SceneObjectName = "InSceneObject"; | ||
private Scene m_TestScene; | ||
private NetworkManager m_ClientNetworkManager; | ||
private GameObject m_NetworkManagerGameObject; | ||
private WaitForSeconds m_DefaultWaitForTick = new WaitForSeconds(1.0f / 30); | ||
|
||
[SetUp] | ||
public void Setup() | ||
{ | ||
m_NetworkManagerGameObject = new GameObject(); | ||
m_ClientNetworkManager = m_NetworkManagerGameObject.AddComponent<NetworkManager>(); | ||
m_ClientNetworkManager.NetworkConfig = new NetworkConfig(); | ||
m_ClientNetworkManager.NetworkConfig.NetworkTransport = m_NetworkManagerGameObject.AddComponent<BlankTestingTransport>(); | ||
SceneManager.sceneLoaded += SceneManager_sceneLoaded; | ||
SceneManager.LoadSceneAsync(k_TestScene, LoadSceneMode.Additive); | ||
} | ||
|
||
private void SceneManager_sceneLoaded(Scene scene, LoadSceneMode loadSceneMode) | ||
{ | ||
if (scene.name == k_TestScene) | ||
{ | ||
m_TestScene = scene; | ||
} | ||
} | ||
|
||
[UnityTest] | ||
public IEnumerator SceneObjectsNotDestroyedOnShutdown() | ||
{ | ||
var timeoutHelper = new TimeoutHelper(2); | ||
|
||
// Wait for the scene with the in-scene placed NetworkObject to be loaded. | ||
yield return NetcodeIntegrationTest.WaitForConditionOrTimeOut(() => m_TestScene.IsValid() && m_TestScene.isLoaded, timeoutHelper); | ||
Assert.False(timeoutHelper.TimedOut, "Timed out waiting for scene to load!"); | ||
|
||
var loadedInSceneObject = Object.FindObjectsOfType<NetworkObject>().Where((c) => c.name == k_SceneObjectName).FirstOrDefault(); | ||
|
||
Assert.IsNotNull(loadedInSceneObject, $"Failed to find {k_SceneObjectName} before starting client!"); | ||
|
||
// Only start the client | ||
m_ClientNetworkManager.StartClient(); | ||
|
||
// Wait for a tick | ||
yield return m_DefaultWaitForTick; | ||
|
||
// Shutdown the client | ||
m_ClientNetworkManager.Shutdown(); | ||
|
||
// Wait for a tick | ||
yield return m_DefaultWaitForTick; | ||
|
||
// Find the same object | ||
loadedInSceneObject = Object.FindObjectsOfType<NetworkObject>().Where((c) => c.name == k_SceneObjectName).FirstOrDefault(); | ||
|
||
// Verify it still exists | ||
Assert.IsNotNull(loadedInSceneObject, $"Failed to find {k_SceneObjectName} after starting client!"); | ||
} | ||
|
||
[TearDown] | ||
public void TearDown() | ||
{ | ||
SceneManager.sceneLoaded -= SceneManager_sceneLoaded; | ||
|
||
if (m_TestScene.IsValid() && m_TestScene.isLoaded) | ||
{ | ||
SceneManager.UnloadSceneAsync(m_TestScene); | ||
} | ||
|
||
if (m_NetworkManagerGameObject != null) | ||
{ | ||
Object.DestroyImmediate(m_NetworkManagerGameObject); | ||
} | ||
} | ||
|
||
internal class BlankTestingTransport : TestingNetworkTransport | ||
{ | ||
public override ulong ServerClientId { get; } = 0; | ||
public override void Send(ulong clientId, ArraySegment<byte> payload, NetworkDelivery networkDelivery) | ||
{ | ||
} | ||
|
||
public override NetworkEvent PollEvent(out ulong clientId, out ArraySegment<byte> payload, out float receiveTime) | ||
{ | ||
clientId = 0; | ||
payload = new ArraySegment<byte>(); | ||
receiveTime = 0; | ||
return NetworkEvent.Nothing; | ||
} | ||
|
||
public override bool StartClient() | ||
{ | ||
return true; | ||
} | ||
|
||
public override bool StartServer() | ||
{ | ||
return true; | ||
} | ||
|
||
public override void DisconnectRemoteClient(ulong clientId) | ||
{ | ||
} | ||
|
||
public override void DisconnectLocalClient() | ||
{ | ||
} | ||
|
||
public override ulong GetCurrentRtt(ulong clientId) | ||
{ | ||
return 0; | ||
} | ||
|
||
public override void Shutdown() | ||
{ | ||
} | ||
|
||
public override void Initialize(NetworkManager networkManager = null) | ||
{ | ||
} | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
testproject/Assets/Tests/Runtime/SceneObjectsNotDestroyedOnShutdownTest.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.
Worth a comment for clarity of
"IsSceneObject will be null if I've never connected, or will be true (and stay true) if I've connected and am (and always will be) a scene object."