From fdec2b3d177ba50a073917d592dcef0f76bb777e Mon Sep 17 00:00:00 2001 From: Jordan Dominion Date: Mon, 26 Jun 2023 10:22:38 -0400 Subject: [PATCH] Hopefully fix swarm test spurious errors Also add more logging --- .../Swarm/TestSwarmProtocol.cs | 36 +++++++++++++++---- 1 file changed, 30 insertions(+), 6 deletions(-) diff --git a/tests/Tgstation.Server.Host.Tests/Swarm/TestSwarmProtocol.cs b/tests/Tgstation.Server.Host.Tests/Swarm/TestSwarmProtocol.cs index be23a3bce7..80ef9bd8f0 100644 --- a/tests/Tgstation.Server.Host.Tests/Swarm/TestSwarmProtocol.cs +++ b/tests/Tgstation.Server.Host.Tests/Swarm/TestSwarmProtocol.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Diagnostics; using System.IO; using System.Threading; using System.Threading.Tasks; @@ -21,6 +22,7 @@ namespace Tgstation.Server.Host.Swarm.Tests { static readonly HashSet usedPorts = new (); static ILoggerFactory loggerFactory; + static ILogger logger; static ISeekableFileStreamProvider updateFileStreamProvider; @@ -33,6 +35,8 @@ namespace Tgstation.Server.Host.Swarm.Tests builder.AddConsole(); }); + logger = loggerFactory.CreateLogger(); + var ms = new MemoryStream(new byte[] { 1, 2, 3, 4 }); updateFileStreamProvider = new BufferedFileStreamProvider(ms); @@ -122,14 +126,16 @@ namespace Tgstation.Server.Host.Swarm.Tests await using var node1 = GenNode(controller); TestableSwarmNode.Link(controller, node1); - Assert.AreEqual(SwarmRegistrationResult.Success, await controller.TryInit()); + + node1.RpcMapper.AsyncRequests = false; Assert.AreEqual(SwarmRegistrationResult.Success, await node1.TryInit()); + node1.RpcMapper.AsyncRequests = true; Assert.AreEqual(2, controller.Service.GetSwarmServers().Count); Assert.AreEqual(1, node1.Service.GetSwarmServers().Count); - await DelayMax(() => Assert.AreEqual(2, node1.Service.GetSwarmServers().Count)); + await DelayMax(() => Assert.AreEqual(2, node1.Service.GetSwarmServers().Count), 15); // node checks every 5 minutes, delays are 100ms await controller.SimulateReboot(default); Assert.AreEqual(SwarmRegistrationResult.Success, await controller.TryInit()); @@ -283,20 +289,38 @@ namespace Tgstation.Server.Host.Swarm.Tests return new TestableSwarmNode(loggerFactory, config, version); } - static async Task DelayMax(Action assertion, ulong seconds = 1) + static async Task DelayMax(Action assertion, long seconds = 1) { - for (var i = 0U; i < (seconds * 10); ++i) + var id = Guid.NewGuid(); + logger.LogInformation("Begin DelayMax {id}: {seconds}", id, seconds); + var stopwatch = Stopwatch.StartNew(); + do { try { assertion(); + logger.LogInformation("End DelayMax {id} after {milliseconds}ms", id, stopwatch.ElapsedMilliseconds); return; } - catch (AssertFailedException) { } + catch (AssertFailedException) + { + } + await Task.Delay(TimeSpan.FromMilliseconds(100)); } + while (stopwatch.ElapsedMilliseconds < (seconds * 1000)); - assertion(); + try + { + assertion(); + } + catch + { + logger.LogError("Fail DelayMax {id} after {milliseconds}ms", id, stopwatch.ElapsedMilliseconds); + throw; + } + + logger.LogInformation("End DelayMax {id} after {milliseconds}ms", id, stopwatch.ElapsedMilliseconds); } } }