Skip to content
This repository was archived by the owner on Jan 18, 2022. It is now read-only.

Commit 97a4cc6

Browse files
author
Jamie Brynes
authored
Add player connection port parsing tests (#1139)
1 parent 4dbd879 commit 97a4cc6

File tree

3 files changed

+75
-9
lines changed

3 files changed

+75
-9
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using System;
2+
using NUnit.Framework;
3+
4+
namespace Improbable.Gdk.Core.EditmodeTests
5+
{
6+
[TestFixture]
7+
public class PlayerConnectionPortTests
8+
{
9+
private const string CorrectTemplate = "PlayerConnection initialized network socket : 0.0.0.0 {0}";
10+
11+
[Test]
12+
public void ExtractPlayerConnectionPort_throws_exception_if_no_match()
13+
{
14+
var noPortHere = "There isn't a port here.\nYou really shouldn't even bother looking.";
15+
Assert.Throws<Exception>(() => WorkerConnector.ExtractPlayerConnectionPort(noPortHere));
16+
}
17+
18+
[Test]
19+
public void ExtractPlayerConnectionPort_finds_port_regardless_of_position_in_file()
20+
{
21+
const string validPort = "5555";
22+
23+
// First start with a single line.
24+
var exampleFile = string.Format(CorrectTemplate, validPort);
25+
Assert.AreEqual(ushort.Parse(validPort), WorkerConnector.ExtractPlayerConnectionPort(exampleFile));
26+
27+
// Put something in front.
28+
exampleFile = $"Something in front!\n{exampleFile}";
29+
Assert.AreEqual(ushort.Parse(validPort), WorkerConnector.ExtractPlayerConnectionPort(exampleFile));
30+
31+
// Put something behind.
32+
exampleFile = $"{exampleFile}\nSomething behind!";
33+
Assert.AreEqual(ushort.Parse(validPort), WorkerConnector.ExtractPlayerConnectionPort(exampleFile));
34+
}
35+
36+
[TestCase("0", typeof(Exception))]
37+
[TestCase("70000", typeof(OverflowException))]
38+
public void ExtractPlayerConnectionPort_throws_exception_if_invalid_port(string port, Type exceptionType)
39+
{
40+
var file = string.Format(CorrectTemplate, port);
41+
Assert.Throws(exceptionType, () => WorkerConnector.ExtractPlayerConnectionPort(file));
42+
}
43+
}
44+
}

workers/unity/Packages/io.improbable.gdk.core/Tests/Editmode/Utility/PlayerConnectionPortTests.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

workers/unity/Packages/io.improbable.gdk.core/Worker/WorkerConnector.cs

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ private void RemoveFromPlayerLoop()
253253
}
254254
}
255255

256-
private ushort GetPlayerConnectionPort()
256+
private static ushort GetPlayerConnectionPort()
257257
{
258258
var companyName = Application.companyName;
259259
var productName = Application.productName;
@@ -287,19 +287,30 @@ private ushort GetPlayerConnectionPort()
287287
using (var readStream = new StreamReader(stream))
288288
{
289289
var logContents = readStream.ReadToEnd();
290+
return ExtractPlayerConnectionPort(logContents);
291+
}
292+
}
290293

291-
const string portRegex =
292-
"PlayerConnection initialized network socket : [0-9]\\.[0-9]\\.[0-9]\\.[0-9] ([0-9]*)";
294+
internal static ushort ExtractPlayerConnectionPort(string fileContents)
295+
{
296+
const string portRegex =
297+
"PlayerConnection initialized network socket : 0\\.0\\.0\\.0 ([0-9]+)";
293298

294-
var regex = new Regex(portRegex, RegexOptions.Compiled);
299+
var regex = new Regex(portRegex, RegexOptions.Compiled);
295300

296-
if (!regex.IsMatch(logContents))
297-
{
298-
throw new Exception("Could not find PlayerConnection port in logfile");
299-
}
301+
if (!regex.IsMatch(fileContents))
302+
{
303+
throw new Exception("Could not find PlayerConnection port in logfile");
304+
}
305+
306+
var port = ushort.Parse(regex.Match(fileContents).Groups[1].Value);
300307

301-
return ushort.Parse(regex.Match(logContents).Groups[1].Value);
308+
if (port == 0)
309+
{
310+
throw new Exception("PlayerConnection port cannot be 0.");
302311
}
312+
313+
return port;
303314
}
304315
}
305316
}

0 commit comments

Comments
 (0)