Path: redb-route/tests/redb.Route.Tests.IbmMq/IbmMqBatchConsumerTests.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/IbmMqBatchConsumerTests.cs
----
using System.Collections.Concurrent;
using System.Text;
using IBM.WMQ;
using redb.Route.Abstractions;
using redb.Route.Core;
using redb.Route.IbmMq;
namespace redb.Route.Tests.IbmMq;
///
/// The batch contract on the IBM MQ consumer against the broker: batches read with MQGET under syncpoint, collected by
/// size and idle timeout, the body shape, Split, a request moved to the backout queue (batched with disableReplyTo),
/// and concurrency bounded by concurrentConsumers. A batch accepted, rejected to the backout queue and returned at
/// stop are cases of .
///
[Trait("Category", "Integration")]
[Collection("IbmMqIntegration")]
public sealed class IbmMqBatchConsumerTests
{
private const string Queue = "DEV.QUEUE.BATCH";
private const string BackoutQueue = "DEV.QUEUE.BATCH.BOQ";
private const string Connection =
"host=localhost&port=1414&channel=DEV.APP.SVRCONN&queueManager=QM1&user=app&password=admin";
private static void Reset()
{
IbmMqPoisonTests.Drain(Queue);
IbmMqPoisonTests.Drain(BackoutQueue);
}
private static void PutRequest(string body, string replyTo)
{
using var qm = IbmMqPoisonTests.Connect();
var q = qm.AccessQueue(Queue, MQC.MQOO_OUTPUT | MQC.MQOO_FAIL_IF_QUIESCING);
var message = new MQMessage { Format = MQC.MQFMT_STRING, MessageType = MQC.MQMT_REQUEST, ReplyToQueueName = replyTo };
message.WriteString(body);
q.Put(message);
q.Close();
qm.Disconnect();
}
private static async Task Start(string options, Func route)
{
var context = new RouteContext();
context.AddComponent(new IbmMqComponent());
context.AddRoutes(r => r
.From($"wmq:{Queue}?{Connection}&batching=true&backoutQueue={BackoutQueue}&{options}")
.Process(async (ex, _) => await route(ex)));
await context.Start();
return context;
}
private static List Records(IExchange exchange) => (List)exchange.In.Body!;
private static string Text(IMessage message)
=> message.Body is byte[] bytes ? Encoding.UTF8.GetString(bytes) : message.Body?.ToString() ?? "";
[Fact]
public async Task A_batch_is_collected_up_to_maxBatchMessages_and_the_rest_by_the_idle_timeout()
{
Reset();
IbmMqPoisonTests.Put(Queue, "a", "b", "c", "d", "e", "f", "g");
var batches = new ConcurrentQueue>();
var all = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
await using (await Start("maxBatchMessages=3&pollTimeoutMs=1500", ex =>
{
batches.Enqueue(Records(ex).Select(Text).ToList());
if (batches.Sum(b => b.Count) == 7) all.TrySetResult();
return Task.CompletedTask;
}))
{
await all.Task.WaitAsync(TimeSpan.FromSeconds(60));
}
batches.Select(b => b.Count).Should().Equal(3, 3, 1);
batches.SelectMany(b => b).Should().Equal("a", "b", "c", "d", "e", "f", "g");
IbmMqPoisonTests.Drain(Queue).Should().BeEmpty("every batch was committed");
}
[Fact]
public async Task One_message_is_still_a_list_with_its_own_headers_and_the_destination_on_the_batch()
{
Reset();
IbmMqPoisonTests.Put(Queue, "only");
var seen = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
await using (await Start("pollTimeoutMs=500", ex => { seen.TrySetResult(ex); return Task.CompletedTask; }))
{
var batch = await seen.Task.WaitAsync(TimeSpan.FromSeconds(30));
batch.Pattern.Should().Be(ExchangePattern.InOnly);
var record = Records(batch).Should().ContainSingle().Subject;
Text(record).Should().Be("only");
record.Headers.Should().ContainKey(IbmMqHeaders.MsgId);
batch.In.Headers[IbmMqHeaders.Destination].Should().Be(Queue);
}
}
[Fact]
public async Task A_split_part_gets_its_messages_body_and_the_split_metadata_as_properties()
{
Reset();
IbmMqPoisonTests.Put(Queue, "a", "b");
var parts = new ConcurrentQueue<(string Body, bool HasMsgId, int Index)>();
var done = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
var context = new RouteContext();
context.AddComponent(new IbmMqComponent());
context.AddRoutes(r => r
.From($"wmq:{Queue}?{Connection}&batching=true&backoutQueue={BackoutQueue}&maxBatchMessages=2&pollTimeoutMs=2000")
.Split(ex => (IEnumerable