Skip to content

Commit 99d5e40

Browse files
authored
Handle NullReferenceException and prove with Test (#1859)
2 parents 86a7b0f + f85a14f commit 99d5e40

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -681,7 +681,10 @@ public void HandleBasicCancelOk(string consumerTag)
681681
k.m_consumer = DefaultConsumer;
682682
}
683683

684-
ConsumerDispatcher.HandleBasicCancelOk(k.m_consumer, consumerTag);
684+
if (!(k.m_consumer is null))
685+
{
686+
ConsumerDispatcher.HandleBasicCancelOk(k.m_consumer, consumerTag);
687+
}
685688

686689
k.HandleCommand(IncomingCommand.Empty); // release the continuation.
687690
}
@@ -1073,7 +1076,10 @@ public void BasicCancel(string consumerTag)
10731076
_consumers.Remove(consumerTag);
10741077
}
10751078

1076-
ModelShutdown -= k.m_consumer.HandleModelShutdown;
1079+
if (!(k.m_consumer is null))
1080+
{
1081+
ModelShutdown -= k.m_consumer.HandleModelShutdown;
1082+
}
10771083
}
10781084

10791085
public void BasicCancelNoWait(string consumerTag)
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using System;
2+
using System.Threading;
3+
using NUnit.Framework;
4+
using RabbitMQ.Client.Events;
5+
6+
namespace RabbitMQ.Client.Unit
7+
{
8+
[TestFixture]
9+
public class TestBasicCancelAnyConsumer : IntegrationFixture
10+
{
11+
[Test]
12+
public void TestBasicCancelWithQueueDelete()
13+
{
14+
// Arrange
15+
Exception modelException = null;
16+
Model.CallbackException += HandleCallbackException;
17+
Model.QueueDeclare(TestContext.CurrentContext.Test.Name, false, false, false);
18+
string consumerTag = Model.BasicConsume(TestContext.CurrentContext.Test.Name, false, new VoidConsumer());
19+
20+
// Act
21+
WithTemporaryModel(m => m.QueueDelete(TestContext.CurrentContext.Test.Name));
22+
TestDelegate act = () => Model.BasicCancel(consumerTag);
23+
24+
// Assert
25+
Assert.DoesNotThrow(act);
26+
SpinWait.SpinUntil(() => !(modelException is null), TimingFixture.TestTimeout);
27+
Assert.IsNull(modelException, "The model has thrown an exception when cancelling a consumer after the queue has been deleted. This exception comes from the scheduling of CancelOk Action.");
28+
29+
// Cleanup
30+
Model.CallbackException -= HandleCallbackException;
31+
32+
void HandleCallbackException(object sender, CallbackExceptionEventArgs e) => modelException = e.Exception;
33+
}
34+
35+
private sealed class VoidConsumer : DefaultBasicConsumer{}
36+
}
37+
}

0 commit comments

Comments
 (0)