|
| 1 | +using System.Collections; |
| 2 | +using NUnit.Framework; |
| 3 | +using Unity.Netcode.TestHelpers.Runtime; |
| 4 | +using UnityEngine; |
| 5 | +using UnityEngine.TestTools; |
| 6 | +using UnityEngine.TestTools.Constraints; |
| 7 | +using Is = NUnit.Framework.Is; |
| 8 | + |
| 9 | +namespace Unity.Netcode.RuntimeTests |
| 10 | +{ |
| 11 | + internal class AllocationTestBehaviour : NetworkBehaviour |
| 12 | + { |
| 13 | + internal int RpcReceivedCount; |
| 14 | + |
| 15 | + public NetworkVariable<int> TestVariable = new(); |
| 16 | + |
| 17 | + [Rpc(SendTo.NotMe)] |
| 18 | + public void NotMeRpc() |
| 19 | + { |
| 20 | + RpcReceivedCount++; |
| 21 | + } |
| 22 | + } |
| 23 | + |
| 24 | + [TestFixture(HostOrServer.Host)] |
| 25 | + [TestFixture(HostOrServer.DAHost)] |
| 26 | + internal class MessageReceiveAllocationTests : NetcodeIntegrationTest |
| 27 | + { |
| 28 | + protected override int NumberOfClients => 1; |
| 29 | + |
| 30 | + public MessageReceiveAllocationTests(HostOrServer hostOrServer) : base(hostOrServer) {} |
| 31 | + |
| 32 | + private GameObject m_Prefab; |
| 33 | + |
| 34 | + protected override void OnServerAndClientsCreated() |
| 35 | + { |
| 36 | + m_Prefab = CreateNetworkObjectPrefab(nameof(AllocationTestBehaviour)); |
| 37 | + m_Prefab.AddComponent<AllocationTestBehaviour>(); |
| 38 | + base.OnServerAndClientsCreated(); |
| 39 | + } |
| 40 | + |
| 41 | + [UnityTest] |
| 42 | + public IEnumerator NoAllocationsOnMessageReceive() |
| 43 | + { |
| 44 | + var authority = GetAuthorityNetworkManager(); |
| 45 | + var nonAuthority = GetNonAuthorityNetworkManager(); |
| 46 | + |
| 47 | + var authorityInstance = SpawnObject(m_Prefab, authority); |
| 48 | + var authorityComponent = authorityInstance.GetComponent<AllocationTestBehaviour>(); |
| 49 | + yield return WaitForSpawnedOnAllOrTimeOut(authorityInstance); |
| 50 | + AssertOnTimeout("Timed out waiting for objects to spawn"); |
| 51 | + |
| 52 | + var nonAuthorityComponent = nonAuthority.SpawnManager.SpawnedObjects[authorityComponent.NetworkObjectId].GetComponent<AllocationTestBehaviour>(); |
| 53 | + |
| 54 | + /* |
| 55 | + * RpcMessage |
| 56 | + */ |
| 57 | + var rpcCatcher = new MessageCatcher<RpcMessage>(nonAuthority); |
| 58 | + nonAuthority.ConnectionManager.MessageManager.Hook(rpcCatcher); |
| 59 | + |
| 60 | + // Send the same message twice: the first is replayed as a warm-up. |
| 61 | + // The second will follow the identical code path and is checked for allocations. |
| 62 | + authorityComponent.NotMeRpc(); |
| 63 | + authorityComponent.NotMeRpc(); |
| 64 | + yield return WaitForConditionOrTimeOut(() => rpcCatcher.CaughtMessageCount == 2); |
| 65 | + AssertOnTimeout($"Timed out waiting to catch all expected {nameof(RpcMessage)} messages. Expected: 2, Actual: {rpcCatcher.CaughtMessageCount}"); |
| 66 | + |
| 67 | + // Unhook first so the replayed messages are handled instead of being caught again |
| 68 | + nonAuthority.ConnectionManager.MessageManager.Unhook(rpcCatcher); |
| 69 | + |
| 70 | + rpcCatcher.HandleCaughtMessage(0); |
| 71 | + Assert.AreEqual(1, nonAuthorityComponent.RpcReceivedCount); |
| 72 | + |
| 73 | + Assert.That(() => |
| 74 | + { |
| 75 | + rpcCatcher.HandleCaughtMessage(1); |
| 76 | + }, Is.Not.AllocatingGCMemory()); |
| 77 | + Assert.AreEqual(2, nonAuthorityComponent.RpcReceivedCount); |
| 78 | + |
| 79 | + /* |
| 80 | + * NetworkVariableDeltaMessage |
| 81 | + */ |
| 82 | + var deltaCatcher = new MessageCatcher<NetworkVariableDeltaMessage>(nonAuthority); |
| 83 | + nonAuthority.ConnectionManager.MessageManager.Hook(deltaCatcher); |
| 84 | + |
| 85 | + authorityComponent.TestVariable.Value = 1; |
| 86 | + // Wait for the first change to be received client-side before sending the second change |
| 87 | + yield return WaitForConditionOrTimeOut(() => deltaCatcher.CaughtMessageCount == 1); |
| 88 | + authorityComponent.TestVariable.Value = 2; |
| 89 | + yield return WaitForConditionOrTimeOut(() => deltaCatcher.CaughtMessageCount == 2); |
| 90 | + AssertOnTimeout($"Timed out waiting to catch all expected {nameof(NetworkVariableDeltaMessage)} messages. Expected: 2, Actual: {deltaCatcher.CaughtMessageCount}"); |
| 91 | + |
| 92 | + // Unhook first so the replayed messages are handled instead of being caught again |
| 93 | + nonAuthority.ConnectionManager.MessageManager.Unhook(deltaCatcher); |
| 94 | + |
| 95 | + deltaCatcher.HandleCaughtMessage(0); |
| 96 | + Assert.AreEqual(1, nonAuthorityComponent.TestVariable.Value); |
| 97 | + |
| 98 | + Assert.That(() => |
| 99 | + { |
| 100 | + deltaCatcher.HandleCaughtMessage(1); |
| 101 | + }, Is.Not.AllocatingGCMemory()); |
| 102 | + Assert.AreEqual(2, nonAuthorityComponent.TestVariable.Value); |
| 103 | + } |
| 104 | + |
| 105 | + } |
| 106 | +} |
0 commit comments