Skip to content

Commit b5555cb

Browse files
committed
Add ENSURE_CODEC_TESTS environment variable
1 parent 3289148 commit b5555cb

File tree

2 files changed

+40
-10
lines changed

2 files changed

+40
-10
lines changed

.yamato/desktop-standalone-tests.yml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,12 @@ desktop_standalone_test_{{ project.name }}_{{ platform.name }}_{{ backend }}_{{
7777
image: {{ platform.image }}
7878
flavor: {{ platform.flavor }}
7979
variables:
80+
{% if platform.name != "win" %}
8081
ECHO_SERVER_PORT: "7788"
82+
# Ensure the DA codec tests will fail if they cannot connect to the echo-server
83+
# The default is to ignore the codec tests if the echo-server fails to connect
84+
ENSURE_CODEC_TESTS: "true"
85+
{% endif %}
8186
commands:
8287
# Platform specific UTR setup
8388
- |
@@ -91,9 +96,8 @@ desktop_standalone_test_{{ project.name }}_{{ platform.name }}_{{ backend }}_{{
9196
- unity-downloader-cli -u {{ editor }} -c Editor {% if backend == "il2cpp" %} -c il2cpp {% endif %} --fast --wait
9297

9398
# If ubuntu, run rust echo server
94-
{% if platform.name == "ubuntu" %}
95-
- |
96-
git clone https://github.com/Unity-Technologies/mps-common-multiplayer-backend.git
99+
{% if platform.name != "win" %}
100+
- git clone https://github.com/Unity-Technologies/mps-common-multiplayer-backend.git
97101
# Install rust
98102
- curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y
99103
# Build the echo server

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

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,20 @@
1616

1717
namespace Unity.Netcode.RuntimeTests
1818
{
19+
/// <summary>
20+
/// This class tests the NGO message codec between the C# SDK and the Rust runtime.
21+
/// </summary>
22+
/// <remarks>
23+
/// These tests are run against a rust echo-server.
24+
/// This server decodes incoming messages, and then re-encodes them before sending them back to the client.
25+
/// Any errors in decoding or encoding messages will not echo the messages back, causing the tests to fail
26+
/// No message handling logic is tested in these tests. They are only testing the codec.
27+
/// The tests check if they can bind to a rust echo-server at the given address and port, if all tests are ignored.
28+
/// The rust echo-server is run using `cargo run --example ngo_echo_server -- --port {port}`
29+
/// The C# port can be configured using the environment variable "ECHO_SERVER_PORT"
30+
/// The default behaviour when unity fails to connect to the echo-server is to ignore all tests in this class.
31+
/// This can be overridden by setting the environment variable "ENSURE_CODEC_TESTS" to any value - then the tests will fail.
32+
/// </remarks>
1933
internal class DistributedAuthorityCodecTests : NetcodeIntegrationTest
2034
{
2135
protected override int NumberOfClients => 1;
@@ -30,21 +44,25 @@ internal class DistributedAuthorityCodecTests : NetcodeIntegrationTest
3044
private NetworkManager Client => m_ClientNetworkManagers[0];
3145

3246
private string m_TransportHost = Environment.GetEnvironmentVariable("NGO_HOST") ?? "127.0.0.1";
33-
private static readonly ushort k_TransportPort = GetPortToBind(7777);
47+
private static readonly ushort k_TransportPort = GetPortToBind();
3448
private const int k_ClientId = 0;
3549

36-
private static ushort GetPortToBind(ushort defaultPort)
50+
/// <summary>
51+
/// Configures the port to look for the rust echo-server.
52+
/// </summary>
53+
/// <returns>The port from the environment variable "ECHO_SERVER_PORT" if it is set and valid; otherwise uses port 7777</returns>
54+
private static ushort GetPortToBind()
3755
{
3856
var value = Environment.GetEnvironmentVariable("ECHO_SERVER_PORT");
39-
return ushort.TryParse(value, out var configuredPort) ? configuredPort : defaultPort;
57+
return ushort.TryParse(value, out var configuredPort) ? configuredPort : (ushort)7777;
4058
}
4159

4260
private GameObject m_SpawnObject;
4361

4462
internal class TestNetworkComponent : NetworkBehaviour
4563
{
4664
public NetworkList<int> MyNetworkList = new NetworkList<int>(new List<int> { 1, 2, 3 });
47-
public NetworkVariable<int> MyNetworkVar = new NetworkVariable<int>(3);
65+
public NetworkVariable<int> myNetworkVar = new NetworkVariable<int>(3);
4866

4967
[Rpc(SendTo.Authority)]
5068
public void TestAuthorityRpc(byte[] _)
@@ -60,7 +78,15 @@ protected override void OnOneTimeSetup()
6078
#else
6179
if (!CanConnectToServer(m_TransportHost, k_TransportPort))
6280
{
63-
Assert.Ignore($"ignoring DA codec tests because UTP transport cannot connect to the rust echo-server at ${m_TransportHost}:{k_TransportPort}");
81+
var shouldFail = Environment.GetEnvironmentVariable("ENSURE_CODEC_TESTS");
82+
if (!string.IsNullOrEmpty(shouldFail))
83+
{
84+
Assert.Fail($"Failed to connect to the rust echo-server at {m_TransportHost}:{k_TransportPort}");
85+
}
86+
else
87+
{
88+
Assert.Ignore($"ignoring DA codec tests because UTP transport cannot connect to the rust echo-server at {m_TransportHost}:{k_TransportPort}");
89+
}
6490
}
6591
#endif
6692
base.OnOneTimeSetup();
@@ -234,9 +260,9 @@ public IEnumerator NetworkVariableDelta_WithValueUpdate()
234260
var component = instance.GetComponent<TestNetworkComponent>();
235261

236262
var newValue = 5;
237-
component.MyNetworkVar.Value = newValue;
263+
component.myNetworkVar.Value = newValue;
238264
yield return m_ClientCodecHook.WaitForMessageReceived<NetworkVariableDeltaMessage>();
239-
Assert.AreEqual(newValue, component.MyNetworkVar.Value);
265+
Assert.AreEqual(newValue, component.myNetworkVar.Value);
240266
}
241267

242268
[UnityTest]

0 commit comments

Comments
 (0)