From 363b50b4b9b858d915a707be2d481727be31ad45 Mon Sep 17 00:00:00 2001 From: Weston Pace Date: Fri, 6 Jan 2023 17:13:38 -0800 Subject: [PATCH] GH-15234: modify TestWebFactory to use a dynamic port to avoid port conflicts --- .../Apache.Arrow.Flight.Tests/TestWebFactory.cs | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/csharp/test/Apache.Arrow.Flight.Tests/TestWebFactory.cs b/csharp/test/Apache.Arrow.Flight.Tests/TestWebFactory.cs index 9e6ebc476bb45..74873e733bf04 100644 --- a/csharp/test/Apache.Arrow.Flight.Tests/TestWebFactory.cs +++ b/csharp/test/Apache.Arrow.Flight.Tests/TestWebFactory.cs @@ -15,11 +15,14 @@ using System; using System.Collections.Generic; +using System.Linq; using System.Net; using System.Text; using Apache.Arrow.Flight.TestWeb; using Grpc.Net.Client; using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Hosting.Server; +using Microsoft.AspNetCore.Hosting.Server.Features; using Microsoft.AspNetCore.Server.Kestrel.Core; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; @@ -29,11 +32,20 @@ namespace Apache.Arrow.Flight.Tests public class TestWebFactory : IDisposable { readonly IHost host; + private int _port; public TestWebFactory(FlightStore flightStore) { host = WebHostBuilder(flightStore).Build(); //Create the server host.Start(); + var addressInfo = host.Services.GetRequiredService().Features.Get(); + if (addressInfo == null) + { + throw new Exception("No address info could be found for configured server"); + } + var address = addressInfo.Addresses.First(); + var addressUri = new Uri(address); + _port = addressUri.Port; AppContext.SetSwitch( "System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true); } @@ -46,7 +58,7 @@ private IHostBuilder WebHostBuilder(FlightStore flightStore) webBuilder .ConfigureKestrel(c => { - c.Listen(IPEndPoint.Parse("0.0.0.0:5001"), l => l.Protocols = HttpProtocols.Http2); + c.ListenAnyIP(0, l => l.Protocols = HttpProtocols.Http2); }) .UseStartup() .ConfigureServices(services => @@ -58,7 +70,7 @@ private IHostBuilder WebHostBuilder(FlightStore flightStore) public string GetAddress() { - return "http://127.0.0.1:5001"; + return $"http://127.0.0.1:{_port}"; } public GrpcChannel GetChannel()