Skip to content

Commit 8265347

Browse files
test-update
Reverting back to always incrementing clients when running against a CMB server. Excluding a few more tests I missed previously. Fixing RpcProxyMessageTesting to actually run and test against the correct expected counts.
1 parent 328fdc0 commit 8265347

File tree

7 files changed

+102
-47
lines changed

7 files changed

+102
-47
lines changed

com.unity.netcode.gameobjects/TestHelpers/Runtime/NetcodeIntegrationTest.cs

Lines changed: 17 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ public enum HostOrServer
164164

165165
/// <summary>The Server <see cref="NetworkManager"/> instance instantiated and tracked within the current test</summary>
166166
protected NetworkManager m_ServerNetworkManager;
167+
167168
/// <summary>All the client <see cref="NetworkManager"/> instances instantiated and tracked within the current test</summary>
168169
protected NetworkManager[] m_ClientNetworkManagers;
169170
/// <summary>All the <see cref="NetworkManager"/> instances instantiated and tracked within the current test</summary>
@@ -251,14 +252,14 @@ private bool GetServiceEnvironmentVariable()
251252
{
252253
if (!m_UseCmbServiceEnv && m_UseCmbServiceEnvString == null)
253254
{
254-
var useCmbService = Environment.GetEnvironmentVariable("USE_CMB_SERVICE") ?? "false";
255-
if (bool.TryParse(useCmbService.ToLower(), out bool isTrue))
255+
m_UseCmbServiceEnvString = Environment.GetEnvironmentVariable("USE_CMB_SERVICE") ?? "false";
256+
if (bool.TryParse(m_UseCmbServiceEnvString.ToLower(), out bool isTrue))
256257
{
257258
m_UseCmbServiceEnv = isTrue;
258259
}
259260
else
260261
{
261-
Debug.LogWarning($"The USE_CMB_SERVICE ({useCmbService}) value is an invalid bool string. {m_UseCmbService} is being set to false.");
262+
Debug.LogWarning($"The USE_CMB_SERVICE ({m_UseCmbServiceEnvString}) value is an invalid bool string. {m_UseCmbService} is being set to false.");
262263
m_UseCmbServiceEnv = false;
263264
}
264265
}
@@ -592,21 +593,6 @@ protected virtual void OnServerAndClientsCreated()
592593
{
593594
}
594595

595-
/// <summary>
596-
/// Will create <see cref="NumberOfClients"/> number of clients.
597-
/// To create a specific number of clients <see cref="CreateServerAndClients(int)"/>
598-
/// </summary>
599-
protected void CreateServerAndClients()
600-
{
601-
// If we are connecting to a CMB server and we have a zero client count,
602-
// then we must make m_NumberOfClients = 1 for the session owner.
603-
if (m_UseCmbService && NumberOfClients == 0 && m_NumberOfClients == 0)
604-
{
605-
m_NumberOfClients = 1;
606-
}
607-
CreateServerAndClients(m_NumberOfClients);
608-
}
609-
610596
private void AddRemoveNetworkManager(NetworkManager networkManager, bool addNetworkManager)
611597
{
612598
var clientNetworkManagersList = new List<NetworkManager>(m_ClientNetworkManagers);
@@ -877,6 +863,15 @@ protected void SetTimeTravelSimulatedLatencyJitter(float jitterSeconds)
877863
}
878864
}
879865

866+
/// <summary>
867+
/// Will create <see cref="NumberOfClients"/> number of clients.
868+
/// To create a specific number of clients <see cref="CreateServerAndClients(int)"/>
869+
/// </summary>
870+
protected void CreateServerAndClients()
871+
{
872+
CreateServerAndClients(NumberOfClients);
873+
}
874+
880875
/// <summary>
881876
/// Creates the server and clients
882877
/// </summary>
@@ -892,23 +887,10 @@ protected void CreateServerAndClients(int numberOfClients)
892887
m_TargetFrameRate = -1;
893888
}
894889

895-
// In the event this is invoked within a derived integration test and
896-
// the number of clients is 0, then we need to have at least 1 client
897-
// to be the session owner.
898-
if (m_UseCmbService && numberOfClients == 0)
890+
// If we are connecting to a CMB server we add +1 for the session owner
891+
if (m_UseCmbService)
899892
{
900-
numberOfClients = 1;
901-
// If m_NumberOfCleints == 0, then we should increment it.
902-
if (m_NumberOfClients == 0)
903-
{
904-
m_NumberOfClients = 1;
905-
}
906-
else
907-
{
908-
// Otherwise, log a warning to the developer that they may be doing something bad.
909-
Debug.LogWarning($"[{nameof(CreateServerAndClients)}] Invoked with number of clients set to zero but m_NumberOfClients was {m_NumberOfClients}. " +
910-
$"Unless this was intended, this could cause issues with the {nameof(NetcodeIntegrationTest)}!");
911-
}
893+
numberOfClients++;
912894
}
913895

914896
// Create multiple NetworkManager instances
@@ -917,7 +899,7 @@ protected void CreateServerAndClients(int numberOfClients)
917899
Debug.LogError("Failed to create instances");
918900
Assert.Fail("Failed to create instances");
919901
}
920-
902+
m_NumberOfClients = numberOfClients;
921903
m_ClientNetworkManagers = clients;
922904
m_ServerNetworkManager = server;
923905

com.unity.netcode.gameobjects/Tests/Runtime/DistributedAuthority/RpcProxyMessageTesting.cs

Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Text;
44
using NUnit.Framework;
55
using Unity.Netcode.TestHelpers.Runtime;
6+
using UnityEngine.TestTools;
67

78
namespace Unity.Netcode.RuntimeTests
89
{
@@ -20,10 +21,17 @@ public class RpcProxyMessageTesting : NetcodeIntegrationTest
2021
{
2122
protected override int NumberOfClients => 2;
2223

23-
private List<RpcProxyText> m_ProxyTestInstances = new List<RpcProxyText>();
24+
private List<RpcProxyTest> m_ProxyTestInstances = new List<RpcProxyTest>();
2425

2526
private StringBuilder m_ValidationLogger = new StringBuilder();
2627

28+
protected override void OnPreInitializeConfiguration()
29+
{
30+
System.Environment.SetEnvironmentVariable("USE_CMB_SERVICE", "true");
31+
System.Environment.SetEnvironmentVariable("CMB_SERVICE_PORT", null);
32+
base.OnPreInitializeConfiguration();
33+
}
34+
2735
public RpcProxyMessageTesting(HostOrServer hostOrServer) : base(hostOrServer) { }
2836

2937
protected override IEnumerator OnSetup()
@@ -34,7 +42,7 @@ protected override IEnumerator OnSetup()
3442

3543
protected override void OnCreatePlayerPrefab()
3644
{
37-
m_PlayerPrefab.AddComponent<RpcProxyText>();
45+
m_PlayerPrefab.AddComponent<RpcProxyTest>();
3846
base.OnCreatePlayerPrefab();
3947
}
4048

@@ -44,28 +52,53 @@ private bool ValidateRpcProxyRpcs()
4452
m_ValidationLogger.Clear();
4553
foreach (var proxy in m_ProxyTestInstances)
4654
{
47-
if (proxy.ReceivedRpc.Count < NumberOfClients)
55+
56+
// Since we are sending to everyone but the authority, the local instance of each client's player should have zero
57+
// entries.
58+
if (proxy.ReceivedRpc.Count != 0)
4859
{
49-
m_ValidationLogger.AppendLine($"Not all clients received RPC from Client-{proxy.OwnerClientId}!");
60+
m_ValidationLogger.AppendLine($"Client-{proxy.OwnerClientId} sent itself an Rpc!");
5061
}
51-
foreach (var clientId in proxy.ReceivedRpc)
62+
foreach (var networkManager in m_NetworkManagers)
5263
{
53-
if (clientId == proxy.OwnerClientId)
64+
// Skip the local player instance
65+
if (networkManager.LocalClientId == proxy.OwnerClientId)
66+
{
67+
continue;
68+
}
69+
70+
// Get the cloned player instance of the player based on the player's NetworkObjectId
71+
if (!networkManager.SpawnManager.SpawnedObjects.ContainsKey(proxy.NetworkObjectId))
5472
{
55-
m_ValidationLogger.AppendLine($"Client-{proxy.OwnerClientId} sent itself an Rpc!");
73+
m_ValidationLogger.AppendLine($"Client-{networkManager.LocalClientId} does not have a cloned instance for Player-{proxy.OwnerClientId}!");
74+
}
75+
var clonedPlayer = networkManager.SpawnManager.SpawnedObjects[proxy.NetworkObjectId].GetComponent<RpcProxyTest>();
76+
// For each cloned player, each client should receive 1 RPC call per cloned player instance.
77+
// Example (With 3 clients including session owner):
78+
// Client-1 (SO): Sends to NotAuthority
79+
// Client-2: Should receive 1 RPC on its clone of Player-1
80+
// Client-3: Should receive 1 RPC on its clone of Player-1
81+
// Client-2: Sends to NotAuthority
82+
// Client-1: Should receive 1 RPC on its clone of Player-2
83+
// Client-3: Should receive 1 RPC on its clone of Player-2
84+
// Client-3: Sends to NotAuthority
85+
// Client-1: Should receive 1 RPC on its clone of Player-3
86+
// Client-2: Should receive 1 RPC on its clone of Player-3
87+
if (clonedPlayer.ReceivedRpc.Count != 1)
88+
{
89+
m_ValidationLogger.AppendLine($"[{clonedPlayer.name}] Received ({clonedPlayer.ReceivedRpc.Count}) RPCs when we were expected only 1!");
5690
}
5791
}
5892
}
5993
return m_ValidationLogger.Length == 0;
6094
}
6195

62-
96+
[UnityTest]
6397
public IEnumerator ProxyDoesNotInvokeOnSender()
6498
{
65-
m_ProxyTestInstances.Add(m_ServerNetworkManager.LocalClient.PlayerObject.GetComponent<RpcProxyText>());
66-
foreach (var client in m_ClientNetworkManagers)
99+
foreach (var client in m_NetworkManagers)
67100
{
68-
m_ProxyTestInstances.Add(client.LocalClient.PlayerObject.GetComponent<RpcProxyText>());
101+
m_ProxyTestInstances.Add(client.LocalClient.PlayerObject.GetComponent<RpcProxyTest>());
69102
}
70103

71104
foreach (var clientProxyTest in m_ProxyTestInstances)
@@ -77,7 +110,7 @@ public IEnumerator ProxyDoesNotInvokeOnSender()
77110
AssertOnTimeout(m_ValidationLogger.ToString());
78111
}
79112

80-
public class RpcProxyText : NetworkBehaviour
113+
public class RpcProxyTest : NetworkBehaviour
81114
{
82115
public List<ulong> ReceivedRpc = new List<ulong>();
83116

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
using NUnit.Framework;
2+
using Unity.Netcode.TestHelpers.Runtime;
23
using UnityEngine;
34

45
namespace Unity.Netcode.RuntimeTests
56
{
67
internal class NetworkManagerCustomMessageManagerTests
78
{
9+
[OneTimeSetUp]
10+
public void OneTimeSetup()
11+
{
12+
// This test does not need to run against the Rust server.
13+
NetcodeIntegrationTestHelpers.IgnoreIfServiceEnviromentVariableSet();
14+
}
15+
816
[Test]
917
public void CustomMessageManagerAssigned()
1018
{

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
using NUnit.Framework;
2+
using Unity.Netcode.TestHelpers.Runtime;
23
using UnityEngine;
34

45
namespace Unity.Netcode.RuntimeTests
56
{
67
internal class NetworkManagerSceneManagerTests
78
{
9+
[OneTimeSetUp]
10+
public void OneTimeSetup()
11+
{
12+
// This test does not need to run against the Rust server.
13+
NetcodeIntegrationTestHelpers.IgnoreIfServiceEnviromentVariableSet();
14+
}
15+
816
[Test]
917
public void SceneManagerAssigned()
1018
{

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@ namespace Unity.Netcode.RuntimeTests
1111
{
1212
internal class NetworkManagerTransportTests
1313
{
14+
[OneTimeSetUp]
15+
public void OneTimeSetup()
16+
{
17+
// This test does not need to run against the Rust server.
18+
NetcodeIntegrationTestHelpers.IgnoreIfServiceEnviromentVariableSet();
19+
}
20+
1421
[Test]
1522
public void ClientDoesNotStartWhenTransportFails()
1623
{

com.unity.netcode.gameobjects/Tests/Runtime/NetworkTransform/NetworkTransformStateTests.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#if !MULTIPLAYER_TOOLS
22
using NUnit.Framework;
33
using Unity.Netcode.Components;
4+
using Unity.Netcode.TestHelpers.Runtime;
45
using UnityEngine;
56

67

@@ -77,6 +78,13 @@ public enum Precision
7778
private Precision m_Precision;
7879
private Rotation m_Rotation;
7980

81+
[OneTimeSetUp]
82+
public void OneTimeSetup()
83+
{
84+
// This test does not need to run against the Rust server.
85+
NetcodeIntegrationTestHelpers.IgnoreIfServiceEnviromentVariableSet();
86+
}
87+
8088
public NetworkTransformStateTests(TransformSpace transformSpace, Precision precision, Rotation rotation)
8189
{
8290
m_TransformSpace = transformSpace;

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections;
33
using System.Linq;
44
using NUnit.Framework;
5+
using Unity.Netcode.TestHelpers.Runtime;
56
using UnityEngine;
67
using UnityEngine.LowLevel;
78
using UnityEngine.PlayerLoop;
@@ -11,6 +12,14 @@ namespace Unity.Netcode.RuntimeTests
1112
{
1213
internal class NetworkUpdateLoopTests
1314
{
15+
16+
[OneTimeSetUp]
17+
public void OneTimeSetup()
18+
{
19+
// This test does not need to run against the Rust server.
20+
NetcodeIntegrationTestHelpers.IgnoreIfServiceEnviromentVariableSet();
21+
}
22+
1423
[Test]
1524
public void RegisterCustomLoopInTheMiddle()
1625
{

0 commit comments

Comments
 (0)