-
Notifications
You must be signed in to change notification settings - Fork 462
fix: network manager does not clean up if exception is thrown while starting #3864
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
Open
NoelStephensUnity
wants to merge
11
commits into
develop-2.0.0
from
fix/3552-networkmanager-fails-to-cleanup-if-exception-thrown-during-start
+255
−33
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
ff3075a
fix
NoelStephensUnity b3ed5c4
Move all connection tests into the Connection folder
EmandM a819265
Catch any exceptions in PrefabHandler.Instantiate
EmandM 1e7a70c
Add tests for exceptions thrown during startup
EmandM 1051ea1
Merge branch 'develop-2.0.0' into fix/3552-networkmanager-fails-to-cl...
EmandM 811c22c
Update CHANGELOG
EmandM 51dd9be
Fix code formatting and remove unused code
EmandM ff2f1a8
Move more connection tests
EmandM 0890689
Fix tests
EmandM ef44dc2
Update CHANGELOG
EmandM 6ad52fc
Rename test file
EmandM 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
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
130 changes: 130 additions & 0 deletions
com.unity.netcode.gameobjects/Tests/Runtime/NetworkManagerStartExceptionTests.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,130 @@ | ||
| using System; | ||
| using System.Collections; | ||
| using NUnit.Framework; | ||
| using Unity.Netcode.TestHelpers.Runtime; | ||
| using Unity.Netcode.Transports.UTP; | ||
| using UnityEngine; | ||
| using UnityEngine.TestTools; | ||
|
|
||
| namespace Unity.Netcode.RuntimeTests | ||
| { | ||
| [TestFixture(StartType.Server)] | ||
| [TestFixture(StartType.Host)] | ||
| [TestFixture(StartType.Client)] | ||
| internal class NetworkManagerStartExceptionTests : NetcodeIntegrationTest | ||
| { | ||
| protected override int NumberOfClients => 0; | ||
|
|
||
| public enum StartType | ||
| { | ||
| Server, | ||
| Host, | ||
| Client | ||
| } | ||
|
|
||
| private StartType m_StartType; | ||
| public NetworkManagerStartExceptionTests(StartType startType) : base(startType == StartType.Server ? HostOrServer.Server : HostOrServer.Host) | ||
| { | ||
| m_StartType = startType; | ||
| } | ||
|
|
||
| private const string k_ExceptionText = "This is a test exception"; | ||
|
|
||
| private void ThrowExceptionAction() | ||
| { | ||
| throw new Exception(k_ExceptionText); | ||
| } | ||
|
|
||
| private SessionConfig SessionConfigExceptionThrower() | ||
| { | ||
| throw new Exception(k_ExceptionText); | ||
| } | ||
|
|
||
| [UnityTest] | ||
| public IEnumerator VerifyNetworkManagerHandlesExceptionDuringStart() | ||
| { | ||
| var startType = m_StartType; | ||
| NetworkManager toTest; | ||
| if (startType == StartType.Client) | ||
| { | ||
| toTest = CreateNewClient(); | ||
| } | ||
| else | ||
| { | ||
| toTest = GetAuthorityNetworkManager(); | ||
| yield return StopOneClient(toTest); | ||
| } | ||
|
|
||
| var transport = toTest.NetworkConfig.NetworkTransport as UnityTransport; | ||
| Assert.That(transport, Is.Not.Null, "Transport should not be null"); | ||
|
|
||
| var isListening = true; | ||
|
|
||
| /* | ||
| * Test exception being thrown during NetworkManager.Initialize() | ||
| */ | ||
|
|
||
| // It's not possible to throw an exception during server initialization | ||
| if (startType != StartType.Server) | ||
| { | ||
| // OnSessionConfig is called within Initialize only in DAMode | ||
| toTest.NetworkConfig.NetworkTopology = NetworkTopologyTypes.DistributedAuthority; | ||
|
|
||
| toTest.OnGetSessionConfig += SessionConfigExceptionThrower; | ||
|
|
||
| LogAssert.Expect(LogType.Exception, $"Exception: {k_ExceptionText}"); | ||
| isListening = startType == StartType.Host ? toTest.StartHost() : toTest.StartClient(); | ||
|
|
||
| Assert.That(isListening, Is.False, "Should not have started after exception during Initialize()"); | ||
| Assert.That(transport.GetNetworkDriver().IsCreated, Is.False, "NetworkDriver should not be created."); | ||
|
|
||
| toTest.OnGetSessionConfig -= SessionConfigExceptionThrower; | ||
| toTest.NetworkConfig.NetworkTopology = NetworkTopologyTypes.ClientServer; | ||
| } | ||
|
|
||
| /* | ||
| * Test exception being thrown after NetworkTransport.StartClient() | ||
| */ | ||
|
|
||
| toTest.OnClientStarted += ThrowExceptionAction; | ||
| toTest.OnServerStarted += ThrowExceptionAction; | ||
|
|
||
| LogAssert.Expect(LogType.Exception, $"Exception: {k_ExceptionText}"); | ||
| isListening = startType switch | ||
| { | ||
| StartType.Server => toTest.StartServer(), | ||
| StartType.Host => toTest.StartHost(), | ||
| StartType.Client => toTest.StartClient(), | ||
| _ => true | ||
| }; | ||
|
|
||
| Assert.That(isListening, Is.False, "Should not have started after exception during startup"); | ||
| Assert.That(transport.GetNetworkDriver().IsCreated, Is.False, "NetworkDriver should not be created."); | ||
| Assert.False(toTest.IsServer, "IsServer should be false when NetworkManager failed to start"); | ||
| Assert.False(toTest.IsClient, "IsClient should be false when NetworkManager failed to start"); | ||
|
|
||
| toTest.OnClientStarted -= ThrowExceptionAction; | ||
| toTest.OnServerStarted -= ThrowExceptionAction; | ||
|
|
||
| if (startType == StartType.Client) | ||
| { | ||
| // Start the client fully to ensure startup still works with no exceptions | ||
| yield return StartClient(toTest); | ||
|
|
||
| Assert.That(toTest.IsListening, Is.True, "Client failed to start"); | ||
| Assert.That(transport.GetNetworkDriver().IsCreated, Is.True, "NetworkDriver should be created."); | ||
| } | ||
| else | ||
| { | ||
| var isHost = startType == StartType.Host; | ||
| NetcodeIntegrationTestHelpers.StartServer(isHost, toTest); | ||
| var hostOrServer = isHost ? "Host" : "Server"; | ||
| Assert.That(toTest.IsListening, Is.True, $"{hostOrServer} failed to start"); | ||
| Assert.That(transport.GetNetworkDriver().IsCreated, Is.True, "NetworkDriver should be created."); | ||
|
|
||
| yield return CreateAndStartNewClient(); | ||
| } | ||
| } | ||
|
|
||
| } | ||
| } |
3 changes: 3 additions & 0 deletions
com.unity.netcode.gameobjects/Tests/Runtime/NetworkManagerStartExceptionTests.cs.meta
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
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.