3

我使用 OWIN 来自托管 Web API,同时使用 NCrunch并行运行我的测试,我在 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类在多个线程之间共享最后使用的端口,但它没有帮助。

这是我的测试基类:

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;
    }
}

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

4

1 回答 1

1

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

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

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

其次,问题是由于定义端口是否可用的错误逻辑引起的:之间Interlocked.ReadInterlocked.Increment其他线程可以进行相同的检查并返回相同的端口!例如:

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

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

  • 中使用CAS 操作 (您可以删除变量,类似这样(请自行测试此代码): CompareExchangeInterlockedport

    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;
    
于 2015-11-25T11:40:15.800 回答