Skip to content

Commit

Permalink
Fix cancellation of RPC methods
Browse files Browse the repository at this point in the history
Fixes #1750

* Start by adding a test that reproduces the error. Give a 5ms cancellation to `BasicConsumeAsync`, with a much longer delay via Toxiproxy. If running in debug mode, you will see the same `task canceled` exception, but it does not propagate to the test itself.
  • Loading branch information
lukebakken committed Dec 26, 2024
1 parent a2a0967 commit f331ddf
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions projects/Test/Integration/TestToxiproxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
using System.Threading.Tasks;
using Integration;
using RabbitMQ.Client;
using RabbitMQ.Client.Events;
using RabbitMQ.Client.Exceptions;
using Toxiproxy.Net.Toxics;
using Xunit;
Expand Down Expand Up @@ -409,6 +410,47 @@ public async Task TestPublisherConfirmationThrottling()
Assert.Equal(TotalMessageCount, publishCount);
}

[SkippableFact]
[Trait("Category", "Toxiproxy")]
public async Task TestBasicConsumeCancellation_GH1750()
{
Skip.IfNot(AreToxiproxyTestsEnabled, "RABBITMQ_TOXIPROXY_TESTS is not set, skipping test");
Assert.NotNull(_toxiproxyManager);
Assert.Null(_conn);
Assert.Null(_channel);

ConnectionFactory cf = CreateConnectionFactory();
cf.AutomaticRecoveryEnabled = false;
cf.TopologyRecoveryEnabled = false;

cf.Endpoint = new AmqpTcpEndpoint(IPAddress.Loopback.ToString(), _proxyPort);
_conn = await cf.CreateConnectionAsync();
_channel = await _conn.CreateChannelAsync();

QueueDeclareOk q = await _channel.QueueDeclareAsync();

var consumer = new AsyncEventingBasicConsumer(_channel);
consumer.ReceivedAsync += (o, a) =>
{
return Task.CompletedTask;
};

string toxicName = $"rmq-localhost-delay-{Now}-{GenerateShortUuid()}";
var latencyToxic = new LatencyToxic
{
Name = toxicName
};
latencyToxic.Attributes.Latency = 500;
latencyToxic.Toxicity = 1.0;
latencyToxic.Stream = ToxicDirection.DownStream;

Task<LatencyToxic> addToxicTask = _toxiproxyManager.AddToxicAsync(latencyToxic);
await addToxicTask.WaitAsync(WaitSpan);

using var cts = new CancellationTokenSource(5);
await _channel.BasicConsumeAsync(q.QueueName, true, consumer, cts.Token);
}

private bool AreToxiproxyTestsEnabled
{
get
Expand Down

0 comments on commit f331ddf

Please sign in to comment.