Skip to content

Commit

Permalink
Added UnitTest that exposes hanging dispose on a blocked connection
Browse files Browse the repository at this point in the history
Added TimeOut to Abort call to prevent waiting forever in Dispose

Upped timeout to 15 seconds to prevent false positives
  • Loading branch information
ceeskaasnice authored and lukebakken committed Feb 25, 2022
1 parent 39a9f2b commit 23f6613
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ public void Dispose()

try
{
this.Abort();
this.Abort(TimeSpan.FromSeconds(15));
}
catch (Exception)
{
Expand Down
4 changes: 2 additions & 2 deletions projects/RabbitMQ.Client/client/impl/Connection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@

namespace RabbitMQ.Client.Framing.Impl
{
#nullable enable
#nullable enable
internal sealed partial class Connection : IConnection
{
private bool _disposed;
Expand Down Expand Up @@ -410,7 +410,7 @@ public void Dispose()

try
{
this.Abort();
this.Abort(TimeSpan.FromSeconds(15));
_mainLoopTask.Wait();
}
catch (OperationInterruptedException)
Expand Down
24 changes: 23 additions & 1 deletion projects/Unit/TestConnectionBlocked.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

using System;
using System.Threading;

using System.Threading.Tasks;
using RabbitMQ.Client.Events;

using Xunit;
Expand All @@ -42,6 +42,7 @@ namespace RabbitMQ.Client.Unit
[Collection("NoParallelization")]
public class TestConnectionBlocked : IntegrationFixture
{
private readonly ManualResetEventSlim _connDisposed = new ManualResetEventSlim(false);
private readonly object _lockObject = new object();
private bool _notified;

Expand All @@ -68,6 +69,7 @@ protected override void ReleaseResources()
[Fact]
public void TestConnectionBlockedNotification()
{
_notified = false;
_conn.ConnectionBlocked += HandleBlocked;
_conn.ConnectionUnblocked += HandleUnblocked;

Expand All @@ -85,5 +87,25 @@ public void TestConnectionBlockedNotification()
Assert.True(false, "Unblock notification not received.");
}
}

[Fact]
public void TestDisposeOnBlockedConnectionDoesNotHang()
{
_notified = false;
Block();
Task.Factory.StartNew(DisposeConnection);

if (!_connDisposed.Wait(TimeSpan.FromSeconds(20)))
{
Unblock();
Assert.True(false, "Dispose must have finished within 20 seconds after starting");
}
}

private void DisposeConnection()
{
_conn.Dispose();
_connDisposed.Set();
}
}
}

0 comments on commit 23f6613

Please sign in to comment.