|
1 | 1 |
|
2 | 2 | using System.Collections;
|
| 3 | +using System.Linq; |
3 | 4 | using NUnit.Framework;
|
4 | 5 | using UnityEngine;
|
5 | 6 | using UnityEngine.TestTools;
|
@@ -383,5 +384,111 @@ public IEnumerator LateJoinSynchronizationTest([Values] OwnerShipMode ownerShipM
|
383 | 384 | yield return StopOneClient(newlyJoinedClient);
|
384 | 385 | VerboseDebug($" ------------------ Late-Join Test [{TriggerTest.Iteration}][{ownerShipMode}] Stopping ------------------ ");
|
385 | 386 | }
|
| 387 | + |
| 388 | + private bool m_ClientDisconnected; |
| 389 | + /// <summary> |
| 390 | + /// This validates that NetworkAnimator properly removes its subscription to the |
| 391 | + /// OnClientConnectedCallback when it is despawned and destroyed during the |
| 392 | + /// shutdown sequence on both the server and the client. |
| 393 | + /// </summary> |
| 394 | + [UnityTest] |
| 395 | + public IEnumerator ShutdownWhileSpawnedAndStartBackUpTest() |
| 396 | + { |
| 397 | + VerboseDebug($" ++++++++++++++++++ Disconnect-Reconnect Server Test Starting ++++++++++++++++++ "); |
| 398 | + // Spawn our test animator object |
| 399 | + var objectInstance = SpawnPrefab(false, AuthoritativeMode.ServerAuth); |
| 400 | + var networkObjectInstance = objectInstance.GetComponent<NetworkObject>(); |
| 401 | + var serverAnimatorTestHelper = objectInstance.GetComponent<AnimatorTestHelper>(); |
| 402 | + m_ServerNetworkManager.OnClientDisconnectCallback += ServerNetworkManager_OnClientDisconnectCallback; |
| 403 | + // Wait for it to spawn server-side |
| 404 | + yield return WaitForConditionOrTimeOut(() => AnimatorTestHelper.ServerSideInstance != null); |
| 405 | + AssertOnTimeout($"Timed out waiting for the server-side instance of {GetNetworkAnimatorName(AuthoritativeMode.ServerAuth)} to be spawned!"); |
| 406 | + |
| 407 | + // Wait for it to spawn client-side |
| 408 | + yield return WaitForConditionOrTimeOut(WaitForClientsToInitialize); |
| 409 | + AssertOnTimeout($"Timed out waiting for the client-side instance of {GetNetworkAnimatorName(AuthoritativeMode.ServerAuth)} to be spawned!"); |
| 410 | + |
| 411 | + var clientAnimatorTestHelper = s_GlobalNetworkObjects[m_ClientNetworkManagers[0].LocalClientId].Values.Where((c) => c.GetComponent<AnimatorTestHelper>() != null).First().GetComponent<AnimatorTestHelper>(); |
| 412 | + Assert.IsNotNull(clientAnimatorTestHelper, $"Could not find the client side {nameof(AnimatorTestHelper)}!"); |
| 413 | + VerboseDebug($" ++++++++++++++++++ Disconnect-Reconnect Shutting Down Client and Server ++++++++++++++++++ "); |
| 414 | + clientAnimatorTestHelper.OnCheckIsServerIsClient += Client_OnCheckIsServerIsClient; |
| 415 | + |
| 416 | + // Now shutdown the client-side to verify this fix. |
| 417 | + // The client-side spawned NetworkObject should get despawned |
| 418 | + // and invoke the Client_OnCheckIsServerIsClient action. |
| 419 | + m_ClientNetworkManagers[0].Shutdown(true); |
| 420 | + |
| 421 | + // Wait for the server to receive the client disconnection notification |
| 422 | + yield return WaitForConditionOrTimeOut(() => m_ClientDisconnected); |
| 423 | + AssertOnTimeout($"Timed out waiting for the client to disconnect!"); |
| 424 | + |
| 425 | + Assert.IsTrue(m_ClientTestHelperDespawned, $"Client-Side {nameof(AnimatorTestHelper)} did not have a valid IsClient setting!"); |
| 426 | + |
| 427 | + serverAnimatorTestHelper.OnCheckIsServerIsClient += Server_OnCheckIsServerIsClient; |
| 428 | + m_ServerNetworkManager.OnClientDisconnectCallback -= ServerNetworkManager_OnClientDisconnectCallback; |
| 429 | + |
| 430 | + // Now shutdown the server-side to verify this fix. |
| 431 | + // The server-side spawned NetworkObject should get despawned |
| 432 | + // and invoke the Server_OnCheckIsServerIsClient action. |
| 433 | + m_ServerNetworkManager.Shutdown(); |
| 434 | + |
| 435 | + yield return s_DefaultWaitForTick; |
| 436 | + |
| 437 | + yield return WaitForConditionOrTimeOut(() => !m_ServerNetworkManager.ShutdownInProgress); |
| 438 | + |
| 439 | + Assert.IsTrue(m_ServerTestHelperDespawned, $"Server-Side {nameof(AnimatorTestHelper)} did not have a valid IsServer setting!"); |
| 440 | + AssertOnTimeout($"Timed out waiting for the server to shutdown!"); |
| 441 | + |
| 442 | + VerboseDebug($" ++++++++++++++++++ Disconnect-Reconnect Restarting Server and Client ++++++++++++++++++ "); |
| 443 | + // Since the dynamically generated PlayerPrefab is destroyed when the server shuts down, |
| 444 | + // we need to create a new one and assign it to NetworkPrefab index 0 |
| 445 | + m_PlayerPrefab = new GameObject("Player"); |
| 446 | + NetworkObject networkObject = m_PlayerPrefab.AddComponent<NetworkObject>(); |
| 447 | + NetcodeIntegrationTestHelpers.MakeNetworkObjectTestPrefab(networkObject); |
| 448 | + m_ServerNetworkManager.NetworkConfig.NetworkPrefabs[0].Prefab = m_PlayerPrefab; |
| 449 | + m_ClientNetworkManagers[0].NetworkConfig.NetworkPrefabs[0].Prefab = m_PlayerPrefab; |
| 450 | + OnCreatePlayerPrefab(); |
| 451 | + |
| 452 | + // Now, restart the server and the client |
| 453 | + m_ServerNetworkManager.StartHost(); |
| 454 | + m_ClientNetworkManagers[0].StartClient(); |
| 455 | + |
| 456 | + // Wait for the server and client to start and connect |
| 457 | + yield return WaitForClientsConnectedOrTimeOut(); |
| 458 | + |
| 459 | + VerboseDebug($" ++++++++++++++++++ Disconnect-Reconnect Server Test Stopping ++++++++++++++++++ "); |
| 460 | + } |
| 461 | + |
| 462 | + private bool m_ServerTestHelperDespawned; |
| 463 | + /// <summary> |
| 464 | + /// Server-Side |
| 465 | + /// This callback will be invoked as the spawned prefab is destroyed during shutdown |
| 466 | + /// </summary> |
| 467 | + private void Server_OnCheckIsServerIsClient(bool isServer, bool isClient) |
| 468 | + { |
| 469 | + // Validates this is still set when the NetworkObject is despawned during shutdown |
| 470 | + Assert.IsTrue(isServer); |
| 471 | + m_ServerTestHelperDespawned = true; |
| 472 | + } |
| 473 | + |
| 474 | + private bool m_ClientTestHelperDespawned; |
| 475 | + /// <summary> |
| 476 | + /// Client-Side |
| 477 | + /// This callback will be invoked as the spawned prefab is destroyed during shutdown |
| 478 | + /// </summary> |
| 479 | + private void Client_OnCheckIsServerIsClient(bool isServer, bool isClient) |
| 480 | + { |
| 481 | + // Validates this is still set when the NetworkObject is despawned during shutdown |
| 482 | + Assert.IsTrue(isClient); |
| 483 | + m_ClientTestHelperDespawned = true; |
| 484 | + } |
| 485 | + |
| 486 | + /// <summary> |
| 487 | + /// Verifies the client has disconnected |
| 488 | + /// </summary> |
| 489 | + private void ServerNetworkManager_OnClientDisconnectCallback(ulong obj) |
| 490 | + { |
| 491 | + m_ClientDisconnected = true; |
| 492 | + } |
386 | 493 | }
|
387 | 494 | }
|
0 commit comments