Skip to content

Commit 08e09c2

Browse files
authored
test: Added ability to switch the transport to UTP from the command line (#1991)
1 parent cf7f737 commit 08e09c2

File tree

6 files changed

+137
-18
lines changed

6 files changed

+137
-18
lines changed

testproject/Assets/Scripts/CommandLineHandler.cs

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,13 @@ public class CommandLineProcessor
2525
private string m_SceneToLoad;
2626
private Dictionary<string, string> m_CommandLineArguments = new Dictionary<string, string>();
2727
private ConnectionModeScript m_ConnectionModeScript;
28+
29+
public string TransportAddress;
30+
public string TransportPort;
31+
2832
public CommandLineProcessor(string[] args)
2933
{
34+
Debug.Log("CommandLineProcessor constructor called");
3035
try
3136
{
3237
if (s_Singleton != null)
@@ -54,7 +59,6 @@ public CommandLineProcessor(string[] args)
5459
m_CommandLineArguments.Add(arg, value);
5560
}
5661
}
57-
5862
ProcessCommandLine();
5963
}
6064

@@ -90,6 +94,13 @@ public void ProcessCommandLine()
9094
}
9195
}
9296

97+
if (m_CommandLineArguments.TryGetValue("-transport", out string transportName))
98+
{
99+
Debug.Log($"Setting transport to {transportName}");
100+
SetTransport(transportName);
101+
m_CommandLineArguments.Remove("-transport");
102+
}
103+
93104
if (m_CommandLineArguments.TryGetValue("-ip", out string ipValue))
94105
{
95106
SetTransportAddress(ipValue);
@@ -98,6 +109,7 @@ public void ProcessCommandLine()
98109

99110
if (m_CommandLineArguments.TryGetValue("-p", out string port))
100111
{
112+
TransportPort = port;
101113
SetPort(ushort.Parse(port));
102114
m_CommandLineArguments.Remove("-p");
103115
}
@@ -110,6 +122,7 @@ public void ProcessCommandLine()
110122

111123
if (m_CommandLineArguments.TryGetValue("-m", out string netcodeValue))
112124
{
125+
Debug.Log($"Starting {netcodeValue}");
113126
switch (netcodeValue)
114127
{
115128
case "server":
@@ -197,8 +210,44 @@ private void StartClient()
197210
}
198211
}
199212

213+
private void SetTransport(string transportName)
214+
{
215+
if (transportName.Equals("utp", StringComparison.CurrentCultureIgnoreCase))
216+
{
217+
AddUnityTransport(NetworkManager.Singleton);
218+
}
219+
#if UNITY_UNET_PRESENT
220+
else if (transportName.Equals("unet", StringComparison.CurrentCultureIgnoreCase))
221+
{
222+
// Do nothing, this is the default
223+
}
224+
#endif
225+
226+
}
227+
228+
private static void AddUnityTransport(NetworkManager networkManager)
229+
{
230+
// Create transport
231+
var unityTransport = networkManager.gameObject.AddComponent<UnityTransport>();
232+
// We need to increase this buffer size for tests that spawn a bunch of things
233+
unityTransport.MaxPayloadSize = 256000;
234+
unityTransport.MaxSendQueueSize = 1024 * 1024;
235+
236+
// Allow 4 connection attempts that each will time out after 500ms
237+
unityTransport.MaxConnectAttempts = 4;
238+
unityTransport.ConnectTimeoutMS = 500;
239+
240+
// Set the NetworkConfig
241+
networkManager.NetworkConfig = new NetworkConfig()
242+
{
243+
// Set transport
244+
NetworkTransport = unityTransport
245+
};
246+
}
247+
200248
private void SetTransportAddress(string address)
201249
{
250+
TransportAddress = address;
202251
var transport = NetworkManager.Singleton.NetworkConfig.NetworkTransport;
203252
switch (transport)
204253
{

testproject/Assets/Tests/Runtime/MultiprocessRuntime/Helpers/MultiprocessOrchestration.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ public static string StartWorkerNode()
140140
workerProcess.StartInfo.UseShellExecute = false;
141141
workerProcess.StartInfo.RedirectStandardError = true;
142142
workerProcess.StartInfo.RedirectStandardOutput = true;
143-
workerProcess.StartInfo.Arguments = $"{IsWorkerArg} {extraArgs} -logFile {logPath} -s {BuildMultiprocessTestPlayer.MainSceneName}";
143+
workerProcess.StartInfo.Arguments = $"{IsWorkerArg} {extraArgs} -logFile {logPath}";
144144

145145
try
146146
{

testproject/Assets/Tests/Runtime/MultiprocessRuntime/TestCoordinator.cs

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,23 +79,57 @@ public void Awake()
7979
MultiprocessLogger.Log($"Awake {s_ProcessId}");
8080
ReadGitHashFile();
8181

82-
// There are two possible ways for configuration data to be set
83-
// 1. From a webapi
84-
// 2. From a text file resource
82+
// Configuration via command line (supported for many but not all platforms)
8583
bool isClient = Environment.GetCommandLineArgs().Any(value => value == MultiprocessOrchestration.IsWorkerArg);
8684
if (isClient)
8785
{
86+
MultiprocessLogger.Log("Setting up via command line - client");
8887
m_IsClient = isClient;
88+
var cli = new CommandLineProcessor(Environment.GetCommandLineArgs());
89+
if (Environment.GetCommandLineArgs().Any(value => value == "-ip"))
90+
{
91+
m_ConnectAddress = cli.TransportAddress;
92+
}
93+
if (Environment.GetCommandLineArgs().Any(value => value == "-p"))
94+
{
95+
Port = cli.TransportPort;
96+
}
8997
SetConfigurationTypeAndConnect(ConfigurationType.CommandLine);
9098
}
9199

100+
if (ConfigurationType == ConfigurationType.Unknown)
101+
{
102+
bool isHost = Environment.GetCommandLineArgs().Any(value => value == "host");
103+
if (isHost)
104+
{
105+
MultiprocessLogger.Log("Setting up via command line - host");
106+
var cli = new CommandLineProcessor(Environment.GetCommandLineArgs());
107+
ConfigurationType = ConfigurationType.CommandLine;
108+
}
109+
}
110+
111+
112+
// Configuration via configuration file - all platform support but set at build time
113+
if (ConfigurationType == ConfigurationType.Unknown)
114+
{
115+
//TODO: For next PR
116+
}
117+
118+
// configuration via WebApi - works on all platforms and is set at run time
92119
if (ConfigurationType == ConfigurationType.Unknown)
93120
{
94121
MultiprocessLogger.Log($"Awake {s_ProcessId} - Calling ConfigureViewWebApi");
95122
ConfigureViaWebApi();
96123
MultiprocessLogger.Log($"Awake {s_ProcessId} - Calling ConfigureViewWebApi completed");
97124
}
98125

126+
127+
// if we've tried all the configuration types and none of them are correct then we should throw an exception
128+
if (ConfigurationType == ConfigurationType.Unknown)
129+
{
130+
throw new Exception("Unable to determine configuration for NetworkManager via commandline, webapi or config file");
131+
}
132+
99133
if (Instance != null)
100134
{
101135
MultiprocessLogger.LogError("Multiple test coordinator, destroying this instance");

testproject/Assets/Tests/Runtime/MultiprocessRuntime/ThreeDText.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public void Update()
8383
t.text = $"On Update -\ntestCoordinator.isActiveAndEnabled:{testCoordinator.isActiveAndEnabled} {testCoordinator.ConfigurationType}\n" +
8484
$"Transport: {transportString}\n" +
8585
$"{CommandLineArguments}\n" +
86-
$"IsHost: {NetworkManager.Singleton.IsHost} IsClient: {NetworkManager.Singleton.IsClient}\n" +
86+
$"IsHost: {NetworkManager.Singleton.IsHost} IsClient: {NetworkManager.Singleton.IsClient} {NetworkManager.Singleton.IsConnectedClient}\n" +
8787
$"{m_UpdateCounter}\n";
8888
}
8989
}

testproject/Assets/Tests/Runtime/MultiprocessRuntime/readme.md

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,10 @@ Tests when launched locally will simply create new OS processes for each worker
235235
![](readme-ressources/OrchestrationOverview.jpg)
236236
*Note that this diagram is still WIP for the CI part*
237237
### Bokken orchestration
238-
todo
238+
Bokken Orchestration can be performed with the support of the following tool:
239+
[Multiplayer Multiprocess Test Tools](https://github.cds.internal.unity3d.com/unity/multiplayer-multiprocess-test-tools)
240+
[Documentation](https://backstage.corp.unity3d.com/catalog/default/component/multiplayer-multiprocess-test-tools/docs/)
241+
239242
### CI
240243
todo
241244
#### Performance report dashboards
@@ -247,6 +250,30 @@ The test coordinator in client mode will automatically try to connect to a serve
247250
Test methods are executed twice. Once in "registration" mode, to have all the steps register themselves using a unique ID. This ID is deterministic between client and server processes, so that when a server calls a step during actual test execution, the clients have the same ID associated with the same lambda.
248251
During test execution, the main node's step will call an RPC on clients to trigger their pre-registered lambda. The main node's step will then yield until it receives a "client done" RPC from all clients. The main node's test will then be able to continue execution to the next step.
249252

253+
## The MultiprocessTestPlayer
254+
The MultiprocessTestPlayer, which is built by the sub-menu items under "Netcode" -> "Multiprocess Test", supports many workflows and configurations.
255+
256+
### Command Line
257+
Currently the MultiprocessTestPlayer can be configured via the command line on all platforms that support parsing of command line arguments in the C# layer.
258+
For example, this means that command line configuration is not available on Android.
259+
260+
#### Setting the transport address
261+
Default Values on Client: 127.0.0.1, 3076
262+
263+
In order to set the transport address for either the server/host or the client, the options of "-ip" and "-p" can be used. For example:
264+
265+
-ip 127.0.0.1 -p 3076
266+
267+
These options can be passed when starting the client, for example, in order to let it know where the host is to connect to.
268+
269+
#### Setting the transport
270+
Default Values: UNET
271+
272+
The default transport is UNET but this can be switched to UTP by using
273+
274+
-transport utp
275+
276+
250277
# Future considerations
251278
- Integrate with local MultiInstance tests?
252279
- Have ExecuteStepInContext a game facing feature for sequencing client-server actions?
Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,27 @@
11
{
22
"name": "TestProject.MultiprocessTests",
3+
"rootNamespace": "",
34
"references": [
45
"Unity.Netcode.Runtime",
6+
"TestProject",
7+
"Unity.Netcode.Editor",
8+
"Unity.Netcode.Components",
59
"ScriptsForAutomatedTesting",
6-
"Unity.PerformanceTesting"
10+
"Unity.PerformanceTesting",
11+
"UnityEngine.TestRunner",
12+
"UnityEditor.TestRunner"
713
],
8-
"optionalUnityReferences": [
9-
"TestAssemblies"
14+
"includePlatforms": [],
15+
"excludePlatforms": [],
16+
"allowUnsafeCode": true,
17+
"overrideReferences": false,
18+
"precompiledReferences": [
19+
"nunit.framework.dll"
1020
],
11-
"includePlatforms": [
12-
"Editor",
13-
"macOSStandalone",
14-
"LinuxStandalone64",
15-
"WindowsStandalone32",
16-
"WindowsStandalone64"
17-
]
18-
}
21+
"autoReferenced": true,
22+
"defineConstraints": [
23+
"UNITY_INCLUDE_TESTS"
24+
],
25+
"versionDefines": [],
26+
"noEngineReferences": false
27+
}

0 commit comments

Comments
 (0)