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)ex.In.Body!) .Process(ex => { parts.Enqueue((ex.In.Body is byte[] b ? Encoding.UTF8.GetString(b) : ex.In.Body?.ToString() ?? "", ex.In.Headers.ContainsKey(IbmMqHeaders.MsgId), ex.GetProperty(ExchangePropertyKeys.SplitIndex))); if (parts.Count == 2) done.TrySetResult(); }) .EndSplit()); await using (context) { await context.Start(); await done.Task.WaitAsync(TimeSpan.FromSeconds(30)); } parts.Should().Equal(("a", true, 0), ("b", true, 1)); } [Fact] public async Task A_request_goes_to_the_backout_queue_and_the_others_run() { Reset(); IbmMqPoisonTests.Put(Queue, "a"); PutRequest("request", "SOMEBODYS.REPLIES"); IbmMqPoisonTests.Put(Queue, "b"); var batched = new ConcurrentQueue(); var both = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); await using (await Start("pollTimeoutMs=1500", ex => { foreach (var record in Records(ex)) batched.Enqueue(Text(record)); if (batched.Count >= 2) both.TrySetResult(); return Task.CompletedTask; })) { await both.Task.WaitAsync(TimeSpan.FromSeconds(30)); await Task.Delay(500); } batched.Should().BeEquivalentTo(["a", "b"]); IbmMqPoisonTests.Drain(BackoutQueue, waitMs: 2000).Should().Equal("request"); } [Fact] public async Task With_disableReplyTo_a_request_is_batched_and_nobody_replies() { Reset(); PutRequest("request", "SOMEBODYS.REPLIES"); var batched = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); await using (await Start("pollTimeoutMs=500&disableReplyTo=true", ex => { batched.TrySetResult(Text(Records(ex)[0])); return Task.CompletedTask; })) { (await batched.Task.WaitAsync(TimeSpan.FromSeconds(30))).Should().Be("request"); } IbmMqPoisonTests.Drain(BackoutQueue).Should().BeEmpty(); } [Fact] public async Task ConcurrentConsumers_bounds_how_many_batches_run_at_once() { Reset(); IbmMqPoisonTests.Put(Queue, Enumerable.Range(0, 12).Select(i => $"m{i}").ToArray()); var running = 0; var maxRunning = 0; var processed = 0; var all = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); await using (await Start("maxBatchMessages=3&pollTimeoutMs=500&concurrentConsumers=2", async ex => { var now = Interlocked.Increment(ref running); int seen; while ((seen = Volatile.Read(ref maxRunning)) < now && Interlocked.CompareExchange(ref maxRunning, now, seen) != seen) { } await Task.Delay(300); Interlocked.Decrement(ref running); if (Interlocked.Add(ref processed, Records(ex).Count) == 12) all.TrySetResult(); })) { await all.Task.WaitAsync(TimeSpan.FromSeconds(60)); } maxRunning.Should().BeInRange(1, 2, "two workers run at most two batches at once"); IbmMqPoisonTests.Drain(Queue).Should().BeEmpty(); } }