From ba345a73ff9da98560bdfa527f961a85fd5d64de Mon Sep 17 00:00:00 2001 From: Ian Griffiths Date: Thu, 18 Jan 2024 13:15:41 +0000 Subject: [PATCH] Fixes two sources of intermittent errors: * the XUnit demo's random port selection now avoids ports that are in use * we have used all three ways of declaring that non-test projects really aren't test projects to prevent the 'testhost not found' errors the VS Test Explorer was sometimes reporting --- ...reFunctions.DemoFunctions.InProcess.csproj | 1 + ...ureFunctions.DemoFunctions.Isolated.csproj | 1 + ...sting.AzureFunctions.SpecFlow.NUnit.csproj | 5 ++++ ...vus.Testing.AzureFunctions.SpecFlow.csproj | 5 ++++ .../FunctionPerTestFacts.cs | 2 +- .../Corvus.Testing.AzureFunctions.csproj | 1 + .../AzureFunctions/FunctionsController.cs | 28 +++++++++++++++++++ .../Corvus.Testing.SpecFlow.NUnit.csproj | 5 ++++ .../Corvus.Testing.SpecFlow.csproj | 5 ++++ 9 files changed, 52 insertions(+), 1 deletion(-) diff --git a/Solutions/Corvus.Testing.AzureFunctions.DemoFunction.InProcess/Corvus.Testing.AzureFunctions.DemoFunctions.InProcess.csproj b/Solutions/Corvus.Testing.AzureFunctions.DemoFunction.InProcess/Corvus.Testing.AzureFunctions.DemoFunctions.InProcess.csproj index 3f8609b..27300fc 100644 --- a/Solutions/Corvus.Testing.AzureFunctions.DemoFunction.InProcess/Corvus.Testing.AzureFunctions.DemoFunctions.InProcess.csproj +++ b/Solutions/Corvus.Testing.AzureFunctions.DemoFunction.InProcess/Corvus.Testing.AzureFunctions.DemoFunctions.InProcess.csproj @@ -11,6 +11,7 @@ false + false diff --git a/Solutions/Corvus.Testing.AzureFunctions.DemoFunctions.Isolated/Corvus.Testing.AzureFunctions.DemoFunctions.Isolated.csproj b/Solutions/Corvus.Testing.AzureFunctions.DemoFunctions.Isolated/Corvus.Testing.AzureFunctions.DemoFunctions.Isolated.csproj index 4f17a35..0b86e04 100644 --- a/Solutions/Corvus.Testing.AzureFunctions.DemoFunctions.Isolated/Corvus.Testing.AzureFunctions.DemoFunctions.Isolated.csproj +++ b/Solutions/Corvus.Testing.AzureFunctions.DemoFunctions.Isolated/Corvus.Testing.AzureFunctions.DemoFunctions.Isolated.csproj @@ -12,6 +12,7 @@ false + false diff --git a/Solutions/Corvus.Testing.AzureFunctions.SpecFlow.NUnit/Corvus.Testing.AzureFunctions.SpecFlow.NUnit.csproj b/Solutions/Corvus.Testing.AzureFunctions.SpecFlow.NUnit/Corvus.Testing.AzureFunctions.SpecFlow.NUnit.csproj index 4c8dfa6..f483f9b 100644 --- a/Solutions/Corvus.Testing.AzureFunctions.SpecFlow.NUnit/Corvus.Testing.AzureFunctions.SpecFlow.NUnit.csproj +++ b/Solutions/Corvus.Testing.AzureFunctions.SpecFlow.NUnit/Corvus.Testing.AzureFunctions.SpecFlow.NUnit.csproj @@ -7,7 +7,12 @@ false + false + + + + Apache-2.0 diff --git a/Solutions/Corvus.Testing.AzureFunctions.SpecFlow/Corvus.Testing.AzureFunctions.SpecFlow.csproj b/Solutions/Corvus.Testing.AzureFunctions.SpecFlow/Corvus.Testing.AzureFunctions.SpecFlow.csproj index 456040d..1a4100d 100644 --- a/Solutions/Corvus.Testing.AzureFunctions.SpecFlow/Corvus.Testing.AzureFunctions.SpecFlow.csproj +++ b/Solutions/Corvus.Testing.AzureFunctions.SpecFlow/Corvus.Testing.AzureFunctions.SpecFlow.csproj @@ -10,7 +10,12 @@ false + false + + + + Apache-2.0 diff --git a/Solutions/Corvus.Testing.AzureFunctions.Xunit.Demo/FunctionPerTestFacts.cs b/Solutions/Corvus.Testing.AzureFunctions.Xunit.Demo/FunctionPerTestFacts.cs index 8d32906..2b29ab8 100644 --- a/Solutions/Corvus.Testing.AzureFunctions.Xunit.Demo/FunctionPerTestFacts.cs +++ b/Solutions/Corvus.Testing.AzureFunctions.Xunit.Demo/FunctionPerTestFacts.cs @@ -35,7 +35,7 @@ public FunctionPerTestFacts(ITestOutputHelper output) .CreateLogger("Xunit Demo tests"); this.function = new FunctionsController(logger); - this.Port = new Random().Next(50000, 60000); + this.Port = this.function.FindAvailableTcpPort(50000, 60000); } public int Port { get; } diff --git a/Solutions/Corvus.Testing.AzureFunctions/Corvus.Testing.AzureFunctions.csproj b/Solutions/Corvus.Testing.AzureFunctions/Corvus.Testing.AzureFunctions.csproj index 60c4273..f5df0bc 100644 --- a/Solutions/Corvus.Testing.AzureFunctions/Corvus.Testing.AzureFunctions.csproj +++ b/Solutions/Corvus.Testing.AzureFunctions/Corvus.Testing.AzureFunctions.csproj @@ -11,6 +11,7 @@ false + false diff --git a/Solutions/Corvus.Testing.AzureFunctions/Corvus/Testing/AzureFunctions/FunctionsController.cs b/Solutions/Corvus.Testing.AzureFunctions/Corvus/Testing/AzureFunctions/FunctionsController.cs index e63a52a..3667b3e 100644 --- a/Solutions/Corvus.Testing.AzureFunctions/Corvus/Testing/AzureFunctions/FunctionsController.cs +++ b/Solutions/Corvus.Testing.AzureFunctions/Corvus/Testing/AzureFunctions/FunctionsController.cs @@ -133,6 +133,34 @@ await Task.WhenAny( this.logger.LogInformation("Function {Path} now running on port {Port}", path, port); } + /// + /// Randomly selects a port that appears to be available for use. + /// + /// + /// The lowest port number acceptable. Defaults to 50000. + /// + /// + /// The port number above which no port will be selected. Defaults to 60000. + /// + /// A port number that seems to be available. + public int FindAvailableTcpPort(int? lowerBoundInclusive, int? upperBoundExclusive) + { + int lb = lowerBoundInclusive ?? 50000; + int ub = upperBoundExclusive ?? 60000; + + var portsInRangeInUse = IPGlobalProperties + .GetIPGlobalProperties() + .GetActiveTcpListeners() + .Select(e => e.Port) + .Where(p => p >= lb && p < ub) + .ToHashSet(); + + int availablePorts = ub - lb - portsInRangeInUse.Count; + int availablePortOffset = Random.Shared.Next(availablePorts); + int port = Enumerable.Range(lb, ub - lb).Where(p => !portsInRangeInUse.Contains(p)).ElementAt(availablePortOffset); + return port; + } + /// /// Provides access to the output. /// diff --git a/Solutions/Corvus.Testing.SpecFlow.NUnit/Corvus.Testing.SpecFlow.NUnit.csproj b/Solutions/Corvus.Testing.SpecFlow.NUnit/Corvus.Testing.SpecFlow.NUnit.csproj index 4c149cf..f3c2f17 100644 --- a/Solutions/Corvus.Testing.SpecFlow.NUnit/Corvus.Testing.SpecFlow.NUnit.csproj +++ b/Solutions/Corvus.Testing.SpecFlow.NUnit/Corvus.Testing.SpecFlow.NUnit.csproj @@ -7,7 +7,12 @@ false + false + + + + Apache-2.0 diff --git a/Solutions/Corvus.Testing.SpecFlow/Corvus.Testing.SpecFlow.csproj b/Solutions/Corvus.Testing.SpecFlow/Corvus.Testing.SpecFlow.csproj index 1e468be..474e832 100644 --- a/Solutions/Corvus.Testing.SpecFlow/Corvus.Testing.SpecFlow.csproj +++ b/Solutions/Corvus.Testing.SpecFlow/Corvus.Testing.SpecFlow.csproj @@ -10,7 +10,12 @@ false + false + + + + Apache-2.0