diff --git a/TelegramSearchBot.Test/AppBootstrap/GarnetLuaScriptIntegrationTests.cs b/TelegramSearchBot.Test/AppBootstrap/GarnetLuaScriptIntegrationTests.cs index 28c97303..6ce2c710 100644 --- a/TelegramSearchBot.Test/AppBootstrap/GarnetLuaScriptIntegrationTests.cs +++ b/TelegramSearchBot.Test/AppBootstrap/GarnetLuaScriptIntegrationTests.cs @@ -12,18 +12,20 @@ namespace TelegramSearchBot.Test.AppBootstrap { public sealed class GarnetLuaScriptIntegrationTests { + private const int MaxGarnetStartAttempts = 5; + [Fact] public async Task EmbeddedGarnet_RunsProjectLuaScripts() { - var port = GetAvailablePort(); - using var server = new GarnetServer(SchedulerBootstrap.BuildGarnetArguments(port.ToString())); - server.Start(); + var (server, port) = StartGarnetWithRetry(); - using var redis = await ConnectWithRetryAsync(port); - var db = redis.GetDatabase(); + using (server) { + using var redis = await ConnectWithRetryAsync(port); + var db = redis.GetDatabase(); - await RunCodingAgentEnqueueScriptAsync(db); - await RunAgentChatLockScriptsAsync(db); - await RunMusicGenerationSemaphoreScriptAsync(db); + await RunCodingAgentEnqueueScriptAsync(db); + await RunAgentChatLockScriptsAsync(db); + await RunMusicGenerationSemaphoreScriptAsync(db); + } } private static async Task RunCodingAgentEnqueueScriptAsync(IDatabase db) { @@ -147,6 +149,26 @@ private static async Task ConnectWithRetryAsync(int port) throw new TimeoutException($"Timed out connecting to embedded Garnet on port {port}.", lastException); } + private static (GarnetServer Server, int Port) StartGarnetWithRetry() { + var exceptions = new List(); + for (var attempt = 0; attempt < MaxGarnetStartAttempts; attempt++) { + var port = GetAvailablePort(); + GarnetServer? server = null; + try { + server = new GarnetServer(SchedulerBootstrap.BuildGarnetArguments(port.ToString())); + server.Start(); + return (server, port); + } catch (Exception ex) { + exceptions.Add(ex); + server?.Dispose(); + } + } + + throw new InvalidOperationException( + $"Unable to start embedded Garnet after {MaxGarnetStartAttempts} attempts.", + new AggregateException(exceptions)); + } + private static int GetAvailablePort() { var listener = new TcpListener(IPAddress.Loopback, 0); listener.Start();