Skip to content

Commit dbcf1d2

Browse files
authored
Fixes two sources of intermittent errors: (#430)
* 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
1 parent 7368f7d commit dbcf1d2

File tree

9 files changed

+52
-1
lines changed

9 files changed

+52
-1
lines changed

Solutions/Corvus.Testing.AzureFunctions.DemoFunction.InProcess/Corvus.Testing.AzureFunctions.DemoFunctions.InProcess.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
<PropertyGroup>
1313
<IsTestProject>false</IsTestProject>
14+
<TestProject>false</TestProject>
1415
</PropertyGroup>
1516

1617
<PropertyGroup>

Solutions/Corvus.Testing.AzureFunctions.DemoFunctions.Isolated/Corvus.Testing.AzureFunctions.DemoFunctions.Isolated.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
<PropertyGroup>
1414
<IsTestProject>false</IsTestProject>
15+
<TestProject>false</TestProject>
1516
</PropertyGroup>
1617

1718
<ItemGroup>

Solutions/Corvus.Testing.AzureFunctions.SpecFlow.NUnit/Corvus.Testing.AzureFunctions.SpecFlow.NUnit.csproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@
77

88
<PropertyGroup>
99
<IsTestProject>false</IsTestProject>
10+
<TestProject>false</TestProject>
1011
</PropertyGroup>
12+
<ItemGroup>
13+
<!-- This project takes depedency on nUnit that adds the TestContainer capability importing NUnit.props, but this is not a test project -->
14+
<ProjectCapability Remove="TestContainer" />
15+
</ItemGroup>
1116

1217
<PropertyGroup>
1318
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>

Solutions/Corvus.Testing.AzureFunctions.SpecFlow/Corvus.Testing.AzureFunctions.SpecFlow.csproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@
1010

1111
<PropertyGroup>
1212
<IsTestProject>false</IsTestProject>
13+
<TestProject>false</TestProject>
1314
</PropertyGroup>
15+
<ItemGroup>
16+
<!-- This project takes depedency on nUnit that adds the TestContainer capability importing NUnit.props, but this is not a test project -->
17+
<ProjectCapability Remove="TestContainer" />
18+
</ItemGroup>
1419

1520
<PropertyGroup>
1621
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>

Solutions/Corvus.Testing.AzureFunctions.Xunit.Demo/FunctionPerTestFacts.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public FunctionPerTestFacts(ITestOutputHelper output)
3535
.CreateLogger("Xunit Demo tests");
3636

3737
this.function = new FunctionsController(logger);
38-
this.Port = new Random().Next(50000, 60000);
38+
this.Port = this.function.FindAvailableTcpPort(50000, 60000);
3939
}
4040

4141
public int Port { get; }

Solutions/Corvus.Testing.AzureFunctions/Corvus.Testing.AzureFunctions.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
<PropertyGroup>
1313
<IsTestProject>false</IsTestProject>
14+
<TestProject>false</TestProject>
1415
</PropertyGroup>
1516

1617
<PropertyGroup>

Solutions/Corvus.Testing.AzureFunctions/Corvus/Testing/AzureFunctions/FunctionsController.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,34 @@ await Task.WhenAny(
133133
this.logger.LogInformation("Function {Path} now running on port {Port}", path, port);
134134
}
135135

136+
/// <summary>
137+
/// Randomly selects a port that appears to be available for use.
138+
/// </summary>
139+
/// <param name="lowerBoundInclusive">
140+
/// The lowest port number acceptable. Defaults to 50000.
141+
/// </param>
142+
/// <param name="upperBoundExclusive">
143+
/// The port number above which no port will be selected. Defaults to 60000.
144+
/// </param>
145+
/// <returns>A port number that seems to be available.</returns>
146+
public int FindAvailableTcpPort(int? lowerBoundInclusive, int? upperBoundExclusive)
147+
{
148+
int lb = lowerBoundInclusive ?? 50000;
149+
int ub = upperBoundExclusive ?? 60000;
150+
151+
var portsInRangeInUse = IPGlobalProperties
152+
.GetIPGlobalProperties()
153+
.GetActiveTcpListeners()
154+
.Select(e => e.Port)
155+
.Where(p => p >= lb && p < ub)
156+
.ToHashSet();
157+
158+
int availablePorts = ub - lb - portsInRangeInUse.Count;
159+
int availablePortOffset = Random.Shared.Next(availablePorts);
160+
int port = Enumerable.Range(lb, ub - lb).Where(p => !portsInRangeInUse.Contains(p)).ElementAt(availablePortOffset);
161+
return port;
162+
}
163+
136164
/// <summary>
137165
/// Provides access to the output.
138166
/// </summary>

Solutions/Corvus.Testing.SpecFlow.NUnit/Corvus.Testing.SpecFlow.NUnit.csproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@
77

88
<PropertyGroup>
99
<IsTestProject>false</IsTestProject>
10+
<TestProject>false</TestProject>
1011
</PropertyGroup>
12+
<ItemGroup>
13+
<!-- This project takes depedency on nUnit that adds the TestContainer capability importing NUnit.props, but this is not a test project -->
14+
<ProjectCapability Remove="TestContainer" />
15+
</ItemGroup>
1116

1217
<PropertyGroup>
1318
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>

Solutions/Corvus.Testing.SpecFlow/Corvus.Testing.SpecFlow.csproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@
1010

1111
<PropertyGroup>
1212
<IsTestProject>false</IsTestProject>
13+
<TestProject>false</TestProject>
1314
</PropertyGroup>
15+
<ItemGroup>
16+
<!-- This project takes depedency on nUnit that adds the TestContainer capability importing NUnit.props, but this is not a test project -->
17+
<ProjectCapability Remove="TestContainer" />
18+
</ItemGroup>
1419

1520
<PropertyGroup>
1621
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>

0 commit comments

Comments
 (0)