运行 并行测试时为 WebApi OWIN 自主机获取空闲端口

Get free port for WebApi OWIN self host when running tests in parallel

我正在使用 OWIN 自托管 Web API,而 运行 我使用 NCrunch 在 parallel 中进行测试,我在 BeforeEach 中启动它并在 AfterEach 方法中停止。

在每次测试之前,我都试图获得可用的空闲端口,但通常 85 次测试中有 5-10 次失败,但出现以下异常:

System.Net.HttpListenerException : Failed to listen on prefix  
'http://localhost:3369/' because it conflicts with an existing registration on the machine.

看来,有时我无法获得可用端口。我尝试使用 Interlocked class 以便在多个线程之间共享上次使用的端口,但没有帮助。

这是我的测试基础 class:

public class BaseSteps
{
    private const int PortRangeStart = 3368;
    private const int PortRangeEnd = 8968;
    private static long _portNumber = PortRangeStart;
    private IDisposable _webServer;

    //.....

    [BeforeScenario]
    public void Before()
    {
        Url = GetFullUrl();
        _webServer = WebApp.Start<TestStartup>(Url);
    }

    [AfterScenario]
    public void After()
    {
        _webServer.Dispose();
    }

    private static string GetFullUrl()
    {
        var ipAddress = IPAddress.Loopback;

        var portAvailable = GetAvailablePort(PortRangeStart, PortRangeEnd, ipAddress);

        return String.Format("http://{0}:{1}/", "localhost", portAvailable);
    }

    private static int GetAvailablePort(int rangeStart, int rangeEnd, IPAddress ip, bool includeIdlePorts = false)
    {
        IPGlobalProperties ipProps = IPGlobalProperties.GetIPGlobalProperties();

        // if the ip we want a port on is an 'any' or loopback port we need to exclude all ports that are active on any IP
        Func<IPAddress, bool> isIpAnyOrLoopBack = i => IPAddress.Any.Equals(i) ||
                                                       IPAddress.IPv6Any.Equals(i) ||
                                                       IPAddress.Loopback.Equals(i) ||
                                                       IPAddress.IPv6Loopback.
                                                           Equals(i);
        // get all active ports on specified IP.
        List<ushort> excludedPorts = new List<ushort>();

        // if a port is open on an 'any' or 'loopback' interface then include it in the excludedPorts
        excludedPorts.AddRange(from n in ipProps.GetActiveTcpConnections()
                               where
                                   n.LocalEndPoint.Port >= rangeStart &&
                                   n.LocalEndPoint.Port <= rangeEnd && (
                                   isIpAnyOrLoopBack(ip) || n.LocalEndPoint.Address.Equals(ip) ||
                                    isIpAnyOrLoopBack(n.LocalEndPoint.Address)) &&
                                    (!includeIdlePorts || n.State != TcpState.TimeWait)
                               select (ushort)n.LocalEndPoint.Port);

        excludedPorts.AddRange(from n in ipProps.GetActiveTcpListeners()
                               where n.Port >= rangeStart && n.Port <= rangeEnd && (
                               isIpAnyOrLoopBack(ip) || n.Address.Equals(ip) || isIpAnyOrLoopBack(n.Address))
                               select (ushort)n.Port);

        excludedPorts.AddRange(from n in ipProps.GetActiveUdpListeners()
                               where n.Port >= rangeStart && n.Port <= rangeEnd && (
                               isIpAnyOrLoopBack(ip) || n.Address.Equals(ip) || isIpAnyOrLoopBack(n.Address))
                               select (ushort)n.Port);

        excludedPorts.Sort();

        for (int port = rangeStart; port <= rangeEnd; port++)
        {
            if (!excludedPorts.Contains((ushort)port) && Interlocked.Read(ref _portNumber) < port)
            {
                Interlocked.Increment(ref _portNumber);

                return port;
            }
        }

        return 0;
    }
}

有谁知道如何确保我总是获得可用端口?

你的代码中的问题在这里:

if (!excludedPorts.Contains((ushort)port) && Interlocked.Read(ref _portNumber) < port)
{
    Interlocked.Increment(ref _portNumber);
    return port;
}

首先,您可以在每次测试开始时计算一次 excludedPorts,并将它们存储在某个静态字段中。

其次,问题是由定义端口是否可用的错误逻辑引起的:在 Interlocked.ReadInterlocked.Increment 之间,其他线程可以做同样的检查和 return 相同的端口!例如:

  1. 线程A:检查3369:它不在excludedPorts中,而_portNumber等于3368,所以检查通过。但是停下来,我会考虑一下...
  2. 线程B:检查3369:它不在excludedPorts中,而_portNumber等于3368,所以检查也通过了!哇,太激动了,让我们Increment吧,return3369
  3. 线程 A:好的,那我们去哪儿了?哦,是的,Increment和return3369!

典型的比赛条件。您可以通过两种方式解决它:

  • 使用CAS-operation CompareExchange from Interlocked class(你可以删除port变量,像这样(请自己测试这段代码):

    var portNumber = _portNumber;
    if (excludedPorts.Contains((ushort)portNumber))
    {
        // if port already taken
        continue;
    }
    if (Interlocked.CompareExchange(ref _portNumber, portNumber + 1, portNumber) != portNumber))
    {
        // if exchange operation failed, other thread passed through
        continue;
    }
    // only one thread can succeed
    return portNumber;
    
  • 使用静态 ConcurrentDictionary 端口,并向它们添加新端口,如下所示(您可以选择其他集合):

    // static field in your class
    // value item isn't useful
    static ConcurrentDictionary<int, bool>() ports = new ConcurrentDictionary<int, bool>();
    
    foreach (var p in excludedPorts)
        // you may check here is the adding the port succeed
        ports.TryAdd(p, true);
    var portNumber = _portNumber;
    if (!ports.TryAdd(portNumber, true))
    {
        continue;
    }
    return portNumber;