You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi,
one test in our code base - that was previously working - doesn't work anymore. N messages were published - correctly - over the MQ and then consumed by a consumer components (the component under test). After upgrading RabbitMQ and also the client the messages were not published anymore. Did the #878 introduced a change? Am I doing something wrong?
Expectations: all messages are published and confirmed and available to be consumed.
Results: few messages are published and confirmed, few messages are not confirmed and many get exceptions on publish.
Here the tests:
using NUnit.Framework;
using RabbitMQ.Client;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
public class RabbitMqTests
{
[Test]
public async Task TestMultithreadPublishWithBarrier()
{
var count = 500;
var barrier = new AsyncBarrier(count);
var exchangeName = nameof(TestMultithreadPublishWithBarrier);
var factory = new ConnectionFactory
{
UserName = "rabbitmq",
Password = "rabbitmq",
HostName = "localhost",
Port = 5672,
VirtualHost = "/",
};
var connection = factory.CreateConnection(nameof(TestMultithreadPublishWithBarrier));
var tasksList = new List<Task<(int Code, string? Error)>>();
using (var channel = connection.CreateModel())
{
channel.ExchangeDeclare(exchangeName, ExchangeType.Topic, true);
channel.ConfirmSelect();
for (int i = 0; i < count; i++)
{
var task = PublishAsync(channel, $"Message {i}", exchangeName, $"rabbit.test.{i}", barrier);
tasksList.Add(task);
}
await Task.WhenAll(tasksList);
Console.Error.WriteLine($"# publishing error: {tasksList.Where(t => t.Result.Code == 1).Count()}");
Console.Error.WriteLine($"# confirms error: {tasksList.Where(t => t.Result.Code == 2).Count()}");
Console.Error.WriteLine($"# nack-ed: {tasksList.Where(t => t.Result.Code == 3).Count()}");
Console.Error.WriteLine($"# not confirmed: {tasksList.Where(t => t.Result.Code == 4).Count()}");
Console.Error.WriteLine($"# publishing error: {tasksList.Where(t => t.Result.Code == 1).First().Result.Error}");
Assert.IsTrue(tasksList.Where(t => t.Result.Code == 0).Count() == count);
}
}
[Test]
public async Task TestMultithreadPublish()
{
var count = 500;
var exchangeName = nameof(TestMultithreadPublish);
var factory = new ConnectionFactory
{
UserName = "rabbitmq",
Password = "rabbitmq",
HostName = "localhost",
Port = 5672,
VirtualHost = "/",
};
var connection = factory.CreateConnection(nameof(TestMultithreadPublish));
using (var channel = connection.CreateModel())
{
channel.ExchangeDeclare(exchangeName, ExchangeType.Topic, true);
channel.ConfirmSelect();
var tasksList = new List<Task<(int Code, string? Error)>>();
for (int i = 0; i < count; i++)
{
var task = Task.Run(() => Publish(channel, $"Message {i}", exchangeName, $"rabbit.test.{i}"));
tasksList.Add(task);
}
await Task.WhenAll(tasksList);
Console.Error.WriteLine($"# publishing error: {tasksList.Where(t => t.Result.Code == 1).Count()}");
Console.Error.WriteLine($"# confirms error: {tasksList.Where(t => t.Result.Code == 2).Count()}");
Console.Error.WriteLine($"# nack-ed: {tasksList.Where(t => t.Result.Code == 3).Count()}");
Console.Error.WriteLine($"# not confirmed: {tasksList.Where(t => t.Result.Code == 4).Count()}");
Assert.IsTrue(tasksList.Where(t => t.Result.Code == 0).Count() == count);
}
}
private async Task<(int Code, string? Error)> PublishAsync(IModel channel, string message, string exchangeName, string routingKey, AsyncBarrier barrier)
{
await barrier.SignalAndWaitAsync();
return Publish(channel, message, exchangeName, routingKey);
}
private (int Code, string? Error) Publish(IModel channel, string message, string exchangeName, string routingKey)
{
byte[] byteMessage = Encoding.UTF8.GetBytes(message);
IBasicProperties props = channel.CreateBasicProperties();
props.ContentType = "application/json";
props.Persistent = true;
bool isConfirmed;
bool timedOut;
lock (channel)
{
try
{
channel.BasicPublish(exchangeName, routingKey, props, byteMessage);
}
catch (Exception e)
{
return (1, $"{message}: {e.ToString()}");
}
try
{
isConfirmed = channel.WaitForConfirms(new TimeSpan(0, 1, 0), out timedOut);
}
catch (Exception e)
{
return (2, $"{message}: {e.ToString()}");
}
}
if (!isConfirmed)
{
return (3, $"{message}: got NACK-ed");
}
if (timedOut)
{
return (4, $"{message}: got time-out on waiting for message confirmaiton");
}
return (0, null);
}
private class AsyncBarrier
{
private int _count;
private TaskCompletionSource<bool> _tcs;
private int _waiting;
public AsyncBarrier(int count)
{
_count = count;
_waiting = 0;
// Compiler doesn't recognize the initialization in a method...
_tcs = new TaskCompletionSource<bool>();
}
public Task SignalAndWaitAsync()
{
var tcs = _tcs;
if (Interlocked.Increment(ref _waiting) == _count)
{
// Reset();
tcs.SetResult(true);
}
return tcs.Task;
}
private void Reset()
{
_waiting = 0;
_tcs = new TaskCompletionSource<bool>();
}
}
}
I will convert this issue to a GitHub discussion. Currently GitHub will automatically close and lock the issue even though your question will be transferred and responded to elsewhere. This is to let you know that we do not intend to ignore this but this is how the current GitHub conversion mechanism makes it seem for the users :(
Hi,
one test in our code base - that was previously working - doesn't work anymore. N messages were published - correctly - over the MQ and then consumed by a consumer components (the component under test). After upgrading RabbitMQ and also the client the messages were not published anymore. Did the #878 introduced a change? Am I doing something wrong?
Expectations: all messages are published and confirmed and available to be consumed.
Results: few messages are published and confirmed, few messages are not confirmed and many get exceptions on publish.
Here the tests:
RabbitMQ instance
RabbitMQ logs
dotnet --info
The text was updated successfully, but these errors were encountered: