Skip to content

Using system given free ports in tests to remove transient test failures in CI #3969

Closed

Description

@veikkoeeva

Anedoctally I experienced multiple errors in opening sockets in CI server, which appeared to be due to some socket being already reserved.

As a first measure, I suggest adding a functionality to ask the system to give a free port from the ephemeral range and use that. The code could be like:

/// <summary>
/// Finds a free port from an ephemeral range.
/// </summary>
/// <returns>The found free port.</returns>
public static int GetFreePortFromEphemeralRange()
{
    var listener = new TcpListener(IPAddress.Loopback, 0);
    listener.Start();
    int port = ((IPEndPoint)listener.LocalEndpoint).Port;
    listener.Stop();

    return port;
}

When this change is implemented, it needs to be ensured all places are modified and the current code does not assume well known ports. Assuming all places are changed to use system given free ports, failing tests should reveal places that assume well known ports.

One option to refactor the tests could be to implement a cache, or a DI-like container to (lazily) generate and hold parameters that are then injected in all test class constructors. This could would allow sharing assets where feasible and perhaps make the testing infrastructure more uniform (and more approachable). There are some examples on how to do this at xunit/xunit#687.

Activity

  1. added this to the Triage milestone on Feb 6, 2018
  2. benjaminpetit commented on Jul 6, 2018

    @benjaminpetit
    Contributor

    A bit late but we already have something to take random unused port in TestClusterBuilder.GetAvailableConsecutiveServerPortsPair.

    Feel free to reopen if you think it is not sufficient!

  3. veikkoeeva commented on Jul 6, 2018

    @veikkoeeva
    ContributorAuthor

    @benjaminpetit This has been open for a while, so probably time to close.

    Though the logic looks like asking if a semi-random port is open and if not, retrying instead of just asking an open port. :)

  4. ghost locked as resolved and limited conversation to collaborators on Sep 27, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions