More robust port assignment for tests.
This commit is contained in:
parent
f29dd60999
commit
95722670c1
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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"
|
||||
},
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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"
|
||||
}
|
||||
}
|
||||
|
|
@ -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
|
|||
/// </summary>
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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"
|
||||
},
|
||||
|
|
|
|||
Loading…
Reference in New Issue