Path: redb-route/tests/redb.Route.Tests.IbmMq/IbmMqConcurrencyTests.cs
Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route
Page: https://redbase.app/src/dev-2026-12-06/redb-route/tests/redb.Route.Tests.IbmMq/IbmMqConcurrencyTests.cs
----
using System.Collections.Concurrent;
using redb.Route.Abstractions;
using redb.Route.Core;
using redb.Route.IbmMq;
using Xunit.Abstractions;
namespace redb.Route.Tests.IbmMq;
///
/// End-to-end regression tests for the consumer-concurrency fix: ConcurrentConsumers(N) must
/// actually process up to N messages in parallel (it used to be a dead semaphore on a serial MQGET
/// loop). Each worker runs its own dedicated MQQueueManager, so N workers on a queue opened
/// INPUT_SHARED are real competing consumers. The bound itself (two at a time under a slow route) is a case of
/// ; here every message is processed exactly once. Expects IBM MQ at
/// localhost:1414 (QM1, DEV.APP.SVRCONN, app/admin).
///
[Trait("Category", "Integration")]
[Collection("IbmMqIntegration")]
public sealed class IbmMqConcurrencyTests
{
private const string Host = "localhost";
private const int Port = 1414;
private const string Channel = "DEV.APP.SVRCONN";
private const string QueueManager = "QM1";
private const string User = "app";
private const string Password = "admin";
private readonly ITestOutputHelper _output;
public IbmMqConcurrencyTests(ITestOutputHelper output) => _output = output;
private static IbmMqEndpoint CreateEndpoint(string destination, string? extraParams = null)
{
var qs = $"host={Host}&port={Port}&channel={Channel}&queueManager={QueueManager}&user={User}&password={Password}";
if (extraParams is not null) qs += $"&{extraParams}";
var uri = EndpointUriParser.Parse($"wmq:{destination}?{qs}");
var component = new IbmMqComponent();
return (IbmMqEndpoint)component.CreateEndpoint(uri);
}
private static async Task PublishAsync(string queue, IEnumerable bodies)
{
var ep = CreateEndpoint(queue);
var producer = (IbmMqProducer)ep.CreateProducer();
await producer.Start();
foreach (var b in bodies)
await producer.Process(new Exchange(new Message(b)));
await producer.Stop();
await ep.Stop();
}
/// Drains any leftover messages from the shared dev queue so the test starts clean.
private static async Task DrainAsync(string queue)
{
var ep = CreateEndpoint(queue, "waitInterval=1500");
var processor = Substitute.For();
processor.Process(Arg.Any(), Arg.Any()).Returns(Task.CompletedTask);
var consumer = (IbmMqConsumer)ep.CreateConsumer(processor);
await consumer.Start();
await Task.Delay(2500);
await consumer.Stop();
await ep.Stop();
}
[Fact]
public async Task ConcurrentConsumers_AllMessagesProcessedExactlyOnce()
{
// Competing consumers must SHARE the queue: every message processed once, no duplication/loss.
const int concurrency = 4;
const int messageCount = 24;
const string queue = "DEV.QUEUE.2";
await DrainAsync(queue);
await PublishAsync(queue, Enumerable.Range(0, messageCount).Select(i => $"m-{i}"));
var epCons = CreateEndpoint(queue, $"concurrentConsumers={concurrency}&waitInterval=2000");
var received = new ConcurrentBag();
var allDone = new TaskCompletionSource();
var processor = Substitute.For();
processor.Process(Arg.Any(), Arg.Any())
.Returns(ci =>
{
received.Add(ci.Arg().In.Body?.ToString() ?? "");
if (received.Count >= messageCount) allDone.TrySetResult();
return Task.CompletedTask;
});
var consumer = (IbmMqConsumer)epCons.CreateConsumer(processor);
await consumer.Start();
await Task.WhenAny(allDone.Task, Task.Delay(40_000));
await consumer.Stop();
await epCons.Stop();
var distinct = received.Distinct().ToList();
_output.WriteLine($"received={received.Count}, distinct={distinct.Count}");
distinct.Count.Should().Be(messageCount, "every message delivered exactly once across competing consumers");
received.Count.Should().Be(messageCount, "competing consumers share the queue — no duplicate delivery");
}
}