|
9 | 9 |
|
10 | 10 | namespace Unity.Netcode.RuntimeTests
|
11 | 11 | {
|
| 12 | + [TestFixture(SetDestroyGameObject.DestroyGameObject)] |
| 13 | + [TestFixture(SetDestroyGameObject.DontDestroyGameObject)] |
12 | 14 | internal class DeferredDespawningTests : IntegrationTestWithApproximation
|
13 | 15 | {
|
14 | 16 | private const int k_DaisyChainedCount = 5;
|
15 | 17 | protected override int NumberOfClients => 2;
|
16 | 18 | private List<GameObject> m_DaisyChainedDespawnObjects = new List<GameObject>();
|
17 | 19 | private List<ulong> m_HasReachedEnd = new List<ulong>();
|
18 | 20 |
|
19 |
| - public DeferredDespawningTests() : base(HostOrServer.DAHost) |
| 21 | + public enum SetDestroyGameObject |
20 | 22 | {
|
| 23 | + DestroyGameObject, |
| 24 | + DontDestroyGameObject, |
| 25 | + } |
| 26 | + private bool m_DestroyGameObject; |
| 27 | + |
| 28 | + public DeferredDespawningTests(SetDestroyGameObject destroyGameObject) : base(HostOrServer.DAHost) |
| 29 | + { |
| 30 | + m_DestroyGameObject = destroyGameObject == SetDestroyGameObject.DestroyGameObject; |
21 | 31 | }
|
22 | 32 |
|
23 | 33 | protected override void OnServerAndClientsCreated()
|
@@ -45,12 +55,28 @@ protected override void OnServerAndClientsCreated()
|
45 | 55 | [UnityTest]
|
46 | 56 | public IEnumerator DeferredDespawning()
|
47 | 57 | {
|
| 58 | + // Setup for test |
| 59 | + DeferredDespawnDaisyChained.DestroyGameObject = m_DestroyGameObject; |
48 | 60 | DeferredDespawnDaisyChained.EnableVerbose = m_EnableVerboseDebug;
|
| 61 | + DeferredDespawnDaisyChained.ClientRelativeInstances = new Dictionary<ulong, Dictionary<ulong, DeferredDespawnDaisyChained>>(); |
| 62 | + |
| 63 | + // Spawn the initial object |
49 | 64 | var rootInstance = SpawnObject(m_DaisyChainedDespawnObjects[0], m_ServerNetworkManager);
|
50 | 65 | DeferredDespawnDaisyChained.ReachedLastChainInstance = ReachedLastChainObject;
|
| 66 | + |
| 67 | + // Wait for the chain of objects to spawn and despawn |
51 | 68 | var timeoutHelper = new TimeoutHelper(300);
|
52 | 69 | yield return WaitForConditionOrTimeOut(HaveAllClientsReachedEndOfChain, timeoutHelper);
|
53 | 70 | AssertOnTimeout($"Timed out waiting for all children to reach the end of their chained deferred despawns!", timeoutHelper);
|
| 71 | + |
| 72 | + if (m_DestroyGameObject) |
| 73 | + { |
| 74 | + Assert.IsTrue(rootInstance == null); // Assert.IsNull doesn't work here |
| 75 | + } |
| 76 | + else |
| 77 | + { |
| 78 | + Assert.IsTrue(rootInstance != null); // Assert.IsNotNull doesn't work here |
| 79 | + } |
54 | 80 | }
|
55 | 81 |
|
56 | 82 | private bool HaveAllClientsReachedEndOfChain()
|
@@ -88,9 +114,10 @@ private void ReachedLastChainObject(ulong clientId)
|
88 | 114 | internal class DeferredDespawnDaisyChained : NetworkBehaviour
|
89 | 115 | {
|
90 | 116 | public static bool EnableVerbose;
|
| 117 | + public static bool DestroyGameObject; |
91 | 118 | public static Action<ulong> ReachedLastChainInstance;
|
92 | 119 | private const int k_StartingDeferTick = 4;
|
93 |
| - public static Dictionary<ulong, Dictionary<ulong, DeferredDespawnDaisyChained>> ClientRelativeInstances = new Dictionary<ulong, Dictionary<ulong, DeferredDespawnDaisyChained>>(); |
| 120 | + public static Dictionary<ulong, Dictionary<ulong, DeferredDespawnDaisyChained>> ClientRelativeInstances; |
94 | 121 | public bool IsRoot;
|
95 | 122 | public GameObject PrefabToSpawnWhenDespawned;
|
96 | 123 | public bool WasContactedByPeviousChainMember { get; private set; }
|
@@ -182,7 +209,7 @@ private void InvokeDespawn()
|
182 | 209 | {
|
183 | 210 | FailTest($"[{nameof(InvokeDespawn)}] Client is not the authority but this was invoked (integration test logic issue)!");
|
184 | 211 | }
|
185 |
| - NetworkObject.DeferDespawn(DeferDespawnTick); |
| 212 | + NetworkObject.DeferDespawn(DeferDespawnTick, DestroyGameObject); |
186 | 213 | }
|
187 | 214 |
|
188 | 215 | public override void OnDeferringDespawn(int despawnTick)
|
@@ -241,7 +268,7 @@ private void Update()
|
241 | 268 | continue;
|
242 | 269 | }
|
243 | 270 |
|
244 |
| - // This should happen shortly afte the instances spawns (based on the deferred despawn count) |
| 271 | + // This should happen shortly after the instances spawn (based on the deferred despawn count) |
245 | 272 | if (!IsRoot && !ClientRelativeInstances[clientId][NetworkObjectId].WasContactedByPeviousChainMember)
|
246 | 273 | {
|
247 | 274 | // exit early if the non-authority instance has not been contacted yet
|
|
0 commit comments