Skip to content

Commit 8ece71d

Browse files
test - fix
Fixing the race condition test instability when running OwnershipPermissionsTests against CMB service until the order in which a message is received will reflect the order in which it is sent relative to messages received prior to and after the given message.
1 parent 66fdf96 commit 8ece71d

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

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

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ private bool WaitForOneClientToBeApproved(OwnershipPermissionsTestHelper[] clien
6767
{
6868
approvedClients++;
6969
}
70-
else if (helper.OwnershipRequestResponseStatus == NetworkObject.OwnershipRequestResponseStatus.RequestInProgress)
70+
else if (helper.OwnershipRequestResponseStatus == NetworkObject.OwnershipRequestResponseStatus.RequestInProgress || helper.OwnershipRequestResponseStatus == NetworkObject.OwnershipRequestResponseStatus.Denied)
7171
{
7272
requestInProgressClients++;
7373
}
@@ -279,8 +279,16 @@ public IEnumerator ValidateOwnershipPermissionsTest()
279279
var fourthInstance = fourthClient.SpawnManager.SpawnedObjects[networkObjectId];
280280
var fourthInstanceHelper = fourthInstance.GetComponent<OwnershipPermissionsTestHelper>();
281281

282+
// Mock race condition scenario where the second instance request arrives before the rest of the requests.
283+
// This could be on the same frame all requests are received or where they stagger over several frames.
284+
// Until we resolve the CMB service issue where the order in which messages are received does not always
285+
// reflect the order in which they are forwarded to their destination (relative to other messages received from
286+
// other clients).
287+
firstInstanceHelper.OnlyAllowTargetClientId = true;
288+
firstInstanceHelper.ClientToAllowOwnership = secondClient.LocalClientId;
289+
282290
// Send out a request from three clients at the same time
283-
// The first one sent (and received for this test) gets ownership
291+
// The first one received gets ownership
284292
requestStatus = secondInstance.RequestOwnership();
285293
Assert.True(requestStatus == NetworkObject.OwnershipRequestStatus.RequestSent, $"Client-{secondClient.LocalClientId} was unable to send a request for ownership because: {requestStatus}!");
286294
requestStatus = thirdInstance.RequestOwnership();
@@ -291,7 +299,7 @@ public IEnumerator ValidateOwnershipPermissionsTest()
291299
// The 2nd and 3rd client should be denied and the 4th client should be approved
292300
yield return WaitForConditionOrTimeOut(() => WaitForOneClientToBeApproved(new[] { secondInstanceHelper, thirdInstanceHelper, fourthInstanceHelper }));
293301
AssertOnTimeout("[Targeted Owner] A client received an incorrect response. " +
294-
$"Expected one client to have {NetworkObject.OwnershipRequestResponseStatus.Approved} and the others to have {NetworkObject.OwnershipRequestResponseStatus.RequestInProgress}!."
302+
$"Expected one client to have {NetworkObject.OwnershipRequestResponseStatus.Approved} and the others to have {NetworkObject.OwnershipRequestResponseStatus.RequestInProgress} or {NetworkObject.OwnershipRequestResponseStatus.Denied}!."
295303
+ $"\n Client-{fourthClient.LocalClientId}: has {fourthInstanceHelper.OwnershipRequestResponseStatus}!"
296304
+ $"\n Client-{thirdClient.LocalClientId}: has {thirdInstanceHelper.OwnershipRequestResponseStatus}!"
297305
+ $"\n Client-{secondClient.LocalClientId}: has {secondInstanceHelper.OwnershipRequestResponseStatus}!");
@@ -305,6 +313,9 @@ public IEnumerator ValidateOwnershipPermissionsTest()
305313
yield return WaitForConditionOrTimeOut(ValidatePermissionsOnAllClients);
306314
AssertOnTimeout($"[Multiple request race condition][Permissions Mismatch] {secondInstance.name}");
307315

316+
// Reset this value once this part of the test is complete
317+
firstInstanceHelper.OnlyAllowTargetClientId = false;
318+
308319
///////////////////////////////////////////////
309320
// Test for targeted ownership request:
310321
///////////////////////////////////////////////

0 commit comments

Comments
 (0)