1616
1717namespace 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