|
| 1 | +#if UNIFIED_NETCODE |
| 2 | +using System.Collections; |
| 3 | +using System.Collections.Generic; |
| 4 | +using NUnit.Framework; |
| 5 | +using Unity.Netcode.Components; |
| 6 | +using Unity.Netcode.TestHelpers.Runtime; |
| 7 | +using UnityEngine.TestTools; |
| 8 | + |
| 9 | +namespace Unity.Netcode.RuntimeTests |
| 10 | +{ |
| 11 | + /// <summary> |
| 12 | + /// Validates <see cref="NetworkManager.OnNetCodeConnect"/> and <see cref="NetworkManager.OnNetCodeDisconnect"/> fire |
| 13 | + /// for each connection, and that a client only goes in game once NGO has synchronized it. |
| 14 | + /// </summary> |
| 15 | + [TestFixture(HostOrServer.UnifiedHost)] |
| 16 | + [TestFixture(HostOrServer.UnifiedServer)] |
| 17 | + internal class HybridConnectionEventsTests : NetcodeIntegrationTest |
| 18 | + { |
| 19 | + protected override int NumberOfClients => 1; |
| 20 | + |
| 21 | + private readonly List<int> m_ServerConnects = new List<int>(); |
| 22 | + private readonly List<int> m_ServerDisconnects = new List<int>(); |
| 23 | + private int m_ClientConnects; |
| 24 | + private bool m_ClientSynchronizedOnConnect; |
| 25 | + |
| 26 | + public HybridConnectionEventsTests(HostOrServer hostOrServer) : base(hostOrServer) { } |
| 27 | + |
| 28 | + protected override bool UseUnifiedTests() |
| 29 | + { |
| 30 | + return true; |
| 31 | + } |
| 32 | + |
| 33 | + protected override void OnServerAndClientsCreated() |
| 34 | + { |
| 35 | + // No world is created for a session without ghost prefabs. |
| 36 | + CreateHybridPrefab("ConnectionEventsPrefab"); |
| 37 | + m_ServerConnects.Clear(); |
| 38 | + m_ServerDisconnects.Clear(); |
| 39 | + m_ClientConnects = 0; |
| 40 | + m_ClientSynchronizedOnConnect = false; |
| 41 | + NetworkManager.OnNetCodeConnect += OnNetCodeConnect; |
| 42 | + NetworkManager.OnNetCodeDisconnect += OnNetCodeDisconnect; |
| 43 | + base.OnServerAndClientsCreated(); |
| 44 | + } |
| 45 | + |
| 46 | + protected override IEnumerator OnTearDown() |
| 47 | + { |
| 48 | + NetworkManager.OnNetCodeConnect -= OnNetCodeConnect; |
| 49 | + NetworkManager.OnNetCodeDisconnect -= OnNetCodeDisconnect; |
| 50 | + return base.OnTearDown(); |
| 51 | + } |
| 52 | + |
| 53 | + private void OnNetCodeConnect(NetcodeConnection connection) |
| 54 | + { |
| 55 | + if (connection.IsServer) |
| 56 | + { |
| 57 | + m_ServerConnects.Add(connection.NetworkId); |
| 58 | + return; |
| 59 | + } |
| 60 | + m_ClientConnects++; |
| 61 | + m_ClientSynchronizedOnConnect = m_ClientNetworkManagers[0].IsConnectedClient; |
| 62 | + } |
| 63 | + |
| 64 | + private void OnNetCodeDisconnect(NetcodeConnection connection) |
| 65 | + { |
| 66 | + if (connection.IsServer) |
| 67 | + { |
| 68 | + m_ServerDisconnects.Add(connection.NetworkId); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + [UnityTest] |
| 73 | + public IEnumerator ConnectFiresOnceSynchronized() |
| 74 | + { |
| 75 | + var clientNetworkId = m_ClientNetworkManagers[0].NetcodeWorld.LocalConnection.NetworkId.Value; |
| 76 | + yield return WaitForConditionOrTimeOut(() => m_ClientConnects > 0 && m_ServerConnects.Contains(clientNetworkId)); |
| 77 | + AssertOnTimeout($"Connect events missing! Client connects: {m_ClientConnects}, server saw the client: {m_ServerConnects.Contains(clientNetworkId)}"); |
| 78 | + |
| 79 | + Assert.AreEqual(1, m_ClientConnects, "The client connection should only be reported once."); |
| 80 | + Assert.IsTrue(m_ClientSynchronizedOnConnect, "The client went in game prior to NGO synchronizing it."); |
| 81 | + } |
| 82 | + |
| 83 | + [UnityTest] |
| 84 | + public IEnumerator ServerReportsClientDisconnect() |
| 85 | + { |
| 86 | + var clientNetworkId = m_ClientNetworkManagers[0].NetcodeWorld.LocalConnection.NetworkId.Value; |
| 87 | + yield return WaitForConditionOrTimeOut(() => m_ServerConnects.Contains(clientNetworkId)); |
| 88 | + AssertOnTimeout("The server never reported the client connection!"); |
| 89 | + |
| 90 | + yield return StopOneClient(m_ClientNetworkManagers[0]); |
| 91 | + yield return WaitForConditionOrTimeOut(() => m_ServerDisconnects.Contains(clientNetworkId)); |
| 92 | + AssertOnTimeout("The server never reported the client disconnect!"); |
| 93 | + Assert.IsTrue(m_ServerNetworkManager.IsListening, "The disconnect should be reported while the session is still running."); |
| 94 | + } |
| 95 | + } |
| 96 | +} |
| 97 | +#endif |
0 commit comments