Skip to content

Commit 518f491

Browse files
committed
Add test for changing ownership to non-observer
1 parent 8c7ecea commit 518f491

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

com.unity.netcode.gameobjects/Tests/Runtime/DistributedAuthority/OwnershipPermissionsTests.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,46 @@ public IEnumerator ValidateOwnershipPermissionsTest()
400400
AssertOnTimeout($"[Remove][Permissions Mismatch] {daHostInstance.name}: \n {m_ErrorLog}");
401401
}
402402

403+
404+
[UnityTest]
405+
public IEnumerator ChangeOwnershipWithoutObservers()
406+
{
407+
var initialLogLevel = m_ServerNetworkManager.LogLevel;
408+
m_ServerNetworkManager.LogLevel = LogLevel.Developer;
409+
var firstInstance = SpawnObject(m_PermissionsObject, m_ServerNetworkManager).GetComponent<NetworkObject>();
410+
OwnershipPermissionsTestHelper.CurrentOwnedInstance = firstInstance;
411+
var firstInstanceHelper = firstInstance.GetComponent<OwnershipPermissionsTestHelper>();
412+
var networkObjectId = firstInstance.NetworkObjectId;
413+
m_ObjectToValidate = OwnershipPermissionsTestHelper.CurrentOwnedInstance;
414+
yield return WaitForConditionOrTimeOut(ValidateObjectSpawnedOnAllClients);
415+
AssertOnTimeout($"[Failed To Spawn] {firstInstance.name}: \n {m_ErrorLog}");
416+
417+
firstInstance.SetOwnershipStatus(NetworkObject.OwnershipStatus.Transferable, true);
418+
// Validate the base non-assigned permissions value for all instances are the same.
419+
yield return WaitForConditionOrTimeOut(ValidatePermissionsOnAllClients);
420+
AssertOnTimeout($"[Permissions Mismatch] {firstInstance.name}: \n {m_ErrorLog}");
421+
422+
var secondInstance = m_ClientNetworkManagers[0].SpawnManager.SpawnedObjects[networkObjectId];
423+
424+
// Remove the client from the observers list
425+
firstInstance.Observers.Remove(m_ClientNetworkManagers[0].LocalClientId);
426+
427+
// ChangeOwnership should fail
428+
firstInstance.ChangeOwnership(m_ClientNetworkManagers[0].LocalClientId);
429+
LogAssert.Expect(LogType.Warning, "[Session-Owner Sender=0] [Invalid Owner] Cannot send Ownership change as client-1 cannot see PermObject{2}-OnServer{0}! Use NetworkShow first.");
430+
Assert.True(firstInstance.IsOwner, $"[Ownership Check] Client-{m_ServerNetworkManager.LocalClientId} should still own this object!");
431+
432+
// Now re-add the client to the Observers list and try to change ownership
433+
firstInstance.Observers.Add(m_ClientNetworkManagers[0].LocalClientId);
434+
firstInstance.ChangeOwnership(m_ClientNetworkManagers[0].LocalClientId);
435+
436+
// Validate the second client now owns the object
437+
yield return WaitForConditionOrTimeOut(() => secondInstance.IsOwner);
438+
AssertOnTimeout($"[Acquire Ownership Failed] Client-{m_ClientNetworkManagers[0].LocalClientId} failed to get ownership!");
439+
440+
m_ServerNetworkManager.LogLevel = initialLogLevel;
441+
}
442+
403443
internal class OwnershipPermissionsTestHelper : NetworkBehaviour
404444
{
405445
public static NetworkObject CurrentOwnedInstance;

0 commit comments

Comments
 (0)