|
| 1 | +using System; |
| 2 | +using System.Collections; |
| 3 | +using NUnit.Framework; |
| 4 | +using Unity.Netcode.TestHelpers.Runtime; |
| 5 | +using Unity.Netcode.Transports.UTP; |
| 6 | +using UnityEngine; |
| 7 | +using UnityEngine.TestTools; |
| 8 | +using Object = System.Object; |
| 9 | + |
| 10 | +namespace Unity.Netcode.RuntimeTests |
| 11 | +{ |
| 12 | + [TestFixture(StartType.Server)] |
| 13 | + [TestFixture(StartType.Host)] |
| 14 | + [TestFixture(StartType.Client)] |
| 15 | + internal class StartupExceptionTests : NetcodeIntegrationTest |
| 16 | + { |
| 17 | + protected override int NumberOfClients => 0; |
| 18 | + |
| 19 | + public enum StartType |
| 20 | + { |
| 21 | + Server, |
| 22 | + Host, |
| 23 | + Client |
| 24 | + } |
| 25 | + |
| 26 | + private StartType m_StartType; |
| 27 | + public StartupExceptionTests(StartType startType) : base(startType == StartType.Server ? HostOrServer.Server : HostOrServer.Host) |
| 28 | + { |
| 29 | + m_StartType = startType; |
| 30 | + } |
| 31 | + |
| 32 | + private const string k_ExceptionText = "This is a test exception"; |
| 33 | + |
| 34 | + private void ThrowExceptionAction() |
| 35 | + { |
| 36 | + throw new Exception(k_ExceptionText); |
| 37 | + } |
| 38 | + |
| 39 | + private SessionConfig SessionConfigExceptionThrower() |
| 40 | + { |
| 41 | + throw new Exception(k_ExceptionText); |
| 42 | + } |
| 43 | + |
| 44 | + private void OnPeerConnectedException(NetworkManager networkManager, ConnectionEventData connectionEventData) |
| 45 | + { |
| 46 | + if (connectionEventData.EventType == ConnectionEvent.PeerConnected) |
| 47 | + { |
| 48 | + ThrowExceptionAction(); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + private void OnClientConnectedException(NetworkManager networkManager, ConnectionEventData connectionEventData) |
| 53 | + { |
| 54 | + if (connectionEventData.EventType == ConnectionEvent.ClientConnected) |
| 55 | + { |
| 56 | + ThrowExceptionAction(); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + |
| 61 | + [UnityTest] |
| 62 | + public IEnumerator VerifyNetworkManagerHandlesExceptionDuringStart() |
| 63 | + { |
| 64 | + var startType = m_StartType; |
| 65 | + NetworkManager toTest; |
| 66 | + if (startType == StartType.Client) |
| 67 | + { |
| 68 | + toTest = CreateNewClient(); |
| 69 | + } |
| 70 | + else |
| 71 | + { |
| 72 | + toTest = GetAuthorityNetworkManager(); |
| 73 | + yield return StopOneClient(toTest); |
| 74 | + } |
| 75 | + |
| 76 | + var transport = toTest.NetworkConfig.NetworkTransport as UnityTransport; |
| 77 | + Assert.That(transport, Is.Not.Null, "Transport should not be null"); |
| 78 | + |
| 79 | + var isListening = true; |
| 80 | + |
| 81 | + /* |
| 82 | + * Test exception being thrown during NetworkManager.Initialize() |
| 83 | + */ |
| 84 | + |
| 85 | + // It's not possible to throw an exception during server initialization |
| 86 | + if (startType != StartType.Server) |
| 87 | + { |
| 88 | + // OnSessionConfig is called within Initialize only in DAMode |
| 89 | + toTest.NetworkConfig.NetworkTopology = NetworkTopologyTypes.DistributedAuthority; |
| 90 | + |
| 91 | + toTest.OnGetSessionConfig += SessionConfigExceptionThrower; |
| 92 | + |
| 93 | + LogAssert.Expect(LogType.Exception, $"Exception: {k_ExceptionText}"); |
| 94 | + isListening = startType == StartType.Host ? toTest.StartHost() : toTest.StartClient(); |
| 95 | + |
| 96 | + Assert.That(isListening, Is.False, "Should not have started after exception during Initialize()"); |
| 97 | + Assert.That(transport.GetNetworkDriver().IsCreated, Is.False, "NetworkDriver should not be created."); |
| 98 | + |
| 99 | + toTest.OnGetSessionConfig -= SessionConfigExceptionThrower; |
| 100 | + toTest.NetworkConfig.NetworkTopology = NetworkTopologyTypes.ClientServer; |
| 101 | + } |
| 102 | + |
| 103 | + /* |
| 104 | + * Test exception being thrown after NetworkTransport.StartClient() |
| 105 | + */ |
| 106 | + |
| 107 | + toTest.OnClientStarted += ThrowExceptionAction; |
| 108 | + toTest.OnServerStarted += ThrowExceptionAction; |
| 109 | + |
| 110 | + LogAssert.Expect(LogType.Exception, $"Exception: {k_ExceptionText}"); |
| 111 | + isListening = startType switch |
| 112 | + { |
| 113 | + StartType.Server => toTest.StartServer(), |
| 114 | + StartType.Host => toTest.StartHost(), |
| 115 | + StartType.Client => toTest.StartClient(), |
| 116 | + _ => true |
| 117 | + }; |
| 118 | + |
| 119 | + Assert.That(isListening, Is.False, "Should not have started after exception during startup"); |
| 120 | + Assert.That(transport.GetNetworkDriver().IsCreated, Is.False, "NetworkDriver should not be created."); |
| 121 | + |
| 122 | + toTest.OnClientStarted -= ThrowExceptionAction; |
| 123 | + toTest.OnServerStarted -= ThrowExceptionAction; |
| 124 | + |
| 125 | + if (startType == StartType.Client) |
| 126 | + { |
| 127 | + // Start the client fully to ensure startup still works with no exceptions |
| 128 | + yield return StartClient(toTest); |
| 129 | + |
| 130 | + Assert.That(toTest.IsListening, Is.True, "Client failed to start"); |
| 131 | + Assert.That(transport.GetNetworkDriver().IsCreated, Is.True, "NetworkDriver should be created."); |
| 132 | + } |
| 133 | + else |
| 134 | + { |
| 135 | + var isHost = startType == StartType.Host; |
| 136 | + NetcodeIntegrationTestHelpers.StartServer(isHost, toTest); |
| 137 | + var hostOrServer = isHost ? "Host" : "Server"; |
| 138 | + Assert.That(toTest.IsListening, Is.True, $"{hostOrServer} failed to start"); |
| 139 | + Assert.That(transport.GetNetworkDriver().IsCreated, Is.True, "NetworkDriver should be created."); |
| 140 | + |
| 141 | + yield return CreateAndStartNewClient(); |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + } |
| 146 | +} |
0 commit comments