|
2 | 2 | using System.Text.RegularExpressions;
|
3 | 3 | using NUnit.Framework;
|
4 | 4 | using Unity.Netcode.TestHelpers.Runtime;
|
| 5 | +using Unity.Netcode.Transports.UTP; |
5 | 6 | using UnityEngine;
|
6 | 7 | using UnityEngine.TestTools;
|
7 | 8 |
|
@@ -55,20 +56,28 @@ protected override void OnServerAndClientsCreated()
|
55 | 56 | base.OnServerAndClientsCreated();
|
56 | 57 | }
|
57 | 58 |
|
| 59 | + private MessageCatcher<ConnectionRequestMessage> m_ConnectionRequestCatcher; |
| 60 | + private MessageCatcher<ConnectionApprovedMessage> m_ConnectionApprovedCatcher; |
58 | 61 | protected override IEnumerator OnStartedServerAndClients()
|
59 | 62 | {
|
| 63 | + m_ClientStopped = false; |
| 64 | + m_ServerStopped = false; |
| 65 | + m_ClientNetworkManagers[0].OnClientStopped += OnClientStopped; |
| 66 | + m_ServerNetworkManager.OnServerStopped += OnServerStopped; |
60 | 67 | if (m_ApprovalFailureType == ApprovalTimedOutTypes.ServerDoesNotRespond)
|
61 | 68 | {
|
62 | 69 | // We catch (don't process) the incoming approval message to simulate the server not sending the approved message in time
|
63 |
| - m_ClientNetworkManagers[0].ConnectionManager.MessageManager.Hook(new MessageCatcher<ConnectionApprovedMessage>(m_ClientNetworkManagers[0])); |
| 70 | + m_ConnectionApprovedCatcher = new MessageCatcher<ConnectionApprovedMessage>(m_ClientNetworkManagers[0], m_EnableVerboseDebug); |
| 71 | + m_ClientNetworkManagers[0].ConnectionManager.MessageManager.Hook(m_ConnectionApprovedCatcher); |
64 | 72 | m_ExpectedLogMessage = new Regex("Timed out waiting for the server to approve the connection request.");
|
65 | 73 | m_LogType = LogType.Log;
|
66 | 74 | }
|
67 | 75 | else
|
68 | 76 | {
|
69 | 77 | // We catch (don't process) the incoming connection request message to simulate a transport connection but the client never
|
70 | 78 | // sends (or takes too long to send) the connection request.
|
71 |
| - m_ServerNetworkManager.ConnectionManager.MessageManager.Hook(new MessageCatcher<ConnectionRequestMessage>(m_ServerNetworkManager)); |
| 79 | + m_ConnectionRequestCatcher = new MessageCatcher<ConnectionRequestMessage>(m_ServerNetworkManager, m_EnableVerboseDebug); |
| 80 | + m_ServerNetworkManager.ConnectionManager.MessageManager.Hook(m_ConnectionRequestCatcher); |
72 | 81 |
|
73 | 82 | // For this test, we know the timed out client will be Client-1
|
74 | 83 | m_ExpectedLogMessage = new Regex("Server detected a transport connection from Client-1, but timed out waiting for the connection request message.");
|
@@ -98,6 +107,43 @@ public IEnumerator ValidateApprovalTimeout()
|
98 | 107 |
|
99 | 108 | Assert.AreEqual(0, m_ServerNetworkManager.ConnectionManager.PendingClients.Count, $"Expected no pending clients when there were {m_ServerNetworkManager.ConnectionManager.PendingClients.Count} pending clients!");
|
100 | 109 | Assert.True(!m_ClientNetworkManagers[0].LocalClient.IsApproved, $"Expected the client to not have been approved, but it was!");
|
| 110 | + |
| 111 | + if (m_ApprovalFailureType == ApprovalTimedOutTypes.ServerDoesNotRespond) |
| 112 | + { |
| 113 | + m_ConnectionApprovedCatcher.ClearMessages(); |
| 114 | + } |
| 115 | + else |
| 116 | + { |
| 117 | + m_ConnectionRequestCatcher.ClearMessages(); |
| 118 | + } |
| 119 | + |
| 120 | + if (!m_ClientStopped) |
| 121 | + { |
| 122 | + m_ClientNetworkManagers[0].Shutdown(); |
| 123 | + } |
| 124 | + |
| 125 | + if (!m_ServerStopped) |
| 126 | + { |
| 127 | + m_ServerNetworkManager.Shutdown(); |
| 128 | + } |
| 129 | + |
| 130 | + yield return WaitForConditionOrTimeOut(() => m_ClientStopped && m_ServerStopped); |
| 131 | + AssertOnTimeout($"Timed out waiting for the client or server to stop!"); |
| 132 | + } |
| 133 | + |
| 134 | + private bool m_ClientStopped; |
| 135 | + private void OnClientStopped(bool obj) |
| 136 | + { |
| 137 | + m_ClientNetworkManagers[0].OnClientStopped -= OnClientStopped; |
| 138 | + m_ClientStopped = true; |
101 | 139 | }
|
| 140 | + |
| 141 | + private bool m_ServerStopped; |
| 142 | + private void OnServerStopped(bool obj) |
| 143 | + { |
| 144 | + m_ServerNetworkManager.OnServerStopped -= OnServerStopped; |
| 145 | + m_ServerStopped = true; |
| 146 | + } |
| 147 | + |
102 | 148 | }
|
103 | 149 | }
|
0 commit comments