Skip to content

Commit 096468a

Browse files
test - fix
Going to see if this resolves the issue with 2021.3 windows validation and the memory disposed error message.
1 parent 89a825f commit 096468a

File tree

2 files changed

+82
-4
lines changed

2 files changed

+82
-4
lines changed

com.unity.netcode.gameobjects/Tests/Runtime/ConnectionApprovalTimeoutTests.cs

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Text.RegularExpressions;
33
using NUnit.Framework;
44
using Unity.Netcode.TestHelpers.Runtime;
5+
using Unity.Netcode.Transports.UTP;
56
using UnityEngine;
67
using UnityEngine.TestTools;
78

@@ -55,20 +56,28 @@ protected override void OnServerAndClientsCreated()
5556
base.OnServerAndClientsCreated();
5657
}
5758

59+
private MessageCatcher<ConnectionRequestMessage> m_ConnectionRequestCatcher;
60+
private MessageCatcher<ConnectionApprovedMessage> m_ConnectionApprovedCatcher;
5861
protected override IEnumerator OnStartedServerAndClients()
5962
{
63+
m_ClientStopped = false;
64+
m_ServerStopped = false;
65+
m_ClientNetworkManagers[0].OnClientStopped += OnClientStopped;
66+
m_ServerNetworkManager.OnServerStopped += OnServerStopped;
6067
if (m_ApprovalFailureType == ApprovalTimedOutTypes.ServerDoesNotRespond)
6168
{
6269
// 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);
6472
m_ExpectedLogMessage = new Regex("Timed out waiting for the server to approve the connection request.");
6573
m_LogType = LogType.Log;
6674
}
6775
else
6876
{
6977
// We catch (don't process) the incoming connection request message to simulate a transport connection but the client never
7078
// 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);
7281

7382
// For this test, we know the timed out client will be Client-1
7483
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()
98107

99108
Assert.AreEqual(0, m_ServerNetworkManager.ConnectionManager.PendingClients.Count, $"Expected no pending clients when there were {m_ServerNetworkManager.ConnectionManager.PendingClients.Count} pending clients!");
100109
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;
101139
}
140+
141+
private bool m_ServerStopped;
142+
private void OnServerStopped(bool obj)
143+
{
144+
m_ServerNetworkManager.OnServerStopped -= OnServerStopped;
145+
m_ServerStopped = true;
146+
}
147+
102148
}
103149
}

com.unity.netcode.gameobjects/Tests/Runtime/TestHelpers/MessageCatcher.cs

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
using System;
22
using System.Collections.Generic;
33
using Unity.Collections;
4+
using UnityEngine;
45

56
namespace Unity.Netcode.RuntimeTests
67
{
78
internal class MessageCatcher<TMessageType> : INetworkHooks where TMessageType : INetworkMessage
89
{
910
private NetworkManager m_OwnerNetworkManager;
1011

11-
public MessageCatcher(NetworkManager ownerNetworkManager)
12+
private bool m_VerboseDebug;
13+
14+
public MessageCatcher(NetworkManager ownerNetworkManager, bool verboseDebug = false)
1215
{
1316
m_OwnerNetworkManager = ownerNetworkManager;
1417
}
@@ -23,9 +26,38 @@ private struct TriggerData
2326
}
2427
private readonly List<TriggerData> m_CaughtMessages = new List<TriggerData>();
2528

26-
public void ReleaseMessages()
29+
30+
private void Log(string message)
2731
{
32+
if (!m_VerboseDebug)
33+
{
34+
return;
35+
}
2836

37+
Debug.Log($"[Client-{m_OwnerNetworkManager.LocalClientId}] {message}");
38+
}
39+
40+
internal void ClearMessages()
41+
{
42+
if (m_CaughtMessages.Count == 0)
43+
{
44+
return;
45+
}
46+
Log($"Clearing messages.");
47+
foreach (var caughtSpawn in m_CaughtMessages)
48+
{
49+
if (caughtSpawn.Reader.IsInitialized)
50+
{
51+
Log($"Disposing reader (size: {caughtSpawn.Reader.Length}).");
52+
caughtSpawn.Reader.Dispose();
53+
}
54+
}
55+
56+
m_CaughtMessages.Clear();
57+
}
58+
59+
public void ReleaseMessages()
60+
{
2961
foreach (var caughtSpawn in m_CaughtMessages)
3062
{
3163
// Reader will be disposed within HandleMessage

0 commit comments

Comments
 (0)