Skip to content

Commit 21fcaab

Browse files
fix: MessageSendingTests causing memory leak during editor tests. (#2125)
* fix Seeing if this fixes the issue with our memory leaks. * fix minor fix to my fix. Need to reconnect the client after creating the new MessagingSystem. adjusted a comment. * Update com.unity.netcode.gameobjects/Tests/Editor/Messaging/MessageSendingTests.cs Adding Fatih's LF/CR suggestion for readability Co-authored-by: Fatih Mar <[email protected]> Co-authored-by: Fatih Mar <[email protected]>
1 parent bc23a35 commit 21fcaab

File tree

1 file changed

+32
-4
lines changed

1 file changed

+32
-4
lines changed

com.unity.netcode.gameobjects/Tests/Editor/Messaging/MessageSendingTests.cs

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using System.Collections.Generic;
23
using System.Text.RegularExpressions;
34
using NUnit.Framework;
@@ -43,21 +44,38 @@ public void Send(ulong clientId, NetworkDelivery delivery, FastBufferWriter batc
4344
}
4445
}
4546

46-
private class TestMessageProvider : IMessageProvider
47+
private class TestMessageProvider : IMessageProvider, IDisposable
4748
{
49+
// Keep track of what we sent
50+
private List<List<MessagingSystem.MessageWithHandler>> m_CachedMessages = new List<List<MessagingSystem.MessageWithHandler>>();
51+
52+
public void Dispose()
53+
{
54+
foreach (var cachedItem in m_CachedMessages)
55+
{
56+
// Clear out any references to MessagingSystem.MessageWithHandlers
57+
cachedItem.Clear();
58+
}
59+
m_CachedMessages.Clear();
60+
}
61+
4862
public List<MessagingSystem.MessageWithHandler> GetMessages()
4963
{
50-
return new List<MessagingSystem.MessageWithHandler>
64+
var messageList = new List<MessagingSystem.MessageWithHandler>
5165
{
5266
new MessagingSystem.MessageWithHandler
5367
{
5468
MessageType = typeof(TestMessage),
5569
Handler = MessagingSystem.ReceiveMessage<TestMessage>
5670
}
5771
};
72+
// Track messages sent
73+
m_CachedMessages.Add(messageList);
74+
return messageList;
5875
}
5976
}
6077

78+
private TestMessageProvider m_TestMessageProvider;
6179
private TestMessageSender m_MessageSender;
6280
private MessagingSystem m_MessagingSystem;
6381
private ulong[] m_Clients = { 0 };
@@ -66,15 +84,16 @@ private class TestMessageProvider : IMessageProvider
6684
public void SetUp()
6785
{
6886
TestMessage.Serialized = false;
69-
7087
m_MessageSender = new TestMessageSender();
71-
m_MessagingSystem = new MessagingSystem(m_MessageSender, this, new TestMessageProvider());
88+
m_TestMessageProvider = new TestMessageProvider();
89+
m_MessagingSystem = new MessagingSystem(m_MessageSender, this, m_TestMessageProvider);
7290
m_MessagingSystem.ClientConnected(0);
7391
}
7492

7593
[TearDown]
7694
public void TearDown()
7795
{
96+
m_TestMessageProvider.Dispose();
7897
m_MessagingSystem.Dispose();
7998
}
8099

@@ -246,7 +265,16 @@ private class TestNoHandlerMessageProvider : IMessageProvider
246265
[Test]
247266
public void WhenReceivingAMessageWithoutAHandler_ExceptionIsLogged()
248267
{
268+
// If a MessagingSystem already exists then dispose of it before creating a new MessagingSystem (otherwise memory leak)
269+
if (m_MessagingSystem != null)
270+
{
271+
m_MessagingSystem.Dispose();
272+
m_MessagingSystem = null;
273+
}
274+
275+
// Since m_MessagingSystem is disposed during teardown we don't need to worry about that here.
249276
m_MessagingSystem = new MessagingSystem(new NopMessageSender(), this, new TestNoHandlerMessageProvider());
277+
m_MessagingSystem.ClientConnected(0);
250278

251279
var messageHeader = new MessageHeader
252280
{

0 commit comments

Comments
 (0)