Path: redb-route/tests/redb.Route.Tests.IbmMq/IbmMqListSendTests.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/IbmMqListSendTests.cs
----
using IBM.WMQ;
using redb.Route.Abstractions;
using redb.Route.Core;
using redb.Route.IbmMq;
namespace redb.Route.Tests.IbmMq;
///
/// A list body at the IBM MQ producer: each element of a list of IMessage is its own MQ message with its own body and
/// headers, all put under syncpoint and committed once; a failure in the middle backs the unit of work out and nothing
/// goes; a plain list and a list at a request-reply producer are refused.
///
[Trait("Category", "Integration")]
[Collection("IbmMqIntegration")]
public sealed class IbmMqListSendTests
{
private const string Queue = "DEV.QUEUE.LIST";
private const string Connection =
"host=localhost&port=1414&channel=DEV.APP.SVRCONN&queueManager=QM1&user=app&password=admin";
private static async Task StartProducer(string options = "")
{
var uri = EndpointUriParser.Parse($"wmq:{Queue}?{Connection}{options}");
var producer = (IbmMqProducer)((IbmMqEndpoint)new IbmMqComponent().CreateEndpoint(uri)).CreateProducer();
await producer.Start();
return producer;
}
private static List<(string Body, string? Catalogue)> TakeAll(int waitMs)
{
var got = new List<(string, string?)>();
using var qm = IbmMqPoisonTests.Connect();
var q = qm.AccessQueue(Queue, MQC.MQOO_INPUT_SHARED | MQC.MQOO_FAIL_IF_QUIESCING);
try
{
var gmo = new MQGetMessageOptions { Options = MQC.MQGMO_WAIT | MQC.MQGMO_NO_SYNCPOINT, WaitInterval = waitMs };
while (true)
{
var message = new MQMessage();
try { q.Get(message, gmo); }
catch (MQException ex) when (ex.ReasonCode == MQC.MQRC_NO_MSG_AVAILABLE) { return got; }
string? catalogue = null;
try { catalogue = message.GetStringProperty(IbmMqHeaders.HeaderCatalogue); }
catch (MQException) { /* no headers */ }
got.Add((message.ReadString(message.MessageLength), catalogue));
gmo.WaitInterval = 0;
}
}
finally
{
q.Close();
qm.Disconnect();
}
}
[Fact]
public async Task Each_message_of_a_list_is_its_own_MQ_message_with_its_own_body_and_headers()
{
IbmMqPoisonTests.Drain(Queue);
var producer = await StartProducer();
var a = new Message("a"); a.Headers["XRecord"] = "rec-a";
var b = new Message("b"); b.Headers["XRecord"] = "rec-b";
var parent = new Exchange(new Message(new List { a, b }));
parent.In.Headers["XParent"] = "must-not-travel";
await producer.Process(parent);
await producer.Stop();
var got = TakeAll(waitMs: 10_000);
got.Select(g => g.Body).Should().Equal("a", "b");
got[0].Catalogue.Should().Contain("rec-a").And.NotContain("must-not-travel");
got[1].Catalogue.Should().Contain("rec-b").And.NotContain("must-not-travel");
}
[Fact]
public async Task A_failure_in_the_middle_backs_the_list_out_and_nothing_goes()
{
IbmMqPoisonTests.Drain(Queue);
var producer = await StartProducer();
// Over the queue's default MAXMSGL (4 MB): the put of the second element fails.
var tooLarge = new Message(new byte[5 * 1024 * 1024]);
var act = () => producer.Process(new Exchange(new Message(new List { new Message("first"), tooLarge, new Message("third") })));
await act.Should().ThrowAsync();
await producer.Stop();
TakeAll(waitMs: 2000).Should().BeEmpty("the list is one unit of work: MQBACK took the first put back");
}
[Fact]
public async Task A_plain_list_is_refused()
{
IbmMqPoisonTests.Drain(Queue);
var producer = await StartProducer();
try
{
var act = () => producer.Process(new Exchange(new Message(new List { 1, 2 })));
await act.Should().ThrowAsync().WithMessage("*List*IMessage*");
}
finally
{
await producer.Stop();
}
TakeAll(waitMs: 1000).Should().BeEmpty("nothing, not even the type name, was sent");
}
[Fact]
public async Task A_list_at_a_request_reply_producer_is_refused()
{
var producer = await StartProducer("&replyTo=true&timeout=5");
try
{
var act = () => producer.Process(new Exchange(new Message(new List { new Message("a"), new Message("b") })));
await act.Should().ThrowAsync().WithMessage("*Split the list for request-reply*");
}
finally
{
await producer.Stop();
}
}
}