Path: redb-route/tests/redb.Route.Tests.IbmMq/IbmMqIntegrationTests.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/IbmMqIntegrationTests.cs ---- using System.Collections.Concurrent; using redb.Route.Abstractions; using redb.Route.Core; using redb.Route.Transactions; using redb.Route.Processors; using redb.Route.IbmMq; using Xunit.Abstractions; namespace redb.Route.Tests.IbmMq; /// /// Integration tests against a real IBM MQ broker (Developer Edition). /// Expects IBM MQ at localhost:1414, QM1, channel DEV.APP.SVRCONN, user app/admin. /// Docker: docker compose -f redb.Route/docker-compose.tests.yml --profile ibmmq up -d --wait /// [Trait("Category", "Integration")] [Collection("IbmMqIntegration")] public sealed class IbmMqIntegrationTests { 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 IbmMqIntegrationTests(ITestOutputHelper output) => _output = output; // ───── Helpers ───── private 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); } // ───── Basic send/receive ───── [Fact] public async Task Producer_SendsMessage_ConsumerReceives() { var queue = "DEV.QUEUE.1"; _output.WriteLine($"Queue: {queue}"); // Send var epProd = CreateEndpoint(queue); var producer = (IbmMqProducer)epProd.CreateProducer(); await producer.Start(); var exchange = new Exchange(new Message("Hello IBM MQ")); await producer.Process(exchange); await producer.Stop(); await epProd.Stop(); // The message length depends on the body's CCSID: 12 bytes in a single-byte or UTF-8 code page, more in UTF-16. ((IEndpointStatistics)epProd).BytesOut.Should().BeGreaterThanOrEqualTo(12, "the put counts its message bytes"); // Receive var epCons = CreateEndpoint(queue, "waitInterval=5000"); var received = new ConcurrentBag(); var tcs = new TaskCompletionSource(); var processor = Substitute.For(); processor.Process(Arg.Any(), Arg.Any()) .Returns(callInfo => { var ex = callInfo.Arg(); received.Add(ex.In.Body?.ToString() ?? ""); tcs.TrySetResult(); return Task.CompletedTask; }); var consumer = (IbmMqConsumer)epCons.CreateConsumer(processor); await consumer.Start(); await Task.WhenAny(tcs.Task, Task.Delay(20_000)); await consumer.Stop(); await epCons.Stop(); received.Should().Contain("Hello IBM MQ"); } // ───── Metadata headers ───── [Fact] public async Task Consumer_SetsIbmMqMetadataHeaders() { var queue = "DEV.QUEUE.2"; var epProd = CreateEndpoint(queue); var producer = (IbmMqProducer)epProd.CreateProducer(); await producer.Start(); var exchange = new Exchange(new Message("meta-test")); await producer.Process(exchange); await producer.Stop(); await epProd.Stop(); var epCons = CreateEndpoint(queue, "waitInterval=5000"); IExchange? capturedExchange = null; var tcs = new TaskCompletionSource(); var processor = Substitute.For(); processor.Process(Arg.Any(), Arg.Any()) .Returns(callInfo => { capturedExchange = callInfo.Arg(); tcs.TrySetResult(); return Task.CompletedTask; }); var consumer = (IbmMqConsumer)epCons.CreateConsumer(processor); await consumer.Start(); await Task.WhenAny(tcs.Task, Task.Delay(20_000)); await consumer.Stop(); await epCons.Stop(); capturedExchange.Should().NotBeNull(); capturedExchange!.In.Headers.Should().ContainKey(IbmMqHeaders.Destination); capturedExchange.In.Headers.Should().ContainKey(IbmMqHeaders.QueueManager); capturedExchange.In.Headers.Should().ContainKey(IbmMqHeaders.MsgId); capturedExchange.In.Headers.Should().ContainKey(IbmMqHeaders.Format); capturedExchange.In.Headers.Should().ContainKey(IbmMqHeaders.Persistence); capturedExchange.In.Headers.Should().ContainKey(IbmMqHeaders.Priority); capturedExchange.In.Headers.Should().ContainKey(IbmMqHeaders.MsgType); capturedExchange.In.Headers[IbmMqHeaders.Destination].Should().Be(queue); capturedExchange.In.Headers[IbmMqHeaders.QueueManager].Should().Be(QueueManager); } // ───── Transacted producer: the block decides ───── /// Runs inside a transacted block, as .Transacted() on a route does. private static Task InTransaction(IExchange exchange, Func body) => new TransactedProcessor(new DelegateProcessor(body), new TransactionPolicy()).Process(exchange); /// /// Sends one uniquely marked message from inside a .Transacted() block that then fails; returns what reached /// the shared queue during the receive window. /// private async Task> SendFromAFailedBlock(string body, string? producerParams) { const string queue = "DEV.QUEUE.2"; var epProd = CreateEndpoint(queue, producerParams); var producer = (IbmMqProducer)epProd.CreateProducer(); await producer.Start(); var failed = () => InTransaction(new Exchange(new Message(body)), async (ex, ct) => { await producer.Process(ex, ct); throw new InvalidOperationException("the unit of work failed"); }); await failed.Should().ThrowAsync(); await producer.Stop(); await epProd.Stop(); var epCons = CreateEndpoint(queue, "waitInterval=2000"); var received = new ConcurrentBag(); var processor = Substitute.For(); processor.Process(Arg.Any(), Arg.Any()) .Returns(callInfo => { received.Add(callInfo.Arg().In.Body?.ToString() ?? ""); return Task.CompletedTask; }); var consumer = (IbmMqConsumer)epCons.CreateConsumer(processor); await consumer.Start(); await Task.Delay(5_000); await consumer.Stop(); await epCons.Stop(); return received; } [Fact] public async Task Producer_without_transacted_joins_the_block_and_sends_nothing_when_it_fails() { var body = $"join-{Guid.NewGuid():N}"; var received = await SendFromAFailedBlock(body, producerParams: null); received.Should().NotContain(body, "inside .Transacted() a send joins the transaction unless transacted=false opts out"); } [Fact] public async Task Producer_with_transacted_false_sends_at_once_even_from_a_failed_block() { var body = $"optout-{Guid.NewGuid():N}"; var received = await SendFromAFailedBlock(body, "transacted=false"); received.Should().Contain(body); } [Fact] public async Task Deferred_puts_of_one_producer_commit_together_or_not_at_all() { const string queue = "DEV.QUEUE.2"; var first = $"batch-first-{Guid.NewGuid():N}"; var epProd = CreateEndpoint(queue); var producer = (IbmMqProducer)epProd.CreateProducer(); await producer.Start(); // Two puts in one block; the second is larger than the queue and channel accept, so it fails at the commit. var commitFailed = () => InTransaction(new Exchange(new Message(first)), async (ex, ct) => { await producer.Process(ex, ct); ex.In.Body = new string('x', 5 * 1024 * 1024); await producer.Process(ex, ct); }); await commitFailed.Should().ThrowAsync(); await producer.Stop(); await epProd.Stop(); ((IEndpointStatistics)epProd).BytesOut.Should().Be(0, "a batch backed out at the commit sent nothing"); var epCons = CreateEndpoint(queue, "waitInterval=2000"); var received = new ConcurrentBag(); var processor = Substitute.For(); processor.Process(Arg.Any(), Arg.Any()) .Returns(callInfo => { received.Add(callInfo.Arg().In.Body?.ToString() ?? ""); return Task.CompletedTask; }); var consumer = (IbmMqConsumer)epCons.CreateConsumer(processor); await consumer.Start(); await Task.Delay(5_000); await consumer.Stop(); await epCons.Stop(); received.Should().NotContain(first, "the puts of one producer in a block commit in one unit of work or not at all"); } [Fact] public void Request_reply_producer_refuses_transacted_true() { var endpoint = CreateEndpoint("DEV.QUEUE.1", "replyTo=true&transacted=true"); var act = () => endpoint.CreateProducer(); act.Should().Throw().WithMessage("*replyTo*transacted*"); } // ───── Multiple messages roundtrip ───── [Fact] public async Task Roundtrip_MultipleMessages() { var queue = "DEV.QUEUE.3"; const int messageCount = 10; var epProd = CreateEndpoint(queue); var producer = (IbmMqProducer)epProd.CreateProducer(); await producer.Start(); for (int i = 0; i < messageCount; i++) { var exchange = new Exchange(new Message($"msg-{i}")); await producer.Process(exchange); } await producer.Stop(); await epProd.Stop(); var epCons = CreateEndpoint(queue, "waitInterval=5000"); var received = new ConcurrentBag(); var allReceived = new TaskCompletionSource(); var processor = Substitute.For(); processor.Process(Arg.Any(), Arg.Any()) .Returns(callInfo => { received.Add(callInfo.Arg().In.Body?.ToString() ?? ""); if (received.Count >= messageCount) allReceived.TrySetResult(); return Task.CompletedTask; }); var consumer = (IbmMqConsumer)epCons.CreateConsumer(processor); await consumer.Start(); await Task.WhenAny(allReceived.Task, Task.Delay(30_000)); await consumer.Stop(); await epCons.Stop(); received.Count.Should().BeGreaterThanOrEqualTo(messageCount); for (int i = 0; i < messageCount; i++) received.Should().Contain($"msg-{i}"); } // ───── RPC request/reply ───── [Fact] public async Task Rpc_RequestReply_ReturnsResponse() { var queue = "DEV.QUEUE.4"; // Server: consumer echoes back var epServer = CreateEndpoint(queue); var processor = Substitute.For(); processor.Process(Arg.Any(), Arg.Any()) .Returns(callInfo => { var ex = callInfo.Arg(); var body = ex.In.Body?.ToString() ?? ""; ex.Out = new Message($"ECHO:{body}"); return Task.CompletedTask; }); var consumer = (IbmMqConsumer)epServer.CreateConsumer(processor); await consumer.Start(); // Client: RPC call var epClient = CreateEndpoint(queue, "replyTo=true&timeout=30"); var rpcProducer = (IbmMqProducer)epClient.CreateProducer(); await rpcProducer.Start(); var exchange = new Exchange(new Message("ping")); await rpcProducer.Process(exchange); await rpcProducer.Stop(); await epClient.Stop(); await consumer.Stop(); await epServer.Stop(); exchange.HasOut.Should().BeTrue(); exchange.Out!.Body?.ToString().Should().Be("ECHO:ping"); } // ───── Persistent message ───── [Fact] public async Task PersistentMessage_SurvivesAndDelivers() { var queue = "DEV.QUEUE.1"; var epProd = CreateEndpoint(queue, "persistence=Persistent"); var producer = (IbmMqProducer)epProd.CreateProducer(); await producer.Start(); var exchange = new Exchange(new Message("persistent-msg")); await producer.Process(exchange); await producer.Stop(); await epProd.Stop(); var epCons = CreateEndpoint(queue, "waitInterval=5000"); IExchange? captured = null; var tcs = new TaskCompletionSource(); var processor = Substitute.For(); processor.Process(Arg.Any(), Arg.Any()) .Returns(callInfo => { captured = callInfo.Arg(); tcs.TrySetResult(); return Task.CompletedTask; }); var consumer = (IbmMqConsumer)epCons.CreateConsumer(processor); await consumer.Start(); await Task.WhenAny(tcs.Task, Task.Delay(20_000)); await consumer.Stop(); await epCons.Stop(); captured.Should().NotBeNull(); captured!.In.Body?.ToString().Should().Be("persistent-msg"); } // ───── Transacted consumer ───── [Fact] public async Task TransactedConsumer_CommitsOnSuccess() { var queue = "DEV.QUEUE.2"; // Send a message normally var epProd = CreateEndpoint(queue); var producer = (IbmMqProducer)epProd.CreateProducer(); await producer.Start(); await producer.Process(new Exchange(new Message("tx-consumer-msg"))); await producer.Stop(); await epProd.Stop(); // Consume with transacted=true var epCons = CreateEndpoint(queue, "transacted=true&waitInterval=5000"); IExchange? captured = null; var tcs = new TaskCompletionSource(); var processor = Substitute.For(); processor.Process(Arg.Any(), Arg.Any()) .Returns(callInfo => { captured = callInfo.Arg(); tcs.TrySetResult(); return Task.CompletedTask; }); var consumer = (IbmMqConsumer)epCons.CreateConsumer(processor); await consumer.Start(); await Task.WhenAny(tcs.Task, Task.Delay(20_000)); // Wait for ProcessedCount to increment var sw = System.Diagnostics.Stopwatch.StartNew(); while (consumer.ProcessedCount < 1 && sw.ElapsedMilliseconds < 3_000) await Task.Delay(50); await consumer.Stop(); await epCons.Stop(); captured.Should().NotBeNull(); captured!.In.Body?.ToString().Should().Be("tx-consumer-msg"); consumer.ProcessedCount.Should().BeGreaterThanOrEqualTo(1); } // ───── Topic pub/sub ───── [Fact] public async Task Topic_PublishSubscribe() { var topic = "DEV/TEST/EVENTS"; // Start subscriber first var epSub = CreateEndpoint(topic, "destinationType=Topic&waitInterval=5000"); var received = new ConcurrentBag(); var tcs = new TaskCompletionSource(); var processor = Substitute.For(); processor.Process(Arg.Any(), Arg.Any()) .Returns(callInfo => { received.Add(callInfo.Arg().In.Body?.ToString() ?? ""); tcs.TrySetResult(); return Task.CompletedTask; }); var consumer = (IbmMqConsumer)epSub.CreateConsumer(processor); await consumer.Start(); await Task.Delay(2000); // let subscription establish // Publish var epPub = CreateEndpoint(topic, "destinationType=Topic"); var producer = (IbmMqProducer)epPub.CreateProducer(); await producer.Start(); var exchange = new Exchange(new Message("topic-event")); await producer.Process(exchange); await producer.Stop(); await epPub.Stop(); await Task.WhenAny(tcs.Task, Task.Delay(20_000)); await consumer.Stop(); await epSub.Stop(); received.Should().Contain("topic-event"); } // ───── Binary payload ───── [Fact] public async Task BinaryPayload_RoundTrip() { var queue = "DEV.QUEUE.4"; var epProd = CreateEndpoint(queue, "targetClient=Mq"); var producer = (IbmMqProducer)epProd.CreateProducer(); await producer.Start(); var payload = new byte[] { 0x01, 0x02, 0x03, 0xFE, 0xFF }; await producer.Process(new Exchange(new Message(payload))); await producer.Stop(); await epProd.Stop(); var epCons = CreateEndpoint(queue, "waitInterval=5000"); IExchange? captured = null; var tcs = new TaskCompletionSource(); var processor = Substitute.For(); processor.Process(Arg.Any(), Arg.Any()) .Returns(callInfo => { captured = callInfo.Arg(); tcs.TrySetResult(); return Task.CompletedTask; }); var consumer = (IbmMqConsumer)epCons.CreateConsumer(processor); await consumer.Start(); await Task.WhenAny(tcs.Task, Task.Delay(20_000)); await consumer.Stop(); await epCons.Stop(); captured.Should().NotBeNull(); captured!.In.Body.Should().BeOfType(); ((byte[])captured.In.Body!).Should().BeEquivalentTo(payload); } // ───── Expression support: Priority + Expiry from exchange headers ───── [Fact] public async Task Producer_ExpressionPriorityAndExpiry_ResolvedOnSend() { var queue = "DEV.QUEUE.1"; // Producer with expression-based priority and expiry var epProd = CreateEndpoint(queue, "priorityExpression=${header.prio}&expiryExpression=${header.exp}"); var producer = (IbmMqProducer)epProd.CreateProducer(); await producer.Start(); var msg = new Message("expression-mqmd"); msg.Headers["prio"] = "7"; msg.Headers["exp"] = "3000"; var exchange = new Exchange(msg); await producer.Process(exchange); await producer.Stop(); await epProd.Stop(); // Receive and verify MQMD fields var epCons = CreateEndpoint(queue, "waitInterval=5000"); IExchange? captured = null; var tcs = new TaskCompletionSource(); var processor = Substitute.For(); processor.Process(Arg.Any(), Arg.Any()) .Returns(callInfo => { captured = callInfo.Arg(); tcs.TrySetResult(); return Task.CompletedTask; }); var consumer = (IbmMqConsumer)epCons.CreateConsumer(processor); await consumer.Start(); await Task.WhenAny(tcs.Task, Task.Delay(20_000)); await consumer.Stop(); await epCons.Stop(); captured.Should().NotBeNull(); captured!.In.Body?.ToString().Should().Be("expression-mqmd"); captured.In.Headers[IbmMqHeaders.Priority].Should().Be(7); captured.In.Headers[IbmMqHeaders.Expiry].Should().BeOfType() .Which.Should().BeGreaterThanOrEqualTo(1); // Expiry ticks down from 3000 } // ───── Expression support: Persistence from exchange headers ───── [Fact] public async Task Producer_ExpressionPersistence_ResolvedOnSend() { var queue = "DEV.QUEUE.2"; // Producer with expression-based persistence (by enum name) var epProd = CreateEndpoint(queue, "persistenceExpression=${header.pers}"); var producer = (IbmMqProducer)epProd.CreateProducer(); await producer.Start(); var msg = new Message("expression-persist"); msg.Headers["pers"] = "Persistent"; var exchange = new Exchange(msg); await producer.Process(exchange); await producer.Stop(); await epProd.Stop(); // Receive and verify MQMD persistence var epCons = CreateEndpoint(queue, "waitInterval=5000"); IExchange? captured = null; var tcs = new TaskCompletionSource(); var processor = Substitute.For(); processor.Process(Arg.Any(), Arg.Any()) .Returns(callInfo => { captured = callInfo.Arg(); tcs.TrySetResult(); return Task.CompletedTask; }); var consumer = (IbmMqConsumer)epCons.CreateConsumer(processor); await consumer.Start(); await Task.WhenAny(tcs.Task, Task.Delay(20_000)); await consumer.Stop(); await epCons.Stop(); captured.Should().NotBeNull(); captured!.In.Body?.ToString().Should().Be("expression-persist"); // MQC.MQPER_PERSISTENT = 1 captured.In.Headers[IbmMqHeaders.Persistence].Should().Be(1); } // ── The options sweep: targetClient ────────────────────────────────────── [Fact] public async Task TargetClientMq_SendsRawMessageWithoutProperties() { // targetClient=Mq: a legacy MQ app expects raw MQMD+body. Message properties are what // materializes as MQRFH2 on property-aware paths - in Mq mode none may be written. // The option was declared and read by nothing. var queue = "DEV.QUEUE.2"; var marker = Guid.NewGuid().ToString("N"); var endpoint = CreateEndpoint(queue, "targetClient=Mq"); var producer = (IbmMqProducer)endpoint.CreateProducer(); await producer.Start(); var msg = new Message(marker); msg.Headers["X-Custom"] = "must-not-travel"; await producer.Process(new Exchange(msg)); await producer.Stop(); // Raw read via MQ classes: the message must carry NO custom properties. var props = new System.Collections.Hashtable { [IBM.WMQ.MQC.HOST_NAME_PROPERTY] = Host, [IBM.WMQ.MQC.PORT_PROPERTY] = Port, [IBM.WMQ.MQC.CHANNEL_PROPERTY] = Channel, [IBM.WMQ.MQC.USER_ID_PROPERTY] = User, [IBM.WMQ.MQC.PASSWORD_PROPERTY] = Password, [IBM.WMQ.MQC.TRANSPORT_PROPERTY] = IBM.WMQ.MQC.TRANSPORT_MQSERIES_MANAGED, }; using var qm = new IBM.WMQ.MQQueueManager(QueueManager, props); var q = qm.AccessQueue(queue, IBM.WMQ.MQC.MQOO_INPUT_SHARED | IBM.WMQ.MQC.MQOO_FAIL_IF_QUIESCING); try { var raw = new IBM.WMQ.MQMessage(); var gmo = new IBM.WMQ.MQGetMessageOptions { Options = IBM.WMQ.MQC.MQGMO_WAIT, WaitInterval = 10000 }; string? body = null; while (body != marker) { raw = new IBM.WMQ.MQMessage(); q.Get(raw, gmo); body = raw.ReadString(raw.MessageLength); } var catalogue = (string?)null; try { catalogue = raw.GetStringProperty(IbmMqHeaders.HeaderCatalogue); } catch (IBM.WMQ.MQException) { /* property absent - exactly what Mq mode promises */ } catalogue.Should().BeNull( "targetClient=Mq must send a bare MQMD+body without message properties"); } finally { q.Close(); qm.Disconnect(); } } }