|
| 1 | +using System; |
| 2 | +using System.Collections; |
| 3 | +using System.Collections.Generic; |
| 4 | +using NUnit.Framework; |
| 5 | +using Unity.Netcode.TestHelpers.Runtime; |
| 6 | +using Unity.Netcode.Transports.SinglePlayer; |
| 7 | +using UnityEngine; |
| 8 | +using UnityEngine.TestTools; |
| 9 | +using Random = UnityEngine.Random; |
| 10 | + |
| 11 | +namespace Unity.Netcode.RuntimeTests |
| 12 | +{ |
| 13 | + internal class SinglePlayerTransportTests : NetcodeIntegrationTest |
| 14 | + { |
| 15 | + protected override int NumberOfClients => 0; |
| 16 | + |
| 17 | + public struct SerializableStruct : INetworkSerializable, IEquatable<SerializableStruct> |
| 18 | + { |
| 19 | + public bool BoolValue; |
| 20 | + public ulong ULongValue; |
| 21 | + |
| 22 | + public bool Equals(SerializableStruct other) |
| 23 | + { |
| 24 | + return other.BoolValue == BoolValue && other.ULongValue == ULongValue; |
| 25 | + } |
| 26 | + |
| 27 | + public void NetworkSerialize<T>(BufferSerializer<T> serializer) where T : IReaderWriter |
| 28 | + { |
| 29 | + serializer.SerializeValue(ref BoolValue); |
| 30 | + serializer.SerializeValue(ref ULongValue); |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + public class SinglePlayerTestComponent : NetworkBehaviour |
| 35 | + { |
| 36 | + private enum SpawnStates |
| 37 | + { |
| 38 | + PreSpawn, |
| 39 | + Spawn, |
| 40 | + PostSpawn, |
| 41 | + } |
| 42 | + |
| 43 | + private enum RpcInvocations |
| 44 | + { |
| 45 | + SendToServerRpc, |
| 46 | + SendToEveryoneRpc, |
| 47 | + SendToOwnerRpc, |
| 48 | + } |
| 49 | + |
| 50 | + private Dictionary<SpawnStates, int> m_SpawnStateInvoked = new Dictionary<SpawnStates, int>(); |
| 51 | + private Dictionary<RpcInvocations, int> m_RpcInvocations = new Dictionary<RpcInvocations, int>(); |
| 52 | + private NetworkVariable<int> m_IntValue = new NetworkVariable<int>(); |
| 53 | + private NetworkVariable<SerializableStruct> m_SerializableValue = new NetworkVariable<SerializableStruct>(); |
| 54 | + |
| 55 | + |
| 56 | + private void SpawnStateInvoked(SpawnStates spawnState) |
| 57 | + { |
| 58 | + if (!m_SpawnStateInvoked.ContainsKey(spawnState)) |
| 59 | + { |
| 60 | + m_SpawnStateInvoked.Add(spawnState, 1); |
| 61 | + } |
| 62 | + else |
| 63 | + { |
| 64 | + m_SpawnStateInvoked[spawnState]++; |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + private void RpcInvoked(RpcInvocations rpcInvocation) |
| 69 | + { |
| 70 | + if (!m_RpcInvocations.ContainsKey(rpcInvocation)) |
| 71 | + { |
| 72 | + m_RpcInvocations.Add(rpcInvocation, 1); |
| 73 | + } |
| 74 | + else |
| 75 | + { |
| 76 | + m_RpcInvocations[rpcInvocation]++; |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + private void ValidateValues(int someIntValue, SerializableStruct someValues) |
| 81 | + { |
| 82 | + Assert.IsTrue(m_IntValue.Value == someIntValue); |
| 83 | + Assert.IsTrue(someValues.BoolValue == m_SerializableValue.Value.BoolValue); |
| 84 | + Assert.IsTrue(someValues.ULongValue == m_SerializableValue.Value.ULongValue); |
| 85 | + } |
| 86 | + |
| 87 | + [Rpc(SendTo.Server)] |
| 88 | + private void SendToServerRpc(int someIntValue, SerializableStruct someValues, RpcParams rpcParams = default) |
| 89 | + { |
| 90 | + ValidateValues(someIntValue, someValues); |
| 91 | + RpcInvoked(RpcInvocations.SendToServerRpc); |
| 92 | + } |
| 93 | + |
| 94 | + [Rpc(SendTo.Everyone)] |
| 95 | + private void SendToEveryoneRpc(int someIntValue, SerializableStruct someValues, RpcParams rpcParams = default) |
| 96 | + { |
| 97 | + ValidateValues(someIntValue, someValues); |
| 98 | + RpcInvoked(RpcInvocations.SendToEveryoneRpc); |
| 99 | + } |
| 100 | + |
| 101 | + [Rpc(SendTo.Owner)] |
| 102 | + private void SendToOwnerRpc(int someIntValue, SerializableStruct someValues, RpcParams rpcParams = default) |
| 103 | + { |
| 104 | + ValidateValues(someIntValue, someValues); |
| 105 | + RpcInvoked(RpcInvocations.SendToOwnerRpc); |
| 106 | + } |
| 107 | + |
| 108 | + |
| 109 | + protected override void OnNetworkPreSpawn(ref NetworkManager networkManager) |
| 110 | + { |
| 111 | + SpawnStateInvoked(SpawnStates.PreSpawn); |
| 112 | + base.OnNetworkPreSpawn(ref networkManager); |
| 113 | + } |
| 114 | + |
| 115 | + public override void OnNetworkSpawn() |
| 116 | + { |
| 117 | + SpawnStateInvoked(SpawnStates.Spawn); |
| 118 | + m_IntValue.Value = Random.Range(0, 100); |
| 119 | + m_SerializableValue.Value = new SerializableStruct() |
| 120 | + { |
| 121 | + BoolValue = Random.Range(0, 100) >= 50.0 ? true : false, |
| 122 | + ULongValue = (ulong)Random.Range(0, 100000), |
| 123 | + }; |
| 124 | + base.OnNetworkSpawn(); |
| 125 | + } |
| 126 | + |
| 127 | + protected override void OnNetworkPostSpawn() |
| 128 | + { |
| 129 | + SpawnStateInvoked(SpawnStates.PostSpawn); |
| 130 | + SendToServerRpc(m_IntValue.Value, m_SerializableValue.Value); |
| 131 | + SendToEveryoneRpc(m_IntValue.Value, m_SerializableValue.Value); |
| 132 | + SendToOwnerRpc(m_IntValue.Value, m_SerializableValue.Value); |
| 133 | + base.OnNetworkPostSpawn(); |
| 134 | + } |
| 135 | + |
| 136 | + public void ValidateStatesAndRpcInvocations() |
| 137 | + { |
| 138 | + foreach (var entry in m_SpawnStateInvoked) |
| 139 | + { |
| 140 | + Assert.True(entry.Value == 1, $"{entry.Key} failed with {entry.Value} invocations!"); |
| 141 | + } |
| 142 | + foreach (var entry in m_RpcInvocations) |
| 143 | + { |
| 144 | + Assert.True(entry.Value == 1, $"{entry.Key} failed with {entry.Value} invocations!"); |
| 145 | + } |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + private GameObject m_PrefabToSpawn; |
| 150 | + private bool m_CanStartHost; |
| 151 | + |
| 152 | + protected override IEnumerator OnSetup() |
| 153 | + { |
| 154 | + m_CanStartHost = false; |
| 155 | + return base.OnSetup(); |
| 156 | + } |
| 157 | + |
| 158 | + protected override void OnCreatePlayerPrefab() |
| 159 | + { |
| 160 | + m_PlayerPrefab.AddComponent<SinglePlayerTestComponent>(); |
| 161 | + base.OnCreatePlayerPrefab(); |
| 162 | + } |
| 163 | + |
| 164 | + protected override void OnServerAndClientsCreated() |
| 165 | + { |
| 166 | + var singlePlayerTransport = m_ServerNetworkManager.gameObject.AddComponent<SinglePlayerTransport>(); |
| 167 | + m_ServerNetworkManager.NetworkConfig.NetworkTransport = singlePlayerTransport; |
| 168 | + m_PrefabToSpawn = CreateNetworkObjectPrefab("TestObject"); |
| 169 | + m_PrefabToSpawn.AddComponent<SinglePlayerTestComponent>(); |
| 170 | + base.OnServerAndClientsCreated(); |
| 171 | + } |
| 172 | + |
| 173 | + protected override bool CanStartServerAndClients() |
| 174 | + { |
| 175 | + return m_CanStartHost; |
| 176 | + } |
| 177 | + |
| 178 | + [UnityTest] |
| 179 | + public IEnumerator StartSinglePlayerAndSpawn() |
| 180 | + { |
| 181 | + m_CanStartHost = true; |
| 182 | + |
| 183 | + yield return StartServerAndClients(); |
| 184 | + |
| 185 | + var spawnedInstance = SpawnObject(m_PrefabToSpawn, m_ServerNetworkManager).GetComponent<NetworkObject>(); |
| 186 | + var testComponent = spawnedInstance.GetComponent<SinglePlayerTestComponent>(); |
| 187 | + yield return s_DefaultWaitForTick; |
| 188 | + var playerTestComponent = m_ServerNetworkManager.LocalClient.PlayerObject.GetComponent<SinglePlayerTestComponent>(); |
| 189 | + testComponent.ValidateStatesAndRpcInvocations(); |
| 190 | + playerTestComponent.ValidateStatesAndRpcInvocations(); |
| 191 | + } |
| 192 | + |
| 193 | + [UnityTest] |
| 194 | + public IEnumerator StartSinglePlayerAsClientError() |
| 195 | + { |
| 196 | + LogAssert.Expect(LogType.Error, $"[Netcode] {SinglePlayerTransport.NotStartingAsHostErrorMessage}"); |
| 197 | + LogAssert.Expect(LogType.Error, $"[Netcode] Client is shutting down due to network transport start failure of {nameof(SinglePlayerTransport)}!"); |
| 198 | + Assert.IsFalse(m_ServerNetworkManager.StartClient()); |
| 199 | + yield return null; |
| 200 | + } |
| 201 | + |
| 202 | + [UnityTest] |
| 203 | + public IEnumerator StartSinglePlayerAsServerError() |
| 204 | + { |
| 205 | + LogAssert.Expect(LogType.Error, $"[Netcode] {SinglePlayerTransport.NotStartingAsHostErrorMessage}"); |
| 206 | + LogAssert.Expect(LogType.Error, $"[Netcode] Server is shutting down due to network transport start failure of {nameof(SinglePlayerTransport)}!"); |
| 207 | + Assert.IsFalse(m_ServerNetworkManager.StartServer()); |
| 208 | + yield return null; |
| 209 | + } |
| 210 | + } |
| 211 | +} |
0 commit comments