Path: redb-route/tests/redb.Route.Tests.IbmMq/IbmMqReplyToBridgeTests.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/IbmMqReplyToBridgeTests.cs ---- using System.Collections; using IBM.WMQ; using redb.Route.Abstractions; using redb.Route.Core; using redb.Route.IbmMq; namespace redb.Route.Tests.IbmMq; /// /// A sender's reply-to does not travel across a bridge, as in Apache Camel's JMS: a message consumed with a reply-to /// queue and forwarded by an ordinary put goes out without it, unless the producer sets preserveMessageQos=true. /// The correlation id is carried; a request-reply producer sets its own reply-to. Expects IBM MQ at localhost:1414 /// with the queues of tests/docker/ibmmq/redb-tests.mqsc. /// [Trait("Category", "Integration")] [Collection("IbmMqIntegration")] public sealed class IbmMqReplyToBridgeTests { private const string Input = "DEV.QUEUE.BRIDGE.IN"; private const string Output = "DEV.QUEUE.BRIDGE.OUT"; private const string Connection = "host=localhost&port=1414&channel=DEV.APP.SVRCONN&queueManager=QM1&user=app&password=admin"; private static MQQueueManager Connect() => new("QM1", new Hashtable { [MQC.HOST_NAME_PROPERTY] = "localhost", [MQC.PORT_PROPERTY] = 1414, [MQC.CHANNEL_PROPERTY] = "DEV.APP.SVRCONN", [MQC.USER_ID_PROPERTY] = "app", [MQC.PASSWORD_PROPERTY] = "admin", [MQC.TRANSPORT_PROPERTY] = MQC.TRANSPORT_MQSERIES_MANAGED, }); private static readonly byte[] CorrelationId = Enumerable.Range(1, 24).Select(i => (byte)i).ToArray(); /// /// Puts one datagram on the input queue with the given reply-to queue and correlation id, bridges it by a route to /// the output queue, and returns the message as it arrived there. /// private static async Task Bridge(string producerOptions, string? senderReplyTo, Action? routeStep = null) { var marker = Guid.NewGuid().ToString("N"); using (var qm = Connect()) { var q = qm.AccessQueue(Input, MQC.MQOO_OUTPUT | MQC.MQOO_FAIL_IF_QUIESCING); var put = new MQMessage { CorrelationId = CorrelationId, Format = MQC.MQFMT_STRING }; if (senderReplyTo is not null) put.ReplyToQueueName = senderReplyTo; put.WriteString(marker); q.Put(put); q.Close(); qm.Disconnect(); } await using var context = new RouteContext(); context.AddComponent(new IbmMqComponent()); context.AddRoutes(r => r .From($"wmq:{Input}?{Connection}") .Process(ex => routeStep?.Invoke(ex)) .To($"wmq:{Output}?{Connection}{producerOptions}")); await context.Start(); using var reader = Connect(); var output = reader.AccessQueue(Output, MQC.MQOO_INPUT_SHARED | MQC.MQOO_FAIL_IF_QUIESCING); try { var gmo = new MQGetMessageOptions { Options = MQC.MQGMO_WAIT, WaitInterval = 20_000 }; while (true) { var got = new MQMessage(); output.Get(got, gmo); // throws MQRC_NO_MSG_AVAILABLE after the wait if (got.ReadString(got.MessageLength) == marker) return got; } } finally { output.Close(); reader.Disconnect(); } } [Fact] public async Task A_forwarded_message_goes_out_without_the_senders_reply_to_and_keeps_its_correlation_id() { var forwarded = await Bridge(producerOptions: "", senderReplyTo: "SENDER.REPLIES"); forwarded.ReplyToQueueName.Trim().Should().BeEmpty("an ordinary put does not carry a sender's reply-to on"); forwarded.CorrelationId.Should().Equal(CorrelationId); } [Fact] public async Task With_preserveMessageQos_the_forwarded_message_keeps_the_senders_reply_to() { var forwarded = await Bridge(producerOptions: "&preserveMessageQos=true", senderReplyTo: "SENDER.REPLIES"); forwarded.ReplyToQueueName.Trim().Should().Be("SENDER.REPLIES"); forwarded.CorrelationId.Should().Equal(CorrelationId); } [Theory] [InlineData("", false)] [InlineData("&preserveMessageQos=true", true)] public async Task A_reply_to_header_set_by_the_route_follows_the_same_rule(string producerOptions, bool kept) { var forwarded = await Bridge(producerOptions, senderReplyTo: null, routeStep: ex => ex.In.Headers[IbmMqHeaders.ReplyToQueue] = "ROUTE.NAMED.REPLIES"); if (kept) forwarded.ReplyToQueueName.Trim().Should().Be("ROUTE.NAMED.REPLIES"); else forwarded.ReplyToQueueName.Trim().Should().BeEmpty(); } [Fact] public async Task A_request_reply_producer_sets_its_own_reply_to_over_a_forwarded_one() { const string queue = "DEV.QUEUE.4"; await using var context = new RouteContext(); context.AddComponent(new IbmMqComponent()); context.AddRoutes(r => r .From($"wmq:{queue}?{Connection}") .Process(ex => ex.Out = new Message("pong"))); await context.Start(); var producer = context.GetEndpoint($"wmq:{queue}?{Connection}&replyTo=true&timeout=20&preserveMessageQos=true") .CreateProducer(); await producer.Start(); try { var exchange = new Exchange(new Message("ping")); exchange.In.Headers[IbmMqHeaders.ReplyToQueue] = "A.QUEUE.OF.ANOTHER.SYSTEM"; exchange.In.Headers[IbmMqHeaders.ReplyToQueueManager] = "QM.OF.ANOTHER.SYSTEM"; await producer.Process(exchange); exchange.HasOut.Should().BeTrue("the reply came back to the producer's own reply queue"); var body = exchange.Out!.Body is byte[] bytes ? System.Text.Encoding.UTF8.GetString(bytes) : exchange.Out.Body?.ToString(); body.Should().Be("pong"); } finally { await producer.Stop(); } } }