Skip to content

Commit c24b4e4

Browse files
committed
Follow-up to #1611
* Name new property `DispatchConsumersAsyncEnabled`. * Add a check on `BasicConsume` for when a regular dispatcher is used, and an async consumer passed.
1 parent 7d3c281 commit c24b4e4

File tree

7 files changed

+33
-7
lines changed

7 files changed

+33
-7
lines changed

projects/RabbitMQ.Client/PublicAPI.Shipped.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,6 @@ RabbitMQ.Client.IConnection.Endpoint.get -> RabbitMQ.Client.AmqpTcpEndpoint
511511
RabbitMQ.Client.IConnection.FrameMax.get -> uint
512512
RabbitMQ.Client.IConnection.Heartbeat.get -> System.TimeSpan
513513
RabbitMQ.Client.IConnection.IsOpen.get -> bool
514-
RabbitMQ.Client.IConnection.DispatchConsumersAsync.get -> bool
515514
RabbitMQ.Client.IConnection.Protocol.get -> RabbitMQ.Client.IProtocol
516515
RabbitMQ.Client.IConnection.QueueNameChangedAfterRecovery -> System.EventHandler<RabbitMQ.Client.Events.QueueNameChangedAfterRecoveryEventArgs>
517516
RabbitMQ.Client.IConnection.RecoveringConsumer -> System.EventHandler<RabbitMQ.Client.Events.RecoveringConsumerEventArgs>
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
RabbitMQ.Client.BasicProperties.BasicProperties(RabbitMQ.Client.ReadOnlyBasicProperties! input) -> void
1+
RabbitMQ.Client.BasicProperties.BasicProperties(RabbitMQ.Client.ReadOnlyBasicProperties! input) -> void
2+
RabbitMQ.Client.IConnection.DispatchConsumersAsyncEnabled.get -> bool

projects/RabbitMQ.Client/client/api/IConnection.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,9 @@ public interface IConnection : INetworkConnection, IDisposable
127127
IEnumerable<ShutdownReportEntry> ShutdownReport { get; }
128128

129129
/// <summary>
130-
/// Returns if connection is set to use asynchronous consumer dispatcher/>.
130+
/// Returns <c>true</c> if the connection is set to use asynchronous consumer dispatchers.
131131
/// </summary>
132-
public bool DispatchConsumersAsync { get; }
132+
public bool DispatchConsumersAsyncEnabled { get; }
133133

134134
/// <summary>
135135
/// Application-specific connection name, will be displayed in the management UI

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ public event EventHandler<RecoveringConsumerEventArgs> RecoveringConsumer
176176

177177
public IProtocol Protocol => Endpoint.Protocol;
178178

179-
public bool DispatchConsumersAsync => _config.DispatchConsumersAsync;
179+
public bool DispatchConsumersAsyncEnabled => _config.DispatchConsumersAsync;
180180

181181
public async ValueTask<RecoveryAwareChannel> CreateNonRecoveringChannelAsync(CancellationToken cancellationToken)
182182
{

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -975,12 +975,20 @@ public async Task<string> BasicConsumeAsync(string queue, bool autoAck, string c
975975
{
976976
if (ConsumerDispatcher is AsyncConsumerDispatcher)
977977
{
978-
if (!(consumer is IAsyncBasicConsumer))
978+
if (false == (consumer is IAsyncBasicConsumer))
979979
{
980980
throw new InvalidOperationException("When using an AsyncConsumerDispatcher, the consumer must implement IAsyncBasicConsumer");
981981
}
982982
}
983983

984+
if (ConsumerDispatcher is ConsumerDispatcher)
985+
{
986+
if (consumer is IAsyncBasicConsumer)
987+
{
988+
throw new InvalidOperationException("When using an ConsumerDispatcher, the consumer must not implement IAsyncBasicConsumer");
989+
}
990+
}
991+
984992
// NOTE:
985993
// Maybe don't dispose this instance because the CancellationToken must remain
986994
// valid for processing the response.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ internal Connection(ConnectionConfig config, IFrameHandler frameHandler)
101101
public int LocalPort => _frameHandler.LocalPort;
102102
public int RemotePort => _frameHandler.RemotePort;
103103

104-
public bool DispatchConsumersAsync => _config.DispatchConsumersAsync;
104+
public bool DispatchConsumersAsyncEnabled => _config.DispatchConsumersAsync;
105105

106106
public IDictionary<string, object?>? ServerProperties { get; private set; }
107107

projects/Test/Integration/TestConsumer.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,24 @@ public TestConsumer(ITestOutputHelper output) : base(output)
4949
{
5050
}
5151

52+
[Fact]
53+
public async Task AsyncConsumerShouldThrowInvalidOperationException()
54+
{
55+
bool sawException = false;
56+
QueueDeclareOk q = await _channel.QueueDeclareAsync(string.Empty, false, false, false);
57+
await _channel.BasicPublishAsync(string.Empty, q.QueueName, GetRandomBody(1024));
58+
var consumer = new AsyncEventingBasicConsumer(_channel);
59+
try
60+
{
61+
string consumerTag = await _channel.BasicConsumeAsync(q.QueueName, false, string.Empty, false, false, null, consumer);
62+
}
63+
catch (InvalidOperationException)
64+
{
65+
sawException = true;
66+
}
67+
Assert.True(sawException, "did not see expected InvalidOperationException");
68+
}
69+
5270
[Fact]
5371
public async Task TestBasicRoundtrip()
5472
{

0 commit comments

Comments
 (0)