Skip to content

Commit 23f6613

Browse files
ceeskaasnicelukebakken
authored andcommitted
Added UnitTest that exposes hanging dispose on a blocked connection
Added TimeOut to Abort call to prevent waiting forever in Dispose Upped timeout to 15 seconds to prevent false positives
1 parent 39a9f2b commit 23f6613

File tree

3 files changed

+26
-4
lines changed

3 files changed

+26
-4
lines changed

projects/RabbitMQ.Client/client/impl/AutorecoveringConnection.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ public void Dispose()
216216

217217
try
218218
{
219-
this.Abort();
219+
this.Abort(TimeSpan.FromSeconds(15));
220220
}
221221
catch (Exception)
222222
{

projects/RabbitMQ.Client/client/impl/Connection.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242

4343
namespace RabbitMQ.Client.Framing.Impl
4444
{
45-
#nullable enable
45+
#nullable enable
4646
internal sealed partial class Connection : IConnection
4747
{
4848
private bool _disposed;
@@ -410,7 +410,7 @@ public void Dispose()
410410

411411
try
412412
{
413-
this.Abort();
413+
this.Abort(TimeSpan.FromSeconds(15));
414414
_mainLoopTask.Wait();
415415
}
416416
catch (OperationInterruptedException)

projects/Unit/TestConnectionBlocked.cs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232
using System;
3333
using System.Threading;
34-
34+
using System.Threading.Tasks;
3535
using RabbitMQ.Client.Events;
3636

3737
using Xunit;
@@ -42,6 +42,7 @@ namespace RabbitMQ.Client.Unit
4242
[Collection("NoParallelization")]
4343
public class TestConnectionBlocked : IntegrationFixture
4444
{
45+
private readonly ManualResetEventSlim _connDisposed = new ManualResetEventSlim(false);
4546
private readonly object _lockObject = new object();
4647
private bool _notified;
4748

@@ -68,6 +69,7 @@ protected override void ReleaseResources()
6869
[Fact]
6970
public void TestConnectionBlockedNotification()
7071
{
72+
_notified = false;
7173
_conn.ConnectionBlocked += HandleBlocked;
7274
_conn.ConnectionUnblocked += HandleUnblocked;
7375

@@ -85,5 +87,25 @@ public void TestConnectionBlockedNotification()
8587
Assert.True(false, "Unblock notification not received.");
8688
}
8789
}
90+
91+
[Fact]
92+
public void TestDisposeOnBlockedConnectionDoesNotHang()
93+
{
94+
_notified = false;
95+
Block();
96+
Task.Factory.StartNew(DisposeConnection);
97+
98+
if (!_connDisposed.Wait(TimeSpan.FromSeconds(20)))
99+
{
100+
Unblock();
101+
Assert.True(false, "Dispose must have finished within 20 seconds after starting");
102+
}
103+
}
104+
105+
private void DisposeConnection()
106+
{
107+
_conn.Dispose();
108+
_connDisposed.Set();
109+
}
88110
}
89111
}

0 commit comments

Comments
 (0)