Skip to content

Commit 1a8d0cb

Browse files
authored
Merge pull request #1568 from rabbitmq/rabbitmq-dotnet-client-1567
Can't close channel from consumer dispatcher
2 parents f8087b6 + a835088 commit 1a8d0cb

File tree

1 file changed

+34
-2
lines changed

1 file changed

+34
-2
lines changed

projects/Test/Integration/TestAsyncConsumer.cs

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -623,9 +623,9 @@ public async Task TestCreateChannelWithinAsyncConsumerCallback_GH650()
623623
});
624624
};
625625

626-
// queue1 -> produce click to queue2
626+
// queue1 -> produce click to queue2
627627
// click -> exchange
628-
// queue2 -> consume click from queue1
628+
// queue2 -> consume click from queue1
629629
await _channel.ExchangeDeclareAsync(exchangeName, ExchangeType.Direct, autoDelete: true);
630630
await _channel.QueueDeclareAsync(queue1Name);
631631
await _channel.QueueBindAsync(queue1Name, exchangeName, queue1Name);
@@ -660,6 +660,38 @@ public async Task TestCreateChannelWithinAsyncConsumerCallback_GH650()
660660
Assert.True(await tcs.Task);
661661
}
662662

663+
[Fact]
664+
public async Task TestCloseWithinEventHandler_GH1567()
665+
{
666+
var tcs = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
667+
668+
QueueDeclareOk q = await _channel.QueueDeclareAsync();
669+
string queueName = q.QueueName;
670+
671+
var consumer = new AsyncEventingBasicConsumer(_channel);
672+
consumer.Received += async (_, eventArgs) =>
673+
{
674+
await _channel.BasicCancelAsync(eventArgs.ConsumerTag);
675+
#pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
676+
_channel.CloseAsync().ContinueWith((_) =>
677+
{
678+
_channel.Dispose();
679+
_channel = null;
680+
});
681+
#pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
682+
tcs.TrySetResult(true);
683+
};
684+
685+
await _channel.BasicConsumeAsync(consumer, queueName, true);
686+
687+
var bp = new BasicProperties();
688+
689+
await _channel.BasicPublishAsync(exchange: string.Empty, routingKey: queueName,
690+
basicProperties: bp, mandatory: true, body: GetRandomBody(64));
691+
692+
Assert.True(await tcs.Task);
693+
}
694+
663695
private static void SetException(Exception ex, params TaskCompletionSource<bool>[] tcsAry)
664696
{
665697
foreach (TaskCompletionSource<bool> tcs in tcsAry)

0 commit comments

Comments
 (0)