diff --git a/com.unity.netcode.gameobjects/Tests/Runtime/NetworkObject/NetworkObjectDontDestroyWithOwnerTests.cs b/com.unity.netcode.gameobjects/Tests/Runtime/NetworkObject/NetworkObjectDontDestroyWithOwnerTests.cs index 4300afddf6..f0993f5ee8 100644 --- a/com.unity.netcode.gameobjects/Tests/Runtime/NetworkObject/NetworkObjectDontDestroyWithOwnerTests.cs +++ b/com.unity.netcode.gameobjects/Tests/Runtime/NetworkObject/NetworkObjectDontDestroyWithOwnerTests.cs @@ -16,6 +16,7 @@ public class NetworkObjectDontDestroyWithOwnerTests : NetcodeIntegrationTest protected override int NumberOfClients => 1; protected GameObject m_PrefabToSpawn; + private GameObject m_PrefabNoObserversSpawn; public NetworkObjectDontDestroyWithOwnerTests(HostOrServer hostOrServer) : base(hostOrServer) { } @@ -23,6 +24,11 @@ protected override void OnServerAndClientsCreated() { m_PrefabToSpawn = CreateNetworkObjectPrefab("ClientOwnedObject"); m_PrefabToSpawn.GetComponent().DontDestroyWithOwner = true; + + m_PrefabNoObserversSpawn = CreateNetworkObjectPrefab("NoObserversObject"); + var prefabNoObserversNetworkObject = m_PrefabNoObserversSpawn.GetComponent(); + prefabNoObserversNetworkObject.SpawnWithObservers = false; + prefabNoObserversNetworkObject.DontDestroyWithOwner = true; } [UnityTest] @@ -51,5 +57,38 @@ public IEnumerator DontDestroyWithOwnerTest() Assert.That(networkObject.OwnerClientId == m_ServerNetworkManager.LocalClientId); } } + + /// + /// Validates that when a is spawned with no observers + /// and has the property set, that + /// upon showing the to a client and changing the owner + /// to the client that upon the client disconnecting the + /// continues to persist in the network session. + /// + /// + [UnityTest] + public IEnumerator NetworkShowThenClientDisconnects() + { + var authorityManager = m_ServerNetworkManager; + var networkObject = SpawnObject(m_PrefabNoObserversSpawn, authorityManager).GetComponent(); + var longWait = new WaitForSeconds(0.25f); + yield return longWait; + var nonAuthorityManager = m_ClientNetworkManagers[0]; + Assert.False(nonAuthorityManager.SpawnManager.SpawnedObjects.ContainsKey(networkObject.NetworkObjectId), $"[Client-{nonAuthorityManager.LocalClientId}] " + + $"Already has an instance of {networkObject.name} when it should not!"); + networkObject.NetworkShow(nonAuthorityManager.LocalClientId); + networkObject.ChangeOwnership(nonAuthorityManager.LocalClientId); + + yield return WaitForConditionOrTimeOut(() => nonAuthorityManager.SpawnManager.SpawnedObjects.ContainsKey(networkObject.NetworkObjectId) + && nonAuthorityManager.SpawnManager.SpawnedObjects[networkObject.NetworkObjectId].OwnerClientId == nonAuthorityManager.LocalClientId); + AssertOnTimeout($"[Client-{nonAuthorityManager.LocalClientId}] Failed to spawn {networkObject.name} when it was shown!"); + + yield return s_DefaultWaitForTick; + + nonAuthorityManager.Shutdown(); + + yield return longWait; + Assert.True(networkObject.IsSpawned, $"The spawned test prefab was despawned on the authority side when it shouldn't have been!"); + } } }