diff --git a/com.unity.netcode.gameobjects/Tests/Runtime/Prefabs/NetworkPrefabHandlerSynchronizationTests.cs b/com.unity.netcode.gameobjects/Tests/Runtime/Prefabs/NetworkPrefabHandlerSynchronizationTests.cs index 0c5895b612..bf341a45fb 100644 --- a/com.unity.netcode.gameobjects/Tests/Runtime/Prefabs/NetworkPrefabHandlerSynchronizationTests.cs +++ b/com.unity.netcode.gameobjects/Tests/Runtime/Prefabs/NetworkPrefabHandlerSynchronizationTests.cs @@ -19,6 +19,28 @@ public NetworkPrefabHandlerSynchronizationTests(HostOrServer hostOrServer) : bas private GameObject m_ClientSideValidPrefab; private GameObject m_ClientSideExceptionPrefab; + public class VerifyLastClientSentRpcToServer : NetworkBehaviour + { + public bool RpcReceived { get; private set; } + + protected override void OnNetworkPreSpawn(ref NetworkManager networkManager) + { + RpcReceived = false; + base.OnNetworkPreSpawn(ref networkManager); + } + + public void DelayUntilOneMessageReceivedRpc(RpcParams rpcParams = default) + { + RpcReceived = true; + } + } + + protected override void OnCreatePlayerPrefab() + { + m_PlayerPrefab.AddComponent(); + base.OnCreatePlayerPrefab(); + } + protected override void OnServerAndClientsCreated() { m_ValidPrefab = CreateNetworkObjectPrefab("ValidPrefab"); @@ -28,7 +50,7 @@ protected override void OnServerAndClientsCreated() } [UnityTest] - [UnityPlatform(exclude = new[] { RuntimePlatform.IPhonePlayer, RuntimePlatform.OSXPlayer, RuntimePlatform.OSXEditor })] // Ignored test tracked in MTT-15473 + //[UnityPlatform(exclude = new[] { RuntimePlatform.IPhonePlayer, RuntimePlatform.OSXPlayer, RuntimePlatform.OSXEditor })] // Ignored test tracked in MTT-15473 public IEnumerator NetworkPrefabHandlerSpawnAndSynchronizeTests() { var nonAuthority = GetNonAuthorityNetworkManager(); @@ -73,6 +95,8 @@ public IEnumerator NetworkPrefabHandlerSpawnAndSynchronizeTests() // Start and synchronize the new client yield return StartClient(newClient); + AssertOnTimeout($"Timed out waiting for the late joining client, {newClient.name}, to connect!"); + // Validate the valid prefab spawned on all clients without issue var expectedAuthorityHash = m_ValidPrefab.GetComponent().GlobalObjectIdHash; @@ -92,6 +116,21 @@ public IEnumerator NetworkPrefabHandlerSpawnAndSynchronizeTests() Assert.That(networkManager.SpawnManager.SpawnedObjects.ContainsKey(exceptionObject.NetworkObjectId), Is.False, "Non authority should not have spawned exception object!"); } } + + // Assure this test continues to run until we verify the late joining client has sent 1 message to the server + // This should be the fix for MTT-15473 where the test finishes/exits before the message from the client has been received and processed by the server. + Assert.IsTrue(authority.SpawnManager.SpawnedObjects.ContainsKey(newClient.LocalClient.PlayerObject.NetworkObjectId), $"Server does not have a player for Client-{newClient.LocalClientId}!"); + + // Get server and late joining client's VerifyLastClientSentRpcToServer NetworkBehaviour + var serverLateClientInstance = authority.SpawnManager.SpawnedObjects[newClient.LocalClient.PlayerObject.NetworkObjectId].GetComponent(); + var sendRpc = newClient.LocalClient.PlayerObject.GetComponent(); + + // Send a message from the late joining client to the server + sendRpc.DelayUntilOneMessageReceivedRpc(); + + // Wait for the server to have received this message before exiting the test. + // If the log message has not been received by the server at this point, then there is some other type of bug specific to iOS and Mac. + yield return WaitForConditionOrTimeOut(() => serverLateClientInstance.RpcReceived); } } }