fix: resolve slopwatch issues — add logging to empty catches and eliminate test timing delays

Replace empty catch blocks with meaningful log statements in NatsServer,
NatsClient, and Program. Add WaitForReadyAsync() to NatsServer for
deterministic server startup. Replace Task.Delay/Thread.Sleep in tests
with PING/PONG protocol flush and SubscribeCoreAsync for reliable
subscription synchronization.
This commit is contained in:
Joseph Doherty
2026-02-22 21:14:16 -05:00
parent 539b2b7588
commit 2e1e1bb341
6 changed files with 93 additions and 105 deletions

View File

@@ -70,7 +70,10 @@ public sealed class NatsClient : IDisposable
await Task.WhenAny(fillTask, processTask);
}
catch (OperationCanceledException) { }
catch (OperationCanceledException)
{
_logger.LogDebug("Client {ClientId} operation cancelled", Id);
}
catch (Exception ex)
{
_logger.LogDebug(ex, "Client {ClientId} connection error", Id);

View File

@@ -15,11 +15,14 @@ public sealed class NatsServer : IMessageRouter, ISubListAccess, IDisposable
private readonly ServerInfo _serverInfo;
private readonly ILogger<NatsServer> _logger;
private readonly ILoggerFactory _loggerFactory;
private readonly TaskCompletionSource _listeningStarted = new(TaskCreationOptions.RunContinuationsAsynchronously);
private Socket? _listener;
private ulong _nextClientId;
public SubList SubList => _subList;
public Task WaitForReadyAsync() => _listeningStarted.Task;
public NatsServer(NatsOptions options, ILoggerFactory loggerFactory)
{
_options = options;
@@ -44,6 +47,7 @@ public sealed class NatsServer : IMessageRouter, ISubListAccess, IDisposable
_options.Host == "0.0.0.0" ? IPAddress.Any : IPAddress.Parse(_options.Host),
_options.Port));
_listener.Listen(128);
_listeningStarted.TrySetResult();
_logger.LogInformation("Listening on {Host}:{Port}", _options.Host, _options.Port);
@@ -64,7 +68,10 @@ public sealed class NatsServer : IMessageRouter, ISubListAccess, IDisposable
_ = RunClientAsync(client, ct);
}
}
catch (OperationCanceledException) { }
catch (OperationCanceledException)
{
_logger.LogDebug("Accept loop cancelled, server shutting down");
}
}
private async Task RunClientAsync(NatsClient client, CancellationToken ct)
@@ -73,9 +80,9 @@ public sealed class NatsServer : IMessageRouter, ISubListAccess, IDisposable
{
await client.RunAsync(ct);
}
catch (Exception)
catch (Exception ex)
{
// Client disconnected or errored
_logger.LogDebug(ex, "Client {ClientId} disconnected with error", client.Id);
}
finally
{