diff --git a/projects/RabbitMQ.Client/client/impl/AutorecoveringConnection.Recovery.cs b/projects/RabbitMQ.Client/client/impl/AutorecoveringConnection.Recovery.cs index 28df238d92..bbd06d982b 100644 --- a/projects/RabbitMQ.Client/client/impl/AutorecoveringConnection.Recovery.cs +++ b/projects/RabbitMQ.Client/client/impl/AutorecoveringConnection.Recovery.cs @@ -59,9 +59,6 @@ private void HandleConnectionShutdown(object _, ShutdownEventArgs args) static bool ShouldTriggerConnectionRecovery(ShutdownEventArgs args) { - // TODO - // string now = DateTime.UtcNow.ToString("o", System.Globalization.CultureInfo.InvariantCulture); - // Console.WriteLine("{0} [DEBUG] ShouldTriggerConnectionRecovery args {1}", now, args); if (args.Initiator == ShutdownInitiator.Peer) { if (args.ReplyCode == Constants.AccessRefused) diff --git a/projects/Test/Integration/TestToxiproxy.cs b/projects/Test/Integration/TestToxiproxy.cs index 7b5319bb72..72b5597e70 100644 --- a/projects/Test/Integration/TestToxiproxy.cs +++ b/projects/Test/Integration/TestToxiproxy.cs @@ -75,7 +75,6 @@ public async Task TestCloseConnection() cf.AutomaticRecoveryEnabled = true; cf.NetworkRecoveryInterval = TimeSpan.FromSeconds(1); cf.RequestedHeartbeat = TimeSpan.FromSeconds(1); - cf.SocketReadTimeout = cf.RequestedHeartbeat; var messagePublishedTcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); var connectionShutdownTcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); @@ -91,25 +90,37 @@ public async Task TestCloseConnection() _output.WriteLine($"[ERROR] unexpected callback exception {ea.Detail} {ea.Exception}"); recoverySucceededTcs.SetResult(false); }; + conn.ConnectionRecoveryError += (s, ea) => { _output.WriteLine($"[ERROR] connection recovery error {ea.Exception}"); recoverySucceededTcs.SetResult(false); }; + conn.ConnectionShutdown += (s, ea) => { - _output.WriteLine($"[INFO] connection shutdown"); + if (IsVerbose) + { + _output.WriteLine($"[INFO] connection shutdown"); + } + /* * Note: using TrySetResult because this callback will be called when the * test exits, and connectionShutdownTcs will have already been set */ connectionShutdownTcs.TrySetResult(true); }; + conn.RecoverySucceeded += (s, ea) => { - _output.WriteLine($"[INFO] connection recovery succeeded"); + if (IsVerbose) + { + _output.WriteLine($"[INFO] connection recovery succeeded"); + } + recoverySucceededTcs.SetResult(true); }; + async Task PublishLoop() { using (IChannel ch = await conn.CreateChannelAsync()) @@ -119,14 +130,42 @@ async Task PublishLoop() while (conn.IsOpen) { await ch.BasicPublishAsync("", q.QueueName, GetRandomBody()); - await ch.WaitForConfirmsAsync(); - await Task.Delay(TimeSpan.FromSeconds(1)); messagePublishedTcs.TrySetResult(true); + /* + * Note: + * In this test, it is possible that the connection + * will be closed before the ack is returned, + * and this await will throw an exception + */ + try + { + await ch.WaitForConfirmsAsync(); + } + catch (AlreadyClosedException ex) + { + if (IsVerbose) + { + _output.WriteLine($"[WARNING] WaitForConfirmsAsync ex: {ex}"); + } + } } + await ch.CloseAsync(); } } - await PublishLoop(); + + try + { + await PublishLoop(); + } + catch (Exception ex) + { + if (IsVerbose) + { + _output.WriteLine($"[WARNING] PublishLoop ex: {ex}"); + } + } + Assert.True(await testSucceededTcs.Task); await conn.CloseAsync(); }