|
| 1 | +using System; |
| 2 | +using System.Collections; |
| 3 | +using UnityEngine; |
| 4 | +using UnityEngine.TestTools; |
| 5 | +using NUnit.Framework; |
| 6 | +using Unity.Netcode.TestHelpers.Runtime; |
| 7 | + |
| 8 | +namespace Unity.Netcode.RuntimeTests |
| 9 | +{ |
| 10 | + public class NetworkManagerEventsTests |
| 11 | + { |
| 12 | + private NetworkManager m_ClientManager; |
| 13 | + private NetworkManager m_ServerManager; |
| 14 | + |
| 15 | + [UnityTest] |
| 16 | + public IEnumerator OnServerStoppedCalledWhenServerStops() |
| 17 | + { |
| 18 | + bool callbackInvoked = false; |
| 19 | + var gameObject = new GameObject(nameof(OnServerStoppedCalledWhenServerStops)); |
| 20 | + m_ServerManager = gameObject.AddComponent<NetworkManager>(); |
| 21 | + |
| 22 | + // Set dummy transport that does nothing |
| 23 | + var transport = gameObject.AddComponent<DummyTransport>(); |
| 24 | + m_ServerManager.NetworkConfig = new NetworkConfig() { NetworkTransport = transport }; |
| 25 | + |
| 26 | + Action<bool> onServerStopped = (bool wasAlsoClient) => |
| 27 | + { |
| 28 | + callbackInvoked = true; |
| 29 | + Assert.IsFalse(wasAlsoClient); |
| 30 | + if (m_ServerManager.IsServer) |
| 31 | + { |
| 32 | + Assert.Fail("OnServerStopped called when the server is still active"); |
| 33 | + } |
| 34 | + }; |
| 35 | + |
| 36 | + // Start server to cause initialization process |
| 37 | + Assert.True(m_ServerManager.StartServer()); |
| 38 | + Assert.True(m_ServerManager.IsListening); |
| 39 | + |
| 40 | + m_ServerManager.OnServerStopped += onServerStopped; |
| 41 | + m_ServerManager.Shutdown(); |
| 42 | + UnityEngine.Object.DestroyImmediate(gameObject); |
| 43 | + |
| 44 | + yield return WaitUntilManagerShutsdown(); |
| 45 | + |
| 46 | + Assert.False(m_ServerManager.IsListening); |
| 47 | + Assert.True(callbackInvoked, "OnServerStopped wasn't invoked"); |
| 48 | + } |
| 49 | + |
| 50 | + [UnityTest] |
| 51 | + public IEnumerator OnClientStoppedCalledWhenClientStops() |
| 52 | + { |
| 53 | + yield return InitializeServerAndAClient(); |
| 54 | + |
| 55 | + bool callbackInvoked = false; |
| 56 | + Action<bool> onClientStopped = (bool wasAlsoServer) => |
| 57 | + { |
| 58 | + callbackInvoked = true; |
| 59 | + Assert.IsFalse(wasAlsoServer); |
| 60 | + if (m_ClientManager.IsClient) |
| 61 | + { |
| 62 | + Assert.Fail("onClientStopped called when the client is still active"); |
| 63 | + } |
| 64 | + }; |
| 65 | + |
| 66 | + m_ClientManager.OnClientStopped += onClientStopped; |
| 67 | + m_ClientManager.Shutdown(); |
| 68 | + yield return WaitUntilManagerShutsdown(); |
| 69 | + |
| 70 | + Assert.True(callbackInvoked, "OnClientStopped wasn't invoked"); |
| 71 | + } |
| 72 | + |
| 73 | + [UnityTest] |
| 74 | + public IEnumerator OnClientAndServerStoppedCalledWhenHostStops() |
| 75 | + { |
| 76 | + var gameObject = new GameObject(nameof(OnClientAndServerStoppedCalledWhenHostStops)); |
| 77 | + m_ServerManager = gameObject.AddComponent<NetworkManager>(); |
| 78 | + |
| 79 | + // Set dummy transport that does nothing |
| 80 | + var transport = gameObject.AddComponent<DummyTransport>(); |
| 81 | + m_ServerManager.NetworkConfig = new NetworkConfig() { NetworkTransport = transport }; |
| 82 | + |
| 83 | + int callbacksInvoked = 0; |
| 84 | + Action<bool> onClientStopped = (bool wasAlsoServer) => |
| 85 | + { |
| 86 | + callbacksInvoked++; |
| 87 | + Assert.IsTrue(wasAlsoServer); |
| 88 | + if (m_ServerManager.IsClient) |
| 89 | + { |
| 90 | + Assert.Fail("onClientStopped called when the client is still active"); |
| 91 | + } |
| 92 | + }; |
| 93 | + |
| 94 | + Action<bool> onServerStopped = (bool wasAlsoClient) => |
| 95 | + { |
| 96 | + callbacksInvoked++; |
| 97 | + Assert.IsTrue(wasAlsoClient); |
| 98 | + if (m_ServerManager.IsServer) |
| 99 | + { |
| 100 | + Assert.Fail("OnServerStopped called when the server is still active"); |
| 101 | + } |
| 102 | + }; |
| 103 | + |
| 104 | + // Start server to cause initialization process |
| 105 | + Assert.True(m_ServerManager.StartHost()); |
| 106 | + Assert.True(m_ServerManager.IsListening); |
| 107 | + |
| 108 | + m_ServerManager.OnServerStopped += onServerStopped; |
| 109 | + m_ServerManager.OnClientStopped += onClientStopped; |
| 110 | + m_ServerManager.Shutdown(); |
| 111 | + UnityEngine.Object.DestroyImmediate(gameObject); |
| 112 | + |
| 113 | + yield return WaitUntilManagerShutsdown(); |
| 114 | + |
| 115 | + Assert.False(m_ServerManager.IsListening); |
| 116 | + Assert.AreEqual(2, callbacksInvoked, "either OnServerStopped or OnClientStopped wasn't invoked"); |
| 117 | + } |
| 118 | + |
| 119 | + [UnityTest] |
| 120 | + public IEnumerator OnServerStartedCalledWhenServerStarts() |
| 121 | + { |
| 122 | + var gameObject = new GameObject(nameof(OnServerStartedCalledWhenServerStarts)); |
| 123 | + m_ServerManager = gameObject.AddComponent<NetworkManager>(); |
| 124 | + |
| 125 | + // Set dummy transport that does nothing |
| 126 | + var transport = gameObject.AddComponent<DummyTransport>(); |
| 127 | + m_ServerManager.NetworkConfig = new NetworkConfig() { NetworkTransport = transport }; |
| 128 | + |
| 129 | + bool callbackInvoked = false; |
| 130 | + Action onServerStarted = () => |
| 131 | + { |
| 132 | + callbackInvoked = true; |
| 133 | + if (!m_ServerManager.IsServer) |
| 134 | + { |
| 135 | + Assert.Fail("OnServerStarted called when the server is not active yet"); |
| 136 | + } |
| 137 | + }; |
| 138 | + |
| 139 | + // Start server to cause initialization process |
| 140 | + m_ServerManager.OnServerStarted += onServerStarted; |
| 141 | + |
| 142 | + Assert.True(m_ServerManager.StartServer()); |
| 143 | + Assert.True(m_ServerManager.IsListening); |
| 144 | + |
| 145 | + yield return WaitUntilServerBufferingIsReady(); |
| 146 | + |
| 147 | + Assert.True(callbackInvoked, "OnServerStarted wasn't invoked"); |
| 148 | + } |
| 149 | + |
| 150 | + [UnityTest] |
| 151 | + public IEnumerator OnClientStartedCalledWhenClientStarts() |
| 152 | + { |
| 153 | + bool callbackInvoked = false; |
| 154 | + Action onClientStarted = () => |
| 155 | + { |
| 156 | + callbackInvoked = true; |
| 157 | + if (!m_ClientManager.IsClient) |
| 158 | + { |
| 159 | + Assert.Fail("onClientStarted called when the client is not active yet"); |
| 160 | + } |
| 161 | + }; |
| 162 | + |
| 163 | + yield return InitializeServerAndAClient(onClientStarted); |
| 164 | + |
| 165 | + Assert.True(callbackInvoked, "OnClientStarted wasn't invoked"); |
| 166 | + } |
| 167 | + |
| 168 | + [UnityTest] |
| 169 | + public IEnumerator OnClientAndServerStartedCalledWhenHostStarts() |
| 170 | + { |
| 171 | + var gameObject = new GameObject(nameof(OnClientAndServerStartedCalledWhenHostStarts)); |
| 172 | + m_ServerManager = gameObject.AddComponent<NetworkManager>(); |
| 173 | + |
| 174 | + // Set dummy transport that does nothing |
| 175 | + var transport = gameObject.AddComponent<DummyTransport>(); |
| 176 | + m_ServerManager.NetworkConfig = new NetworkConfig() { NetworkTransport = transport }; |
| 177 | + |
| 178 | + int callbacksInvoked = 0; |
| 179 | + Action onClientStarted = () => |
| 180 | + { |
| 181 | + callbacksInvoked++; |
| 182 | + if (!m_ServerManager.IsClient) |
| 183 | + { |
| 184 | + Assert.Fail("OnClientStarted called when the client is not active yet"); |
| 185 | + } |
| 186 | + }; |
| 187 | + |
| 188 | + Action onServerStarted = () => |
| 189 | + { |
| 190 | + callbacksInvoked++; |
| 191 | + if (!m_ServerManager.IsServer) |
| 192 | + { |
| 193 | + Assert.Fail("OnServerStarted called when the server is not active yet"); |
| 194 | + } |
| 195 | + }; |
| 196 | + |
| 197 | + m_ServerManager.OnServerStarted += onServerStarted; |
| 198 | + m_ServerManager.OnClientStarted += onClientStarted; |
| 199 | + |
| 200 | + // Start server to cause initialization process |
| 201 | + Assert.True(m_ServerManager.StartHost()); |
| 202 | + Assert.True(m_ServerManager.IsListening); |
| 203 | + |
| 204 | + yield return WaitUntilServerBufferingIsReady(); |
| 205 | + Assert.AreEqual(2, callbacksInvoked, "either OnServerStarted or OnClientStarted wasn't invoked"); |
| 206 | + } |
| 207 | + |
| 208 | + private IEnumerator WaitUntilManagerShutsdown() |
| 209 | + { |
| 210 | + /* Need two updates to actually shut down. First one to see the transport failing, which |
| 211 | + marks the NetworkManager as shutting down. Second one where actual shutdown occurs. */ |
| 212 | + yield return null; |
| 213 | + yield return null; |
| 214 | + } |
| 215 | + |
| 216 | + private IEnumerator InitializeServerAndAClient(Action onClientStarted = null) |
| 217 | + { |
| 218 | + // Create multiple NetworkManager instances |
| 219 | + if (!NetcodeIntegrationTestHelpers.Create(1, out m_ServerManager, out NetworkManager[] clients, 30)) |
| 220 | + { |
| 221 | + Debug.LogError("Failed to create instances"); |
| 222 | + Assert.Fail("Failed to create instances"); |
| 223 | + } |
| 224 | + |
| 225 | + // passing no clients on purpose to start them manually later |
| 226 | + NetcodeIntegrationTestHelpers.Start(false, m_ServerManager, new NetworkManager[] { }); |
| 227 | + |
| 228 | + yield return WaitUntilServerBufferingIsReady(); |
| 229 | + m_ClientManager = clients[0]; |
| 230 | + |
| 231 | + if (onClientStarted != null) |
| 232 | + { |
| 233 | + m_ClientManager.OnClientStarted += onClientStarted; |
| 234 | + } |
| 235 | + |
| 236 | + Assert.True(m_ClientManager.StartClient()); |
| 237 | + NetcodeIntegrationTestHelpers.RegisterHandlers(clients[0]); |
| 238 | + // Wait for connection on client side |
| 239 | + yield return NetcodeIntegrationTestHelpers.WaitForClientsConnected(clients); |
| 240 | + } |
| 241 | + |
| 242 | + private IEnumerator WaitUntilServerBufferingIsReady() |
| 243 | + { |
| 244 | + /* wait until at least more than 2 server ticks have passed |
| 245 | + Note: Waiting for more than 2 ticks on the server is due |
| 246 | + to the time system applying buffering to the received time |
| 247 | + in NetworkTimeSystem.Sync */ |
| 248 | + yield return new WaitUntil(() => m_ServerManager.NetworkTickSystem.ServerTime.Tick > 2); |
| 249 | + } |
| 250 | + |
| 251 | + [UnityTearDown] |
| 252 | + public virtual IEnumerator Teardown() |
| 253 | + { |
| 254 | + NetcodeIntegrationTestHelpers.Destroy(); |
| 255 | + yield return null; |
| 256 | + } |
| 257 | + } |
| 258 | +} |
0 commit comments