|
| 1 | +using System.Collections; |
| 2 | +using System.Collections.Generic; |
| 3 | +using NUnit.Framework; |
| 4 | +using Unity.Netcode.TestHelpers.Runtime; |
| 5 | +using UnityEngine.TestTools; |
| 6 | + |
| 7 | +namespace Unity.Netcode.RuntimeTests |
| 8 | +{ |
| 9 | + [TestFixture(SceneManagementState.SceneManagementEnabled, NetworkTopologyTypes.DistributedAuthority)] |
| 10 | + [TestFixture(SceneManagementState.SceneManagementDisabled, NetworkTopologyTypes.DistributedAuthority)] |
| 11 | + [TestFixture(SceneManagementState.SceneManagementEnabled, NetworkTopologyTypes.ClientServer)] |
| 12 | + [TestFixture(SceneManagementState.SceneManagementDisabled, NetworkTopologyTypes.ClientServer)] |
| 13 | + internal class ClientConnectionTests : IntegrationTestWithApproximation |
| 14 | + { |
| 15 | + protected override int NumberOfClients => 3; |
| 16 | + private readonly bool m_SceneManagementEnabled; |
| 17 | + private HashSet<ulong> m_ServerCallbackCalled = new HashSet<ulong>(); |
| 18 | + private HashSet<ulong> m_ClientCallbackCalled = new HashSet<ulong>(); |
| 19 | + |
| 20 | + public ClientConnectionTests(SceneManagementState sceneManagementState, NetworkTopologyTypes networkTopologyType) : base(networkTopologyType) |
| 21 | + { |
| 22 | + m_SceneManagementEnabled = sceneManagementState == SceneManagementState.SceneManagementEnabled; |
| 23 | + } |
| 24 | + |
| 25 | + protected override void OnServerAndClientsCreated() |
| 26 | + { |
| 27 | + m_ServerNetworkManager.NetworkConfig.EnableSceneManagement = m_SceneManagementEnabled; |
| 28 | + m_ServerNetworkManager.OnClientConnectedCallback += Server_OnClientConnectedCallback; |
| 29 | + |
| 30 | + foreach (var client in m_ClientNetworkManagers) |
| 31 | + { |
| 32 | + client.NetworkConfig.EnableSceneManagement = m_SceneManagementEnabled; |
| 33 | + client.OnClientConnectedCallback += Client_OnClientConnectedCallback; |
| 34 | + } |
| 35 | + |
| 36 | + base.OnServerAndClientsCreated(); |
| 37 | + } |
| 38 | + |
| 39 | + [UnityTest] |
| 40 | + public IEnumerator VerifyOnClientConnectedCallback() |
| 41 | + { |
| 42 | + yield return WaitForConditionOrTimeOut(AllCallbacksCalled); |
| 43 | + AssertOnTimeout("Timed out waiting for all clients to be connected!"); |
| 44 | + |
| 45 | + // The client callbacks should have been called once per client (called once on self) |
| 46 | + Assert.True(m_ClientCallbackCalled.Count == NumberOfClients); |
| 47 | + |
| 48 | + // The server callback should be called for self, and then once per client |
| 49 | + Assert.True(m_ServerCallbackCalled.Count == 1 + NumberOfClients); |
| 50 | + } |
| 51 | + |
| 52 | + private void Server_OnClientConnectedCallback(ulong clientId) |
| 53 | + { |
| 54 | + if (!m_ServerCallbackCalled.Add(clientId)) |
| 55 | + { |
| 56 | + Assert.Fail($"Client already connected: {clientId}"); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + private void Client_OnClientConnectedCallback(ulong clientId) |
| 61 | + { |
| 62 | + if (!m_ClientCallbackCalled.Add(clientId)) |
| 63 | + { |
| 64 | + Assert.Fail($"Client already connected: {clientId}"); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + private bool AllCallbacksCalled() |
| 69 | + { |
| 70 | + foreach (var client in m_ClientNetworkManagers) |
| 71 | + { |
| 72 | + if (!m_ClientCallbackCalled.Contains(client.LocalClientId) || !m_ServerCallbackCalled.Contains(client.LocalClientId)) |
| 73 | + { |
| 74 | + return false; |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + return m_ServerCallbackCalled.Contains(m_ServerNetworkManager.LocalClientId); |
| 79 | + } |
| 80 | + } |
| 81 | +} |
| 82 | + |
0 commit comments