Skip to content

Commit 9ae9896

Browse files
committed
More tweaks to code looking for a free port.
1 parent 33386a5 commit 9ae9896

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

Tools/LambdaTestTool-v2/tests/Amazon.Lambda.TestTool.IntegrationTests/BaseApiGatewayTest.cs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
using Microsoft.AspNetCore.Hosting;
1919
using System.Threading;
2020
using Microsoft.Extensions.Hosting;
21+
using Amazon.SecurityToken;
2122

2223
namespace Amazon.Lambda.TestTool.IntegrationTests;
2324

@@ -158,6 +159,7 @@ protected async Task<HttpResponseMessage> TestEndpoint(string routeName, int api
158159

159160
protected int GetFreePort()
160161
{
162+
Console.WriteLine("Looking for free port");
161163
var builder = WebApplication.CreateBuilder();
162164
builder.WebHost.UseUrls("http://127.0.0.1:0");
163165
var app = builder.Build();
@@ -167,8 +169,35 @@ protected int GetFreePort()
167169
var runTask = app.RunAsync(tokenSource.Token);
168170
var uri = new Uri(app.Urls.First());
169171

172+
using var client = new HttpClient();
173+
string? content = null;
174+
175+
Console.WriteLine($"Testing port: {uri.Port}");
176+
var timeout = DateTime.UtcNow.AddMinutes(1);
177+
while(DateTime.UtcNow < timeout)
178+
{
179+
try
180+
{
181+
content = client.GetStringAsync(uri).GetAwaiter().GetResult();
182+
Console.WriteLine("Port was successful");
183+
break;
184+
}
185+
catch
186+
{
187+
Thread.Sleep(100);
188+
}
189+
}
190+
191+
if (!string.Equals(content, "test"))
192+
{
193+
Console.WriteLine("Port test failed trying again");
194+
var recursivePort = GetFreePort();
195+
tokenSource.Cancel();
196+
return recursivePort;
197+
}
198+
170199
tokenSource.Cancel();
171-
Task.Delay(1000);
200+
Task.Delay(2000);
172201

173202
return uri.Port;
174203
}

0 commit comments

Comments
 (0)