From 95722670c14855e3d5a59d482f01b9b38ed9dff1 Mon Sep 17 00:00:00 2001 From: Cesar Blum Silveira Date: Wed, 20 Apr 2016 17:25:50 -0700 Subject: [PATCH] More robust port assignment for tests. --- .../PortManager.cs | 4 +- .../project.json | 1 + .../PortManager.cs | 38 +++++++++++++++++++ .../project.json | 18 +++++++++ .../TestServer.cs | 5 +-- .../project.json | 1 + 6 files changed, 61 insertions(+), 6 deletions(-) create mode 100644 test/Microsoft.AspNetCore.Server.Kestrel.TestCommon/PortManager.cs create mode 100644 test/Microsoft.AspNetCore.Server.Kestrel.TestCommon/project.json diff --git a/test/Microsoft.AspNetCore.Server.Kestrel.FunctionalTests/PortManager.cs b/test/Microsoft.AspNetCore.Server.Kestrel.FunctionalTests/PortManager.cs index 55ba3577df..62e019d4be 100644 --- a/test/Microsoft.AspNetCore.Server.Kestrel.FunctionalTests/PortManager.cs +++ b/test/Microsoft.AspNetCore.Server.Kestrel.FunctionalTests/PortManager.cs @@ -7,11 +7,9 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests { public static class PortManager { - private static int _nextPort = 8001; - public static int GetPort() { - return Interlocked.Increment(ref _nextPort); + return TestCommon.PortManager.GetNextPort(); } } } diff --git a/test/Microsoft.AspNetCore.Server.Kestrel.FunctionalTests/project.json b/test/Microsoft.AspNetCore.Server.Kestrel.FunctionalTests/project.json index f925e40df7..5eab8dffcd 100644 --- a/test/Microsoft.AspNetCore.Server.Kestrel.FunctionalTests/project.json +++ b/test/Microsoft.AspNetCore.Server.Kestrel.FunctionalTests/project.json @@ -4,6 +4,7 @@ "Microsoft.NETCore.Platforms": "1.0.1-*", "Microsoft.AspNetCore.Http.Abstractions": "1.0.0-*", "Microsoft.AspNetCore.Server.Kestrel": "1.0.0-*", + "Microsoft.AspNetCore.Server.Kestrel.TestCommon": "1.0.0-*", "Microsoft.AspNetCore.Testing": "1.0.0-*", "xunit": "2.1.0" }, diff --git a/test/Microsoft.AspNetCore.Server.Kestrel.TestCommon/PortManager.cs b/test/Microsoft.AspNetCore.Server.Kestrel.TestCommon/PortManager.cs new file mode 100644 index 0000000000..a90f229963 --- /dev/null +++ b/test/Microsoft.AspNetCore.Server.Kestrel.TestCommon/PortManager.cs @@ -0,0 +1,38 @@ +using System.Net; +using System.Net.Sockets; + +namespace Microsoft.AspNetCore.Server.Kestrel.TestCommon +{ + public static class PortManager + { + public static int _nextPort = 8001; + public static object _portLock = new object(); + + public static int GetNextPort() + { + lock (_portLock) + { + using (var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) + { + while (true) + { + try + { + var port = _nextPort++; + socket.Bind(new IPEndPoint(IPAddress.Loopback, port)); + return port; + } + catch (SocketException) + { + // Retry unless exhausted + if (_nextPort == 65536) + { + throw; + } + } + } + } + } + } + } +} \ No newline at end of file diff --git a/test/Microsoft.AspNetCore.Server.Kestrel.TestCommon/project.json b/test/Microsoft.AspNetCore.Server.Kestrel.TestCommon/project.json new file mode 100644 index 0000000000..2795fb5773 --- /dev/null +++ b/test/Microsoft.AspNetCore.Server.Kestrel.TestCommon/project.json @@ -0,0 +1,18 @@ +{ + "version": "1.0.0-*", + "frameworks": { + "net451": { }, + "netstandard1.3": { + "dependencies": { + "System.Threading": "4.0.11-*", + "System.Net.Sockets": "4.1.0-*" + }, + "imports": [ + "portable-net45+win8" + ] + } + }, + "compilationOptions": { + "keyFile": "../../tools/Key.snk" + } +} \ No newline at end of file diff --git a/test/Microsoft.AspNetCore.Server.KestrelTests/TestServer.cs b/test/Microsoft.AspNetCore.Server.KestrelTests/TestServer.cs index 2e51e27b3e..69b876cbf1 100644 --- a/test/Microsoft.AspNetCore.Server.KestrelTests/TestServer.cs +++ b/test/Microsoft.AspNetCore.Server.KestrelTests/TestServer.cs @@ -6,6 +6,7 @@ using System.Threading; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Server.Kestrel; using Microsoft.AspNetCore.Server.Kestrel.Http; +using Microsoft.AspNetCore.Server.Kestrel.TestCommon; namespace Microsoft.AspNetCore.Server.KestrelTests { @@ -14,8 +15,6 @@ namespace Microsoft.AspNetCore.Server.KestrelTests /// public class TestServer : IDisposable { - private static int _nextPort = 9001; - private KestrelEngine _engine; private IDisposable _server; ServerAddress _address; @@ -62,7 +61,7 @@ namespace Microsoft.AspNetCore.Server.KestrelTests public static int GetNextPort() { - return Interlocked.Increment(ref _nextPort); + return PortManager.GetNextPort(); } } } \ No newline at end of file diff --git a/test/Microsoft.AspNetCore.Server.KestrelTests/project.json b/test/Microsoft.AspNetCore.Server.KestrelTests/project.json index 44cbe4df45..c0237faec2 100644 --- a/test/Microsoft.AspNetCore.Server.KestrelTests/project.json +++ b/test/Microsoft.AspNetCore.Server.KestrelTests/project.json @@ -4,6 +4,7 @@ "Microsoft.NETCore.Platforms": "1.0.1-*", "Microsoft.AspNetCore.Server.Kestrel": "1.0.0-*", "Microsoft.AspNetCore.Server.Kestrel.Https": "1.0.0-*", + "Microsoft.AspNetCore.Server.Kestrel.TestCommon": "1.0.0-*", "Microsoft.AspNetCore.Testing": "1.0.0-*", "xunit": "2.1.0" },