Get free port starting from 8001 instead of using TcpListener.

Enable kestrel tests to run in parallel.
This commit is contained in:
Sridhar Periyasamy 2016-04-05 13:12:07 -07:00
parent 17b7d34bb9
commit dbb25d8fb3
4 changed files with 18 additions and 35 deletions

View file

@ -12,8 +12,6 @@ using Xunit;
using System.Threading.Tasks;
using FluentAssertions;
[assembly: CollectionBehavior(DisableTestParallelization = true)]
namespace Microsoft.DotNet.Kestrel.Tests
{
public class DotnetTest : TestBase

View file

@ -2,11 +2,8 @@ using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.DotNet.ProjectModel;
using FluentAssertions;
namespace Microsoft.DotNet.Tools.Test.Utilities
@ -16,8 +13,6 @@ namespace Microsoft.DotNet.Tools.Test.Utilities
// in milliseconds
private const int Timeout = 20000;
private static Queue<TcpListener> s_PortPool = new Queue<TcpListener>();
public static string Localhost { get; } = "http://localhost";
public static bool IsServerUp(string url)
@ -55,36 +50,9 @@ namespace Microsoft.DotNet.Tools.Test.Utilities
}
}
public static int GetFreePort()
{
lock (s_PortPool)
{
if (s_PortPool.Count == 0)
{
for (int i = 0; i < 20; i++)
{
var tcpl = new TcpListener(IPAddress.Loopback, 0);
tcpl.Start();
s_PortPool.Enqueue(tcpl);
}
Console.WriteLine($"Ports Count >>>>>>>>>>>>>>>>>>> {s_PortPool.Count}");
}
var currentTcpl = s_PortPool.Dequeue();
var port = ((IPEndPoint)currentTcpl.LocalEndpoint).Port;
currentTcpl.Stop();
currentTcpl = null;
GC.Collect();
GC.WaitForPendingFinalizers();
return port;
}
}
public static string GetLocalhostUrlWithFreePort()
{
return $"{Localhost}:{GetFreePort()}/";
return $"{Localhost}:{PortManager.GetPort()}/";
}
}
}

View file

@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Threading;
namespace Microsoft.DotNet.Tools.Test.Utilities
{
public static class PortManager
{
private static int s_nextPort = 8001;
public static int GetPort()
{
return Interlocked.Increment(ref s_nextPort);
}
}
}

View file

@ -8,6 +8,7 @@
"Microsoft.NETCore.App": "1.0.0-rc2-23931",
"System.Runtime.Serialization.Primitives": "4.1.1-rc2-23931",
"System.Collections.Immutable": "1.2.0-rc2-23931",
"System.Net.NetworkInformation": "4.1.0-rc2-23931",
"FluentAssertions": "4.0.0",
"xunit": "2.1.0",
"dotnet-test-xunit": "1.0.0-dev-128011-22",