Skip to content

Commit 0b23d7b

Browse files
fix: Unity.Netcode.RuntimeTests Failing on Console - v1.1.0 (#1653)
* Squashed commit of the following: commit 36c52d7 Author: NoelStephensUnity <73188597+NoelStephensUnity@users.noreply.github.com> Date: Wed Feb 2 22:39:30 2022 -0600 style arrg!!!!!! white space commit 861ef54 Author: NoelStephensUnity <73188597+NoelStephensUnity@users.noreply.github.com> Date: Wed Feb 2 19:51:09 2022 -0600 fix and update fixed issue with WhenListContainsManyLargeValues_OverflowExceptionIsNotThrown count comparison value. Updated removed FixedString32Test by removing yield return that was not needed. commit 3e70580 Author: NoelStephensUnity <73188597+NoelStephensUnity@users.noreply.github.com> Date: Wed Feb 2 19:41:57 2022 -0600 update Reviewing the updated methods and making some minor adjustments. commit ba3e09d Author: NoelStephensUnity <73188597+NoelStephensUnity@users.noreply.github.com> Date: Wed Feb 2 19:19:15 2022 -0600 style removing a using statement for a namespace that was not being used. commit 3088309 Author: NoelStephensUnity <73188597+NoelStephensUnity@users.noreply.github.com> Date: Wed Feb 2 19:16:51 2022 -0600 fix The AllNetworkVariableTypes was not properly creating the NetworkManager based on the value of useHost commit 0f9d8ca Author: NoelStephensUnity <73188597+NoelStephensUnity@users.noreply.github.com> Date: Wed Feb 2 18:56:08 2022 -0600 update removing more MultiInstanceHelpers.Run calls commit 38ec0fe Author: NoelStephensUnity <73188597+NoelStephensUnity@users.noreply.github.com> Date: Wed Feb 2 18:48:47 2022 -0600 update removing MultiInstanceHelpers.Run calls from NetworkTransform as they are not needed. commit f8fd60c Author: NoelStephensUnity <73188597+NoelStephensUnity@users.noreply.github.com> Date: Wed Feb 2 18:32:53 2022 -0600 test update and style removing some of the Run calls and beginning to convert over some of the problematic tests to just yield return the conditional check itself to avoid potential timing issues when checking the condition and yielding to the current test group's newly created coroutine. commit 71a4d4e Merge: 994d459 27e8498 Author: NoelStephensUnity <73188597+NoelStephensUnity@users.noreply.github.com> Date: Wed Feb 2 17:56:21 2022 -0600 Merge branch 'develop' into fix/console-tests-failing commit 994d459 Merge: 35b47a5 74df5b2 Author: NoelStephensUnity <73188597+NoelStephensUnity@users.noreply.github.com> Date: Wed Feb 2 17:29:17 2022 -0600 Merge branch 'develop' into fix/console-tests-failing commit 35b47a5 Author: NoelStephensUnity <73188597+NoelStephensUnity@users.noreply.github.com> Date: Wed Feb 2 17:29:07 2022 -0600 test refactor This update collapses all of the time out code into the BaseMultiInstanceTest with an added method WaitForConditionOrTimeOut that leverages from an added TimeOutHelper class. commit 73340d6 Merge: feb9e47 096f614 Author: Noel Stephens <noel.stephens@unity3d.com> Date: Wed Feb 2 02:59:21 2022 -0600 Merge branch 'develop' into fix/console-tests-failing commit feb9e47 Merge: a66d4f3 e07450f Author: Noel Stephens <noel.stephens@unity3d.com> Date: Tue Feb 1 16:14:06 2022 -0600 Merge branch 'develop' into fix/console-tests-failing commit a66d4f3 Author: NoelStephensUnity <73188597+NoelStephensUnity@users.noreply.github.com> Date: Tue Feb 1 16:13:53 2022 -0600 style removing whitespace. commit 02e4681 Author: NoelStephensUnity <73188597+NoelStephensUnity@users.noreply.github.com> Date: Tue Feb 1 13:50:30 2022 -0600 style making sure NetworkTransform was exactly as it was before, removing a LF/CR commit 33e6601 Author: NoelStephensUnity <73188597+NoelStephensUnity@users.noreply.github.com> Date: Tue Feb 1 13:49:13 2022 -0600 refactor Came up with what I hope is a much better fix to get NetworkTransformTests passing on all consoles. Passes on a windows stand alone test runner build...will need to run this on the consoles to verify the fix. commit 91d8f31 Merge: 72d3d49 ef257a9 Author: Noel Stephens <noel.stephens@unity3d.com> Date: Tue Feb 1 09:29:42 2022 -0600 Merge branch 'develop' into fix/console-tests-failing commit 72d3d49 Author: NoelStephensUnity <73188597+NoelStephensUnity@users.noreply.github.com> Date: Mon Jan 31 12:46:02 2022 -0600 refactor removing the timeout code to reduce confusion on the changes to NetworkVariableTests. commit 82d243f Author: NoelStephensUnity <73188597+NoelStephensUnity@users.noreply.github.com> Date: Mon Jan 31 11:38:44 2022 -0600 revert re-applying readonly to the m_TestWithClientNetworkTransform and m_TestWithHost properties as that was changed during troubleshooting. commit 3a90687 Author: NoelStephensUnity <73188597+NoelStephensUnity@users.noreply.github.com> Date: Mon Jan 31 11:34:56 2022 -0600 refactor moving the HasTimedOut check to the end of each while loop to assure that any possible edge case scenario where it finishes just as it would be timing out is not missed. An example would be the condition being checked is validated but it lands right on the timeout period. The test would have been a success, but timedOut would have been set too. commit 71ae813 Author: NoelStephensUnity <73188597+NoelStephensUnity@users.noreply.github.com> Date: Mon Jan 31 11:23:24 2022 -0600 refactor using the bool as opposed to direct method call for the while loops. commit 96b62b4 Author: NoelStephensUnity <73188597+NoelStephensUnity@users.noreply.github.com> Date: Mon Jan 31 10:37:07 2022 -0600 update Applied suggestion by Fatih to use deltaTime with normal Update selected and fixedDeltaTime for FixedUpdate selected. commit 90b9ed9 Merge: d509fca c17f5b8 Author: Noel Stephens <noel.stephens@unity3d.com> Date: Sat Jan 29 15:09:31 2022 -0600 Merge branch 'develop' into fix/console-tests-failing commit d509fca Merge: 8244627 58f194d Author: Noel Stephens <noel.stephens@unity3d.com> Date: Fri Jan 28 17:17:15 2022 -0600 Merge branch 'develop' into fix/console-tests-failing commit 8244627 Merge: 43e9382 402210e Author: NoelStephensUnity <73188597+NoelStephensUnity@users.noreply.github.com> Date: Fri Jan 28 13:18:33 2022 -0600 Merge branch 'fix/console-tests-failing' of https://github.com/Unity-Technologies/com.unity.netcode.gameobjects into fix/console-tests-failing commit 43e9382 Author: NoelStephensUnity <73188597+NoelStephensUnity@users.noreply.github.com> Date: Fri Jan 28 13:18:12 2022 -0600 update Removing the auto-assignment of the NetworkTransformTests's constructor's properties that were left over from troubleshooting. commit 402210e Merge: 554bf0d e4108e5 Author: Noel Stephens <noel.stephens@unity3d.com> Date: Fri Jan 28 13:14:11 2022 -0600 Merge branch 'develop' into fix/console-tests-failing commit 554bf0d Author: NoelStephensUnity <73188597+NoelStephensUnity@users.noreply.github.com> Date: Fri Jan 28 09:06:52 2022 -0600 style removing a portion of the comment as it was a copy-paste of code used in other fixes. commit 7766f27 Author: NoelStephensUnity <73188597+NoelStephensUnity@users.noreply.github.com> Date: Fri Jan 28 02:37:06 2022 -0600 fix delta read was failing. commit 041e10d Author: NoelStephensUnity <73188597+NoelStephensUnity@users.noreply.github.com> Date: Fri Jan 28 01:39:59 2022 -0600 fix This includes updates to the NetworkObjectOnSpawnTests in order to get it passing in stand alone test runner builds in order to get it to pass on consoles. commit 26d2335 Author: NoelStephensUnity <73188597+NoelStephensUnity@users.noreply.github.com> Date: Fri Jan 28 01:16:37 2022 -0600 style white space missing. commit 5b6146a Author: NoelStephensUnity <73188597+NoelStephensUnity@users.noreply.github.com> Date: Fri Jan 28 01:16:08 2022 -0600 fix This includes the fixes for getting NetworkVarBufferCopyTest to run on a stand alone test runner build. commit 0e4435b Merge: 9628212 5032214 Author: NoelStephensUnity <73188597+NoelStephensUnity@users.noreply.github.com> Date: Thu Jan 27 20:41:58 2022 -0600 Merge branch 'develop' into fix/console-tests-failing commit 9628212 Author: NoelStephensUnity <73188597+NoelStephensUnity@users.noreply.github.com> Date: Thu Jan 27 20:41:44 2022 -0600 style Removed unused using statement. commit c022e08 Author: NoelStephensUnity <73188597+NoelStephensUnity@users.noreply.github.com> Date: Thu Jan 27 20:15:02 2022 -0600 fix - partial This is a "quasi-fix" where I had to add the ability to run NetworkTransform in the FixedUpdate in order to get the NetworkTransform tests all passing when running in a stand alone test runner build. commit ae1c750 Merge: 859d88c 1704918 Author: NoelStephensUnity <73188597+NoelStephensUnity@users.noreply.github.com> Date: Thu Jan 27 14:50:38 2022 -0600 Merge branch 'develop' into fix/console-tests-failing commit 859d88c Author: NoelStephensUnity <73188597+NoelStephensUnity@users.noreply.github.com> Date: Thu Jan 27 14:45:56 2022 -0600 style Just added some additional comments. commit 4b40c04 Author: NoelStephensUnity <73188597+NoelStephensUnity@users.noreply.github.com> Date: Thu Jan 27 14:31:20 2022 -0600 fix This should fix the timing issues on consoles for the NetworkVariableTests. * fix and merge updates Fixes and minor modifications due to some differences between develop and v1.1.0 * update weird merge issue fix * test fix fixing a few more tests that failed on some of the console platforms. * test fix Still running into timing issues during transport tests. Bumped the wait up to 2s and had to adjust FilledSendQueueMultipleSends to wait for the packet to be received before proceeding to send the next (this could probably be handled better but just putting a place holder fix in place for transport issues so I can confirm this the NGO runtime tests are all passing and for desktop server to see what I had to do to get it passing on some of the platforms under v1.1.0). Also had to update the rest of the NetworkVariable tests to use the newer WaitForConditionOrTimeOut method. * refactor Putting System.Linq reference back as even though it shows it is not used...it is. * temp backport of #1638 Realized the fixes for this were not backported to v1.0.0. This is a temporary back port for my branch to get past the SendMaximumPayloadSize failures * test backport temp This fixes the issue with the standards checking of data.ToArray(). * test full backport of TransportTests Finally looked at the very top of the TransportTests and realized they should be disabled for consoles. * fix fixes a mistake I made in the IndexOf test * style indent * refactor Added the ConditionalPredicateBase implementation of the IConditionalPredicate and implemented an overloaded version of WaitForConditionOrTimeOut. Changed the original WaitForConditionOrTimeOut to just return true as all use cases of these UnityTest attribute decorated methods had access to the values and were relatively simple conditions. I used the NetworkVariableTests as an example of how to use the a ConditionalPredicateBase derived class to simplify the scenario where you might be re-using the same code or conditional checks in many integration tests. * update and style Condensed the overloaded version of WaitForConditionOrTimeOut that accepts an IConditionalPredicate implementation. Updates and added comments. * update Migrating a fix for and issue that was seen when testing both test project and runtime tests together. The update takes into consideration the rounding error that can occur when calculating tick from time (which was an earlier fix for the tick system). * fix Noticed on short was not switched to ushort.
1 parent 79f66b0 commit 0b23d7b

File tree

12 files changed

+1167
-533
lines changed

12 files changed

+1167
-533
lines changed

com.unity.netcode.adapter.utp/Tests/Runtime/Helpers/RuntimeTestsHelpers.cs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,38 @@
11
using NUnit.Framework;
22
using System;
3-
using System.Linq;
43
using System.Collections;
54
using System.Collections.Generic;
65
using UnityEngine;
7-
86
namespace Unity.Netcode.UTP.RuntimeTests
97
{
108
public static class RuntimeTestsHelpers
119
{
1210
// Half a second might seem like a very long time to wait for a network event, but in CI
1311
// many of the machines are underpowered (e.g. old Android devices or Macs) and there are
14-
// sometimes lag spikes that cause can cause delays upwards of 300ms.
12+
// sometimes very high lag spikes. PS4 and Switch are particularly sensitive in this regard
13+
// so we allow even more time for these platforms.
14+
#if UNITY_PS4 || UNITY_SWITCH
15+
public const float MaxNetworkEventWaitTime = 2.0f;
16+
#else
1517
public const float MaxNetworkEventWaitTime = 0.5f;
18+
#endif
1619

1720
// Wait for an event to appear in the given event list (must be the very next event).
18-
public static IEnumerator WaitForNetworkEvent(NetworkEvent type, List<TransportEvent> events)
21+
public static IEnumerator WaitForNetworkEvent(NetworkEvent type, List<TransportEvent> events,
22+
float timeout = MaxNetworkEventWaitTime)
1923
{
2024
int initialCount = events.Count;
2125
float startTime = Time.realtimeSinceStartup;
2226

23-
while (Time.realtimeSinceStartup - startTime < MaxNetworkEventWaitTime)
27+
while (Time.realtimeSinceStartup - startTime < timeout)
2428
{
2529
if (events.Count > initialCount)
2630
{
2731
Assert.AreEqual(type, events[initialCount].Type);
2832
yield break;
2933
}
3034

31-
yield return null;
35+
yield return new WaitForSeconds(0.01f);
3236
}
3337

3438
Assert.Fail("Timed out while waiting for network event.");
@@ -56,23 +60,22 @@ public struct TransportEvent
5660
public ArraySegment<byte> Data;
5761
public float ReceiveTime;
5862
}
59-
6063
// Utility class that logs events generated by a UnityTransport. Set it up by adding the
6164
// HandleEvent method as an OnTransportEvent delegate of the transport. The list of events
6265
// (in order in which they were generated) can be accessed through the Events property.
6366
public class TransportEventLogger
6467
{
6568
private readonly List<TransportEvent> m_Events = new List<TransportEvent>();
6669
public List<TransportEvent> Events => m_Events;
67-
6870
public void HandleEvent(NetworkEvent type, ulong clientID, ArraySegment<byte> data, float receiveTime)
6971
{
7072
// Copy the data since the backing array will be reused for future messages.
7173
if (data != default(ArraySegment<byte>))
7274
{
73-
data = new ArraySegment<byte>(data.ToArray());
75+
var dataCopy = new byte[data.Count];
76+
Array.Copy(data.Array, data.Offset, dataCopy, 0, data.Count);
77+
data = new ArraySegment<byte>(dataCopy);
7478
}
75-
7679
m_Events.Add(new TransportEvent
7780
{
7881
Type = type,

com.unity.netcode.adapter.utp/Tests/Runtime/TransportTests.cs

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// todo @simon-lemay-unity: un-guard/re-enable after validating UTP on consoles
2+
#if UNITY_EDITOR || UNITY_STANDALONE || UNITY_IOS || UNITY_ANDROID
13
using NUnit.Framework;
24
using System;
35
using System.Collections;
@@ -30,19 +32,25 @@ public IEnumerator Cleanup()
3032
if (m_Server)
3133
{
3234
m_Server.Shutdown();
33-
UnityEngine.Object.DestroyImmediate(m_Server);
35+
36+
// Need to destroy the GameObject (all assigned components will get destroyed too)
37+
UnityEngine.Object.DestroyImmediate(m_Server.gameObject);
3438
}
3539

3640
if (m_Client1)
3741
{
3842
m_Client1.Shutdown();
39-
UnityEngine.Object.DestroyImmediate(m_Client1);
43+
44+
// Need to destroy the GameObject (all assigned components will get destroyed too)
45+
UnityEngine.Object.DestroyImmediate(m_Client1.gameObject);
4046
}
4147

4248
if (m_Client2)
4349
{
4450
m_Client2.Shutdown();
45-
UnityEngine.Object.DestroyImmediate(m_Client2);
51+
52+
// Need to destroy the GameObject (all assigned components will get destroyed too)
53+
UnityEngine.Object.DestroyImmediate(m_Client2.gameObject);
4654
}
4755

4856
m_ServerEvents?.Clear();
@@ -62,7 +70,7 @@ public IEnumerator PingPong([ValueSource("k_DeliveryParameters")] NetworkDeliver
6270
m_Server.StartServer();
6371
m_Client1.StartClient();
6472

65-
yield return WaitForNetworkEvent(NetworkEvent.Connect, m_ServerEvents);
73+
yield return WaitForNetworkEvent(NetworkEvent.Connect, m_Client1Events);
6674

6775
var ping = new ArraySegment<byte>(Encoding.ASCII.GetBytes("ping"));
6876
m_Client1.Send(m_Client1.ServerClientId, ping, delivery);
@@ -91,7 +99,7 @@ public IEnumerator PingPongSimultaneous([ValueSource("k_DeliveryParameters")] Ne
9199
m_Server.StartServer();
92100
m_Client1.StartClient();
93101

94-
yield return WaitForNetworkEvent(NetworkEvent.Connect, m_ServerEvents);
102+
yield return WaitForNetworkEvent(NetworkEvent.Connect, m_Client1Events);
95103

96104
var ping = new ArraySegment<byte>(Encoding.ASCII.GetBytes("ping"));
97105
m_Server.Send(m_ServerEvents[0].ClientID, ping, delivery);
@@ -128,7 +136,7 @@ public IEnumerator SendMaximumPayloadSize([ValueSource("k_DeliveryParameters")]
128136
m_Server.StartServer();
129137
m_Client1.StartClient();
130138

131-
yield return WaitForNetworkEvent(NetworkEvent.Connect, m_ServerEvents);
139+
yield return WaitForNetworkEvent(NetworkEvent.Connect, m_Client1Events);
132140

133141
var payloadData = new byte[payloadSize];
134142
for (int i = 0; i < payloadData.Length; i++)
@@ -139,7 +147,7 @@ public IEnumerator SendMaximumPayloadSize([ValueSource("k_DeliveryParameters")]
139147
var payload = new ArraySegment<byte>(payloadData);
140148
m_Client1.Send(m_Client1.ServerClientId, payload, delivery);
141149

142-
yield return WaitForNetworkEvent(NetworkEvent.Data, m_ServerEvents);
150+
yield return WaitForNetworkEvent(NetworkEvent.Data, m_ServerEvents, MaxNetworkEventWaitTime * 2);
143151

144152
Assert.AreEqual(payloadSize, m_ServerEvents[1].Data.Count);
145153

@@ -162,7 +170,7 @@ public IEnumerator FilledSendQueueMultipleSends([ValueSource("k_DeliveryParamete
162170
m_Server.StartServer();
163171
m_Client1.StartClient();
164172

165-
yield return WaitForNetworkEvent(NetworkEvent.Connect, m_ServerEvents);
173+
yield return WaitForNetworkEvent(NetworkEvent.Connect, m_Client1Events);
166174

167175
var numSends = UnityTransport.InitialMaxSendQueueSize / 1024;
168176

@@ -180,7 +188,7 @@ public IEnumerator FilledSendQueueMultipleSends([ValueSource("k_DeliveryParamete
180188
yield return new WaitForSeconds(numSends * 0.02f);
181189

182190
// Extra event is the connect event.
183-
Assert.AreEqual(m_ServerEvents.Count, numSends + 1);
191+
Assert.AreEqual(numSends + 1, m_ServerEvents.Count);
184192

185193
for (int i = 1; i <= numSends; i++)
186194
{
@@ -201,7 +209,7 @@ public IEnumerator MultipleSendsSingleFrame([ValueSource("k_DeliveryParameters")
201209
m_Server.StartServer();
202210
m_Client1.StartClient();
203211

204-
yield return WaitForNetworkEvent(NetworkEvent.Connect, m_ServerEvents);
212+
yield return WaitForNetworkEvent(NetworkEvent.Connect, m_Client1Events);
205213

206214
var data1 = new ArraySegment<byte>(new byte[] { 11 });
207215
m_Client1.Send(m_Client1.ServerClientId, data1, delivery);
@@ -232,7 +240,11 @@ public IEnumerator SendMultipleClients([ValueSource("k_DeliveryParameters")] Net
232240
m_Client1.StartClient();
233241
m_Client2.StartClient();
234242

235-
yield return WaitForNetworkEvent(NetworkEvent.Connect, m_ServerEvents);
243+
yield return WaitForNetworkEvent(NetworkEvent.Connect, m_Client1Events);
244+
if (m_Client2Events.Count == 0)
245+
{
246+
yield return WaitForNetworkEvent(NetworkEvent.Connect, m_Client2Events);
247+
}
236248

237249
// Ensure we got both Connect events.
238250
Assert.AreEqual(2, m_ServerEvents.Count);
@@ -252,7 +264,7 @@ public IEnumerator SendMultipleClients([ValueSource("k_DeliveryParameters")] Net
252264

253265
byte c1Data = m_Client1Events[1].Data.First();
254266
byte c2Data = m_Client2Events[1].Data.First();
255-
Assert.True((c1Data == 11 && c2Data == 22) || (c1Data == 22 && c2Data == 11));
267+
Assert.That((c1Data == 11 && c2Data == 22) || (c1Data == 22 && c2Data == 11));
256268

257269
yield return null;
258270
}
@@ -270,9 +282,10 @@ public IEnumerator ReceiveMultipleClients([ValueSource("k_DeliveryParameters")]
270282
m_Client2.StartClient();
271283

272284
yield return WaitForNetworkEvent(NetworkEvent.Connect, m_Client1Events);
273-
274-
// Ensure we got the Connect event on the other client too.
275-
Assert.AreEqual(1, m_Client2Events.Count);
285+
if (m_Client2Events.Count == 0)
286+
{
287+
yield return WaitForNetworkEvent(NetworkEvent.Connect, m_Client2Events);
288+
}
276289

277290
var data1 = new ArraySegment<byte>(new byte[] { 11 });
278291
m_Client1.Send(m_Client1.ServerClientId, data1, delivery);
@@ -288,9 +301,10 @@ public IEnumerator ReceiveMultipleClients([ValueSource("k_DeliveryParameters")]
288301

289302
byte sData1 = m_ServerEvents[2].Data.First();
290303
byte sData2 = m_ServerEvents[3].Data.First();
291-
Assert.True((sData1 == 11 && sData2 == 22) || (sData1 == 22 && sData2 == 11));
304+
Assert.That((sData1 == 11 && sData2 == 22) || (sData1 == 22 && sData2 == 11));
292305

293306
yield return null;
294307
}
295308
}
296309
}
310+
#endif

com.unity.netcode.gameobjects/Runtime/Messaging/Messages/NetworkVariableDeltaMessage.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public void Serialize(FastBufferWriter writer)
3636
// This var does not belong to the currently iterating delivery group.
3737
if (NetworkBehaviour.NetworkManager.NetworkConfig.EnsureNetworkVariableLengthSafety)
3838
{
39-
writer.WriteValueSafe((short)0);
39+
writer.WriteValueSafe((ushort)0);
4040
}
4141
else
4242
{
@@ -69,7 +69,12 @@ public void Serialize(FastBufferWriter writer)
6969
var tmpWriter = new FastBufferWriter(MessagingSystem.NON_FRAGMENTED_MESSAGE_MAX_SIZE, Allocator.Temp, short.MaxValue);
7070
NetworkBehaviour.NetworkVariableFields[k].WriteDelta(tmpWriter);
7171

72-
writer.WriteValueSafe((ushort)tmpWriter.Length);
72+
if (!writer.TryBeginWrite(FastBufferWriter.GetWriteSize<ushort>() + tmpWriter.Length))
73+
{
74+
throw new OverflowException($"Not enough space in the buffer to write {nameof(NetworkVariableDeltaMessage)}");
75+
}
76+
77+
writer.WriteValue((ushort)tmpWriter.Length);
7378
tmpWriter.CopyTo(writer);
7479
}
7580
else

0 commit comments

Comments
 (0)