Skip to content

Commit e125938

Browse files
fix: dont destroy with owner not being honored when client exits [Test Backport of 3477] (#3478)
The v1.x branch does not have the same issue as #3477. This PR is only the backport of the test added to 3477 to assure we have the same kind of coverage in both versions. ## Changelog NA ## Testing and Documentation - Includes integration test `NetworkObjectDontDestroyWithOwnerTests.NetworkShowThenClientDisconnects`. - No documentation changes or additions were necessary. <!-- Uncomment and mark items off with a * if this PR deprecates any API: ### Deprecated API - [ ] An `[Obsolete]` attribute was added along with a `(RemovedAfter yyyy-mm-dd)` entry. - [ ] An [api updater] was added. - [ ] Deprecation of the API is explained in the CHANGELOG. - [ ] The users can understand why this API was removed and what they should use instead. --> ## Backport This is a test-only backport of #3477. <!-- If this is a backport: - Add the following to the PR title: "\[Backport\] ..." . - Link to the original PR. If this needs a backport - state this here If a backport is not needed please provide the reason why. If the "Backports" section is not present it will lead to a CI test failure. -->
1 parent 1f4a077 commit e125938

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

com.unity.netcode.gameobjects/Tests/Runtime/NetworkObject/NetworkObjectDontDestroyWithOwnerTests.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,19 @@ public class NetworkObjectDontDestroyWithOwnerTests : NetcodeIntegrationTest
1616
protected override int NumberOfClients => 1;
1717

1818
protected GameObject m_PrefabToSpawn;
19+
private GameObject m_PrefabNoObserversSpawn;
1920

2021
public NetworkObjectDontDestroyWithOwnerTests(HostOrServer hostOrServer) : base(hostOrServer) { }
2122

2223
protected override void OnServerAndClientsCreated()
2324
{
2425
m_PrefabToSpawn = CreateNetworkObjectPrefab("ClientOwnedObject");
2526
m_PrefabToSpawn.GetComponent<NetworkObject>().DontDestroyWithOwner = true;
27+
28+
m_PrefabNoObserversSpawn = CreateNetworkObjectPrefab("NoObserversObject");
29+
var prefabNoObserversNetworkObject = m_PrefabNoObserversSpawn.GetComponent<NetworkObject>();
30+
prefabNoObserversNetworkObject.SpawnWithObservers = false;
31+
prefabNoObserversNetworkObject.DontDestroyWithOwner = true;
2632
}
2733

2834
[UnityTest]
@@ -51,5 +57,38 @@ public IEnumerator DontDestroyWithOwnerTest()
5157
Assert.That(networkObject.OwnerClientId == m_ServerNetworkManager.LocalClientId);
5258
}
5359
}
60+
61+
/// <summary>
62+
/// Validates that when a <see cref="NetworkObject"/> is spawned with no observers
63+
/// and has the <see cref="NetworkObject.DontDestroyWithOwner"/> property set, that
64+
/// upon showing the <see cref="NetworkObject"/> to a client and changing the owner
65+
/// to the client that upon the client disconnecting the <see cref="NetworkObject"/>
66+
/// continues to persist in the network session.
67+
/// </summary>
68+
/// <returns><see cref="IEnumerator"/></returns>
69+
[UnityTest]
70+
public IEnumerator NetworkShowThenClientDisconnects()
71+
{
72+
var authorityManager = m_ServerNetworkManager;
73+
var networkObject = SpawnObject(m_PrefabNoObserversSpawn, authorityManager).GetComponent<NetworkObject>();
74+
var longWait = new WaitForSeconds(0.25f);
75+
yield return longWait;
76+
var nonAuthorityManager = m_ClientNetworkManagers[0];
77+
Assert.False(nonAuthorityManager.SpawnManager.SpawnedObjects.ContainsKey(networkObject.NetworkObjectId), $"[Client-{nonAuthorityManager.LocalClientId}] " +
78+
$"Already has an instance of {networkObject.name} when it should not!");
79+
networkObject.NetworkShow(nonAuthorityManager.LocalClientId);
80+
networkObject.ChangeOwnership(nonAuthorityManager.LocalClientId);
81+
82+
yield return WaitForConditionOrTimeOut(() => nonAuthorityManager.SpawnManager.SpawnedObjects.ContainsKey(networkObject.NetworkObjectId)
83+
&& nonAuthorityManager.SpawnManager.SpawnedObjects[networkObject.NetworkObjectId].OwnerClientId == nonAuthorityManager.LocalClientId);
84+
AssertOnTimeout($"[Client-{nonAuthorityManager.LocalClientId}] Failed to spawn {networkObject.name} when it was shown!");
85+
86+
yield return s_DefaultWaitForTick;
87+
88+
nonAuthorityManager.Shutdown();
89+
90+
yield return longWait;
91+
Assert.True(networkObject.IsSpawned, $"The spawned test prefab was despawned on the authority side when it shouldn't have been!");
92+
}
5493
}
5594
}

0 commit comments

Comments
 (0)